Problem reading data from socket via SocketRead

 

I'm consistently receiving error 5273 when invoking SocketRead MQL5 API.

I connect to the server from an MT terminal and would like to check periodically if some data have arrived from the server. Here is my code: 

    if(client==INVALID_HANDLE) return false;
    uchar rbuf[512];
    uint rlen=512;
    int res=0;
    res=SocketRead(client,rbuf,rlen,1);
    // SocketIsReadable(client) is equal to 1 at this point
    // res is equal to -1 at this point
    // GetLastError() is equal to 5273 at this point

It works just fine when I connect to the very same server and send/receive data if I replace MT5 built-in socket with winsocket. However using winsocket will not allow me to publish the EA on market which is not an option for me.

Sending data works just fine and server receives the data correctly. I also checked that the server sends packets to the connected MT terminal correctly.

Any ideas?

 
the same issue
 
did you find a solution?
 

Dunno if this is still relevant for anyone after so many years but I was also just struggeling with this... For me it worked when I used SocketIsReadable(..) first and then used the returned number of bytes as max size for the buffer in SocketRead(..)

Sample Code:

   static int socket = INVALID_HANDLE;
   if(socket == INVALID_HANDLE) socket = SocketCreate();
   if(!SocketIsConnected(socket) && SocketConnect(socket,SERVER,PORT,1000)){
      Print(__FUNCTION__," > Connected to server socket (",SERVER,":",PORT,")");
   }
   if(SocketIsConnected(socket)){
      int len = 0;
      while((len = SocketIsReadable(socket)) > 0){
         uchar buffer[];
         int res = SocketRead(socket,buffer,len,1000);
         if(res > -1){
            string txt = CharArrayToString(buffer,0,WHOLE_ARRAY,CP_UTF8);
            static string bufferStr;
            bufferStr += CharArrayToString(buffer);
         }
      }
   }
            

Do not ask me why it works. I have no clue but maybe it helps someone.