Network Functions 4014 error

 

I'm new to working with network function, I'm trying to create a simple client server connection between a python app and MQL5  following this tutorial https://www.mql5.com/en/articles/5691. I read the docs of networks functions again and i saw that you get this error if you call the network function from anything other than a script or EA. But this does not make since because i'm using an ea, here is a sample, code fails at: with 4014 error

int socket=SocketCreate(SOCKET_DEFAULT);
//+------------------------------------------------------------------+
//|                                                 SocketClient.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
     int socket=SocketCreate(SOCKET_DEFAULT);
     if(socket!=INVALID_HANDLE) 
     {
       if(SocketConnect(socket,"localhost",65535,1000))
       {
          Print("Connected to "," localhost",":",65535);
        
          MqlRates PriceInfo[];
          ArraySetAsSeries(PriceInfo, true);
          int Data = CopyRates(Symbol(), Period(), 0, 3, PriceInfo);
               
          string tosend;
          for(int i=0;i<ArraySize(PriceInfo);i++)
          {tosend+=(string)PriceInfo[i].close+" ";}
                
          string received = socksend(socket, tosend) ? socketreceive(socket, 10) : ""; 
       }
       else
       {  
          Print("Connection ","localhost",":",65535," error ",GetLastError());
          SocketClose(socket); 
       }
     }
     else
     {
       Print("Socket creation error ",GetLastError()); 
     }
   
 }
//+------------------------------------------------------------------+

// Send to server
bool socksend(int sock,string request) 
  {
   char req[];
   // Byte array conversion
   int  len=StringToCharArray(request,req)-1;
   if(len<0) return(false);
   return(SocketSend(sock,req,len)==len); 
  }
  
// Recieve fro client
string 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());
   return result;
  }

MetaTrader 5 and Python integration: receiving and sending data
MetaTrader 5 and Python integration: receiving and sending data
  • www.mql5.com
A network socket is the endpoint of interprocess communication over a computer network. The MQL5 Standard Library includes a group of Socket functions, which provide a low-level interface for working on the Internet. This is a common interface for different programming languages, as it uses system calls at the operating system level. Data...
 
As 4014 means: "Function is not allowed for call" what is the OS: Linux, OS?
 
I am using windows 10, I have 2 laptops and i tried on both and got the same error 
 
Thembekile Thembekile:
I am using windows 10, I have 2 laptops and i tried on both and got the same error 
did you run the socket function in strategy tester? i think it's not possible from strategy tester
 
According to SocketConnect() documentation, 
Connection address should be added to the list of allowed ones on the client terminal side (Tools \ Options \ Expert Advisors).

So, in MetaTrader Tools\Options\Expert Advisors, add 127.0.0.1 to the Allow WebRequest for listed URL.

Documentation on MQL5: Network Functions / SocketConnect
Documentation on MQL5: Network Functions / SocketConnect
  • www.mql5.com
//|                                                SocketExample.mq5 | //|                        Copyright 2018, MetaQuotes Software Corp. | //|                                             https://www.mql5.com | "Add Address to the list of allowed ones in the terminal settings to let the example work...
 

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".

You need only add socket server address into "Allow WebRequest for listed URL" (Tool-> Option -> Expert Advisors)

Documentation on MQL5: Checkup / GetLastError
Documentation on MQL5: Checkup / GetLastError
  • www.mql5.com
GetLastError - Checkup - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
changes the "localhost" to 127.0.0.1 ( or your IP SERVER, pls don't use localhost as address)
 
Why not use "localhost"?

Please elaborate your statement.