I’m developing an MQL5 Expert Advisor that should connect to a local TCP server (127.0.0.1:5001). The server is running and responds correctly to Telnet. In MetaTrader, I added 127.0.0.1:5001 in Options → Expert Advisors → Allow WebRequest for listed URLs. Firewall is disabled, and the port is free.
My MQL5 code is:
int socket;
int OnInit()
{
socket = SocketCreate();
if(socket == INVALID_HANDLE)
{
Print("SocketCreate failed");
return(INIT_FAILED);
}
if(!SocketConnect(socket, "127.0.0.1", 5001))
{
Print("SocketConnect failed with error: ", GetLastError());
return(INIT_FAILED);
}
Print("Connected successfully");
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
SocketClose(socket);
}
Tests I have done:
- Verified the server is listening and reachable with Telnet.
- Confirmed the port (5001) is open and available.
- Disabled firewall completely.
- Used numeric IP 127.0.0.1.
- Added 127.0.0.1:5001 to Metatrader Expert Advisors options.
- Tried creating a TCP server using Netcat (nc -l 5001), but MQL5 still fails with error 4014.
Question Has anyone implemented a minimal TCP server for MQL5 testing or has a working configuration example? I want to understand whether the issue is on the MQL5 side or the server side. Any example code or demo would be greatly appreciated.
localhostinstead of127.0.0.1:5001in the list of allowed web urls