Mql5 socket for stream data terminal

 

Hi guys i have created this script

#property copyright   "Copyright 2000-2024, MetaQuotes Ltd."
#property link        "https://www.mql5.com"
#property version     "1.01"
#property script_show_inputs

input string Address = "127.0.0.1";
input int Port = 8080;
bool ExtTLS = false;

bool SendTCPData(int socket, string message) {
   char data[];
   int len = StringToCharArray(message, data) - 1;
   return (ExtTLS ? SocketTlsSend(socket, data, len) : SocketSend(socket, data, len)) == len;
}

string GetOpenTradesData() {
   string trade_data = "";
   for (int i = 0; i < PositionsTotal(); i++) {
      ulong ticket = PositionGetTicket(i);
      if (PositionSelectByTicket(ticket)) {
         string symbol = PositionGetString(POSITION_SYMBOL);
         double size = PositionGetDouble(POSITION_VOLUME);
         double open_price = PositionGetDouble(POSITION_PRICE_OPEN);
         double sl = PositionGetDouble(POSITION_SL);
         double tp = PositionGetDouble(POSITION_TP);
         double swap = PositionGetDouble(POSITION_SWAP);
         double commission = PositionGetDouble(POSITION_COMMISSION);
         datetime open_time = PositionGetInteger(POSITION_TIME);
         string comment = PositionGetString(POSITION_COMMENT);
         int type = PositionGetInteger(POSITION_TYPE);

         trade_data += StringFormat("Symbol:%s, OpenTime:%s, Size:%.2f, Comment:%s, Swap:%.2f, Commission:%.2f, OpenPrice:%.5f, SL:%.5f, TP:%.5f, Type:%d, Ticket:%d\n",
                        symbol, TimeToString(open_time, TIME_DATE | TIME_MINUTES), size, comment, swap, commission, open_price, sl, tp, type, ticket);
      }
   }
   return trade_data;
}

void OnStart() {
   int socket = INVALID_HANDLE;
   int attempt = 0;
   
   while (!IsStopped() && attempt < 5) {  // 5 tentativi di connessione
      socket = SocketCreate();
      if (socket == INVALID_HANDLE) {
         Print("Creazione socket fallita, errore ", GetLastError());
         return;
      }

      if (SocketConnect(socket, Address, Port, 1000)) {
         Print("Connessione stabilita a ", Address, ":", Port);
         string trade_data = GetOpenTradesData();
         if (trade_data == "") {
            trade_data = "ciao";  // Se non ci sono dati, invia "ciao"
         }

         if (SendTCPData(socket, trade_data)) {
            Print("Dati inviati con successo.");
         } else {
            Print("Invio dati fallito, errore ", GetLastError());
         }
         SocketClose(socket);
         break;
      } else {
         Print("Tentativo di connessione fallito a ", Address, ":", Port, ", errore ", GetLastError());
         SocketClose(socket);
         attempt++;
         Sleep(2000);  // Aspetta 2 secondi prima di tentare di nuovo
      }
   }
}

i want  send continuously a data that i have  in terminal , in another  app (for the moment only in tenlnet ) i add  127.0.0.1:8080 in expertadvisor ---> allow web request but i dont know  why  when i try to connect with telnet or other app return me "Connecting To 127.0.0.1...Could not open connection to the host, on port 8080: Connect failed" in MT5 log  i have this 2024.11.08 11:57:13.670    TCP_PASS_DATA (EURUSD,H1)    Tentativo di connessione fallito a 127.0.0.1:8080, errore 4014
but firewall is  all open , anyone  have  some idea?  thanks alot

 
faustf:

Hi guys i have created this script

i want  send continuously a data that i have  in terminal , in another  app (for the moment only in tenlnet ) i add  127.0.0.1:8080 in expertadvisor ---> allow web request but i dont know  why  when i try to connect with telnet or other app return me "Connecting To 127.0.0.1...Could not open connection to the host, on port 8080: Connect failed" in MT5 log  i have this 2024.11.08 11:57:13.670    TCP_PASS_DATA (EURUSD,H1)    Tentativo di connessione fallito a 127.0.0.1:8080, errore 4014
but firewall is  all open , anyone  have  some idea?  thanks alot

Socket implementation on MQL side is a client connection. You cannot connect with a telnet client to a client socket....

You need an intermediary server.

Error 4014: Function not allowed
 

but is possible implement or i must create some dll  for do it ?  , i want  tell : exist in mql5 a possibility to do a server socket?

 
faustf #:

but is possible implement or i must create some dll  for do it ?  , i want  tell : exist in mql5 a possibility to do a server socket?

Not without using DLL calls.

MQL only supports client side networking.

Possibly, you could use an sqlite database instead.
 
Dominik Egert #:
Not without using DLL calls.

MQL only supports client side networking.

Possibly, you could use an sqlite database instead.

but in mt4 i just did do i used  a library , now  i try to convert for mt5 script but not work i attach it , but anyway it's the client that I want