SocketTlsSend

通过安全TLS连接发送数据。

int  SocketTlsSend(
   int           socket,               // 套接字
   const uchar&  buffer[],             // 数据缓冲区
   uint          buffer_len            // 缓冲区大小
   );

参数

socket

[in]  套接字句柄通过SocketCreate函数返回。当传递不正确的句柄时,错误号5270 (ERR_NETSOCKET_INVALIDHANDLE)会被写入_LastError

buffer

[in]  通过即将发送的数据,引用uchar类型数组。

buffer_len

[in]  “缓冲区”数组大小。

返回值

如果成功,返回写入套接的字节数。错误的话,返回-1。

注意

如果执行函数时系统套接字发生错误,那么通过SocketConnect建立的连接则中断。

如果数据写入错误,错误号5273 (ERR_NETSOCKET_IO_ERROR)会被写入_LastError

这个函数只能从EA交易和脚本中调用,因为它们在自己的执行线程中运行。如果从指标调用,GetLastError()则返回4014错误号– “函数不允许调用”。

示例:

//+------------------------------------------------------------------+
//|                                                SocketTlsSend.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   =443;
//+------------------------------------------------------------------+
//| 脚本程序起始函数                                                   |
//+------------------------------------------------------------------+
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);
           }
         //--- 向服务器发送 GET 请求
         string request="GET / HTTP/1.1\r\nHost: www.mql5.com\r\nUser-Agent: MT5\r\n\r\n";
         char   req[];
         int    len=StringToCharArray(request,req)-1;
 
         if(len<0)
           {
            Print("StringToCharArray() failed. Error "GetLastError());
            SocketClose(socket);
            return;
           }
         //--- 如果使用了通过端口443的安全TLS连接
         if(SocketTlsSend(socket,req,len)==len)
           {
            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());
        }
      //--- 使用后关闭套接字
      SocketClose(socket);
     }
   else
      Print("Failed to create a socket, error ",GetLastError());
  }
//+------------------------------------------------------------------+
//| 读取服务器响应                                                     |
//+------------------------------------------------------------------+
bool HTTPRecv(int socket,uint timeout_ms)
  {
//--- 在有数据时从套接字读取数据,但不能超过超时时间
   char   rsp[];
   string result;
   ulong  timeout_check=GetTickCount64()+timeout_ms;
 
   do
     {
      uint len=SocketIsReadable(socket);
 
      if(len)
        {
         //--- 读取并解析安全TLS连接的数据
         int rsp_len=SocketTlsRead(socket,rsp,len);
 
         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);
  }

另见

SocketTimeoutsMathSwapStringToCharArray