problem in receiving data via SocketRead

 
hi i am trying to receive a message from python via Socket

the message is "hi from python" , but in mt5 terminal i only receive character "h"

please take a look at my mql5 codes and the images i attached

please help me thanks

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
   do
     {
      uint len=SocketIsReadable(socket);
      if(len)
        {
         int rsp_len;
         //--- various reading commands depending on whether the connection is secure or not
         if(ExtTLS)
            rsp_len=SocketTlsRead(socket,rsp,len);
         else
            rsp_len=SocketRead(socket,rsp,len,timeout);
         //--- analyze the response
         if(rsp_len>0)
           {
            result+=CharArrayToString(rsp,0,rsp_len);
            //--- print only the response header
              {
               Print(result);
               return(true);
              }
           }
        }
     }
   while(GetTickCount()<timeout_check && !IsStopped());
   return(false);
  }
//+------------------------------------------------------------------+
Files:
 
Reza Babagoli:
hi i am trying to receive a message from python via Socket

the message is "hi from python" , but in mt5 terminal i only receive character "h"

please take a look at my mql5 codes and the images i attached

Try to use CP_UTF8 in CharArrayToString. In the real world (with lengthy messages) you need to wait somehow when entire message is received before decoding it.

Documentation on MQL5: Conversion Functions / CharArrayToString
Documentation on MQL5: Conversion Functions / CharArrayToString
  • www.mql5.com
It copies and converts part of array of uchar type into a returned string. Parameters array[] [in]  Array of uchar type. start=0 [in]  ...
 
Stanislav Korotky #:

Try to use CP_UTF8 in CharArrayToString. In the real world (with lengthy messages) you need to wait somehow when entire message is received before decoding it.

i added CP_UTF8 but not worked and it printed "h" again

and i used timeout and sleep but nothing changed
 
Stanislav Korotky #:

Try to use CP_UTF8 in CharArrayToString. In the real world (with lengthy messages) you need to wait somehow when entire message is received before decoding it.

i moved the print function after the " do while " and it worked
void socketreceive(int sock,int timeout)
  {
   char rsp[];
   string result="";
   uint len;
   uint timeout_check=GetTickCount()+timeout;
   do
     {
      len=SocketIsReadable(sock);

      if(len)
        {
         int rsp_len;
         rsp_len=SocketRead(sock,rsp,len,timeout);
         if(rsp_len>0)
           {
            result+=CharArrayToString(rsp,0,rsp_len);
           }
        }
     }
   while((GetTickCount()<timeout_check) && !IsStopped());
//-----------------------------------------------------------------------moved here--------------------------------
   Print(result);
  }