Read data from secure TLS connection.

int  SocketTlsRead(
   int           socket,               // socket
   uchar&        buffer[],             // buffer for reading data from socket
   uint          buffer_maxlen         // number of bytes to read



[in]  Socket handle returned by the SocketCreate function. When an incorrect handle is passed to _LastError, the error 5270 (ERR_NETSOCKET_INVALIDHANDLE) is activated.


[out]  Reference to the uchar type array the data is read in. Dynamic array size is increased by the number of read bytes. The array size cannot exceed INT_MAX (2147483647).


[in]  Number of bytes to read to the buffer[] array. Data not fitting into the array remain in the socket. They can be received by the next SocketTLSRead call. buffer_maxlen cannot exceed INT_MAX (2147483647).

Return Value

If successful, return the number of read bytes. In case of an error, -1 is returned.


If an error occurs on a system socket when executing the function, connection established via SocketConnect is discontinued.

The function is executed till it receives the specified amount of data or the timeout is reached (SocketTimeouts).

In case of a data reading error, the error 5273 (ERR_NETSOCKET_IO_ERROR) is written in _LastError.

The function can be called only from Expert Advisors and scripts, as they run in their own execution threads. If calling from an indicator, GetLastError() returns the error 4014 – "Function is not allowed for call".


//|                                                SocketExample.mq5 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                    |
#property copyright   "Copyright 2018, MetaQuotes Software Corp."
#property link        ""
#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="";
input int    Port   =80;
bool         ExtTLS =false;
//| Send command to the server                                       |
bool HTTPSend(int socket,string request)
   char req[];
   int  len=StringToCharArray(request,req)-1;
//--- if secure TLS connection is used via the port 443
//--- if standard TCP connection is used
//| Read server response                                             |
bool HTTPRecv(int socket,uint timeout)
   char   rsp[];
   string result;
   uint   timeout_check=GetTickCount()+timeout;
//--- read data from sockets till they are still present but not longer than timeout
      uint len=SocketIsReadable(socket);
         int rsp_len;
         //--- various reading commands depending on whether the connection is secure or not
         //--- analyze the response
            //--- print only the response header
            int header_end=StringFind(result,"\r\n\r\n");
               Print("HTTP answer header received:");
   while(GetTickCount()<timeout_check && !IsStopped());
//| Script program start function                                    |
void OnStart()
   int socket=SocketCreate();
//--- check the handle
      //--- connect if all is well
         Print("Established connection to ",Address,":",Port);
         string   subject,issuer,serial,thumbprint;
         datetime expiration;
         //--- if connection is secured by the certificate, display its data
            Print("TLS certificate:");
            Print("   Owner:  ",subject);
            Print("   Issuer:  ",issuer);
            Print("   Number:     ",serial);
            Print("   Print: ",thumbprint);
            Print("   Expiration: ",expiration);
         //--- send GET request to the server
         if(HTTPSend(socket,"GET / HTTP/1.1\r\nHost:\r\n\r\n"))
            Print("GET request sent");
            //--- read the response
               Print("Failed to get a response, error ",GetLastError());
            Print("Failed to send GET request, error ",GetLastError());
         Print("Connection to ",Address,":",Port," failed, error ",GetLastError());
      //--- close a socket after using
      Print("Failed to create a socket, error ",GetLastError());

See also

SocketTimeouts, MathSwap