//+------------------------------------------------------------------+ //| select_server.mq4 | //| nuric | //| https://github.com/nuric/mt4-tcp | //+------------------------------------------------------------------+ #property copyright "nuric" #property link "https://github.com/nuric/mt4-tcp" #property version "1.00" #property description "Multi client select server that pushes tick data." #property strict #include "socket.mqh" input ushort server_port=7777; input string server_ip="0.0.0.0"; int server_socket=INVALID_SOCKET; fd_set sockets; timeval timeout={2,0}; // 2 seconds //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- if(!IsDllsAllowed()) { Print("Require DLL imports."); return(INIT_FAILED); } // Create the server socket server_socket=sock_open(server_ip,server_port); if(server_socket==INVALID_SOCKET) return(INIT_FAILED); fd_zero(sockets); fd_add(server_socket,sockets); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- sock_closefds(sockets); sock_cleanup(); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- fd_set readsockets=sockets; // nfds parameter ignored, just passing 1 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms740141(v=vs.85).aspx if(select(1,readsockets,NULL,NULL,timeout)==SOCKET_ERROR) { Print("Server: select error ",WSAGetLastError()); return; } // Process incoming requests for(uint i=0;i