SocketIsWritable

現在の時点でデータをソケットに書き込めるかどうかを確認します。

bool  SocketIsWritable(
  const int socket     // ソケットのハンドル
  );

パラメータ

socket

[in] SocketCreate関数で返されるソケットハンドルです。無効なハンドルが渡されると、5270 (ERR_NETSOCKET_INVALIDHANDLE)が_LastErrorに書かれます。

戻り値

書き込みが可能の場合はtrue、それ以外の場合はfalse

注意事項

この関数を使用すると、ソケットにデータを書き込めるかどうかを確認できます。

関数の実行時にシステムソケットでエラーが発生した場合、SocketConnectによって確立された接続は中断されます。

この関数は独自のスレッド内で実行されるエキスパートアドバイザーやスクリプトのみから呼び出すことが出来ます。指標から呼び出すと、GetLastError()はエラー4014「Function is not allowed for call(関数呼び出しの許可がありません)」を返します。

例:  

//+------------------------------------------------------------------+
//|                                             SocketIsWritable.mq5 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link     "https://www.mql5.com
#property version     "1.00"
#property description "Add Address to the list of allowed ones in the terminal settings to let the example work"
#property script_show_inputs
 
input string Address    ="www.mql5.com";
input int   Port       =80;
bool         ExtTLS =false;
//+------------------------------------------------------------------+
//| スクリプトプログラム開始関数                                              |
//+------------------------------------------------------------------+
void OnStart(void)
 {
//--- ソケットを作成し、そのハンドルを取得する
  int socket=SocketCreate();
//--- ハンドルを確認する
  if(socket!=INVALID_HANDLE)
    {
    //--- 成功したら接続する
    if(SocketConnect(socket,Address,Port,1000))
       {
        PrintFormat("Established connection to %s:%d",Address,Port);
 
        string   subject,issuer,serial,thumbprint;
        datetime expiration;
        //--- 接続が証明書で保護されている場合は、そのデータを表示する
        if(SocketTlsCertificate(socket,subject,issuer,serial,thumbprint,expiration))
          {
          Print("TLS certificate:");
          Print("   Owner:      ",subject);
          Print("   Issuer:     ",issuer);
          Print("   Number:     ",serial);
          Print("   Print:      ",thumbprint);
          Print("   Expiration: ",expiration);
          ExtTLS=true;
          }
        //--- サーバにGETリクエストを送信する
        if(HTTPSend(socket,"GET / HTTP/1.1\r\nHost: www.mql5.com\r\nUser-Agent: MT5\r\n\r\n"))
          {
          Print("GET request sent");
          //--- 応答を読む
          if(!HTTPRecv(socket,1000))
              Print("Failed to get a response, error ",GetLastError());
          }
        else
          Print("Failed to send GET request, error ",GetLastError());
       }
    else
       {
        PrintFormat("Connection to %s:%d failed, error %d",Address,Port,GetLastError());
       }
    //--- 操作ログの現在の時点でソケットにデータを書き込む機能を表示する
    if(SocketIsWritable(socket))
        Print("At the current moment in time, writing data to the socket is possible");
    else
        Print("It is not possible to write data to the socket at the current time");
    //--- 使用後はソケットを閉じる
    if(SocketClose(socket))
        Print("Now the socket is closed");
    }
  else
    Print("Failed to create a socket, error ",GetLastError());
  /*
  結果:
  At the current moment in time, writing data to the socket is possible
  Socket is closed now
  */
 }
//+------------------------------------------------------------------+
//| サーバにコマンドを送信する                                               |
//+------------------------------------------------------------------+
bool HTTPSend(int socket,string request)
 {
//--- 文字列を文字配列に変換し、終端のゼロを破棄する
  char req[];
  int len=StringToCharArray(request,req)-1;
 
  if(len<0)
    return(false);
//--- ポート443経由の安全なTLS接続が使用されている場合
  if(ExtTLS)
    return(SocketTlsSend(socket,req,len)==len);
//--- 通常のTCP接続が使用される場合
  return(SocketSend(socket,req,len)==len);
 }
//+------------------------------------------------------------------+
//| サーバの応答を読む                                                   |
//+------------------------------------------------------------------+
bool HTTPRecv(int socket,uint timeout_ms)
 {
  char   rsp[];
  string result;
  ulong timeout_check=GetTickCount64()+timeout_ms;
//--- ソケットからデータを読み出すが、タイムアウトを超えない範囲でデータを読み出す
  do
    {
    uint len=SocketIsReadable(socket);
 
    if(len)
       {
        int rsp_len;
        //--- 接続が安全かどうかに応じて異なる読み取りコマンド
        if(ExtTLS)
          rsp_len=SocketTlsRead(socket,rsp,len);
        else
          rsp_len=SocketRead(socket,rsp,len,timeout_ms);
        //--- 応答を解析する
        if(rsp_len>0)
          {
          result+=CharArrayToString(rsp,0,rsp_len);
          //--- 応答ヘッダーのみを表示する
          int header_end=StringFind(result,"\r\n\r\n");
 
          if(header_end>0)
             {
              Print("HTTP answer header received:");
              Print(StringSubstr(result,0,header_end));
              return(true);
             }
            //--- 読み取りタイムアウトの有効期限を更新する
          timeout_check=GetTickCount64()+timeout_ms;
          }
       }
    }
  while(GetTickCount64()<timeout_check && !IsStopped());
 
  return(false);
 }