Getting socket connection error 5272 in EA on updating the timeframe of an MT5 chart

 

Hi

I am building a custom EA for MT5

When it is attached to a chart, the Socket Connection is made in OnInit() function & the connection is closed in OnDeinit() function while detaching the EA.

My code is:

int socket = -2; // Socket Variable 
bool soc=false;

int OnInit()
{
 
      if(socket == -2){
     
         socket = SocketCreate();
         if(socket!=INVALID_HANDLE) {
            while(soc==false){
               soc=SocketConnect(socket,"localhost",9090,1000);
            }
            if(SocketConnect(socket,"localhost",9090,1000)) {
               Print("Connected to "," localhost",":",9090);
               // some code    
                
            }
            else{
               socket = -2; 
               Print("Error in connecting to ","localhost",":",9090,"  Error  :  ",GetLastError()); 
            }    
         }
         else{
            socket = -2;
            Print("Socket creation error ",GetLastError()); 
         }
      }
      else{
         Print("Socket Is Busy");
      
      }
   
         
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
   SocketClose(socket);
   ObjectsDeleteAll(ChartID(),-1,-1);
}



I want that the new socket connection should get created if I update the timeframe of the chart while the EA is still attached to it.

However, if I update the timeframe of the chart while the EA is still attached to it, then it displays "Error in connecting to localhost:9090  Error  :  5272" in the journal.

I am clueless about how to re-connect to the socket

Kindly help

Regards

 
testsid:

Hi

I am building a custom EA for MT5

When it is attached to a chart, the Socket Connection is made in OnInit() function & the connection is closed in OnDeinit() function while detaching the EA.

My code is:



I want that the new socket connection should get created if I update the timeframe of the chart while the EA is still attached to it.

However, if I update the timeframe of the chart while the EA is still attached to it, then it displays "Error in connecting to localhost:9090  Error  :  5272" in the journal.

I am clueless about how to re-connect to the socket

Kindly help

Regards

 if (reason!=REASON_CHARTCHANGE)
 

I replaced

void OnDeinit(const int reason)
{
//---
   SocketClose(socket);
   ObjectsDeleteAll(ChartID(),-1,-1);
}

by

void OnDeinit(const int reason)
{
//---
   if (reason!=REASON_CHARTCHANGE)
        SocketClose(socket);
   ObjectsDeleteAll(ChartID(),-1,-1);
}

However, the issue is still the same

 

I had the same error message. Below is what the error message means. 


ERR_NETSOCKET_CANNOT_CONNECT

5272

Failed to connect to remote host


I resolve the issue and it turned out to be a couple of things. 

Firstly under tools > options > Expert advisor I needed to white list the doamin address. By clicking allow domin and then adding it. That still didn't resolve the issue. Next I needed to be sure I was using the correct port. I was using HTTPS so the port was 443. If I was using HTTP the ports probably 80. Then the connection worked and I got my next Bug lol. But this issue was resolved. 


Hope this helps someone in the future and happy trading.