下载MetaTrader 5

Mt4 socket调用问题,求解答

要添加评论,请登录注册
hxh001
18
hxh001 2015.03.19 02:52 
我在MT4里用socket跟外部程序通信,失败:错误信息如下2015.03.19 09:48:59.000    Script_NET USDJPY,H4: Net Bridge Error: connect() failed: 10051,socket代码是从https://www.mql5.com/en/code/9772下载,我的代码如下:
int Buffer[256];
    int retval;
    int loopcount=0;
    int addr[1];
    int socket_type;
    int server[sockaddr_in];
    int hp;
    int wsaData[WSADATA];
   
    //closesocket(conn_socket);

    if(handlerName == ""  || methodName == "" || Bridge_NET_IsActive == false)
    {
        return("");
    }

    if (Bridge_NET_Protocol=="TCP")
    {
        socket_type = SOCK_STREAM;
    }
    else if (Bridge_NET_Protocol=="UDP")
    {
        socket_type = SOCK_DGRAM;
    }

    retval = WSAStartup(0x202, wsaData);
    if (retval != 0)
    {
        Print("Net Bridge Error: WSAStartup() failed with error "+ retval);
        return("");
    }
   
    if (isalpha(StringGetChar(Bridge_NET_Server,0)))
    {
        Print("Net Bridge Error: IP address should be given in numeric form in this version.");
        return("");
    }
   
    addr[0] = inet_addr(Bridge_NET_Server);
    hp = addr[0];
   
    if (hp == 0 )
    {
        Print("Net Bridge Error: Cannot resolve address \""+Bridge_NET_Server+"\": Error :"+WSAGetLastError());
        return("");
    }
/*
    Print("server:",server[0],server[1],server[2],server[3],",sin_addr:",sin_addr,",addr[0]:",addr[0]);
    int2struct(server,sin_addr,addr[0]);
    int2struct(server,sin_family,AF_INET);
    int2struct(server,sin_port,htons(Bridge_NET_Port));
    */
        int2struct(server,sin_family,AF_INET);
    Print(AF_INET+" family:"+server[0]+" "+server[1]+" "+server[2]+" "+server[3]+" f:"+sin_family);
    if (Bridge_NET_Server=="")
       int2struct(server,sin_addr,INADDR_ANY);
    else 
       int2struct(server,sin_addr,addr[0]);
    Print(addr[0]+" addr:"+server[0]+" "+server[1]+" "+server[2]+" "+server[3]+" f:"+sin_addr);
    int2struct(server,sin_port,htons(Bridge_NET_Port));
    Print(htons(Bridge_NET_Port)+" port:"+server[0]+" "+server[1]+" "+server[2]+" "+server[3]+" f:"+sin_port);
   
   //Print("server:",server[0],server[1],server[2],server[3],server[4],",sin_addr:",sin_addr,",addr[0]:",addr[0]);
    conn_socket = socket(AF_INET, socket_type, 0);
    if (conn_socket <0 )
    {
        Print("Net Bridge Error: Error Opening socket: Error "+ WSAGetLastError());
        return("");
    }
   Print("ArraySize(server):",ArraySize(server));
   Print("ArraySize(server)<<2:",ArraySize(server)<<2);
    retval=connect(conn_socket, server, ArraySize(server)<<2);
   
    if (retval == SOCKET_ERROR)
    {
        Print("Net Bridge Error: connect() failed: ", WSAGetLastError());
        return("");
    }

    ArrayInitialize(Buffer, 0);
    str2struct(Buffer,ArraySize(Buffer)<<18,Ext_Operator_MagicNumber+"|"+handlerName+"|"+methodName+"|"+parameters+"|");
    retval = send(conn_socket, Buffer, ArraySize(Buffer)<<2, 0);
   
    if (retval == SOCKET_ERROR)
    {
        Print("Net Bridge Error: send() failed: error ", WSAGetLastError());
        return("");
    }
    ArrayInitialize(Buffer, 0);
    retval = recv(conn_socket, Buffer, ArraySize(Buffer)<<2, 0);
    if (retval == SOCKET_ERROR)
    {
        Print("Net Bridge Error: recv() failed: error ", WSAGetLastError());
        return("");
    }
   
    if (retval == 0)
    {
        Print("Net Bridge Error: Server closed connection.");
        return("");
    }
TCP socket
TCP socket
  • 投票: 1
  • 2010.07.05
  • seth
  • www.mql5.com
placed utility functions from ryaz's server socket into socket.mqh, makes it really easy to use. comes with similar server example.
hxh001
18
hxh001 2015.03.20 03:11  
请求高手回答!
Jian Chen
版主
3646
Jian Chen 2015.03.28 05:14  

查了下错误代码:

10051—WSAENETUNREACH 

网络不可抵达。试图采取一项操作时,却发现目标网络不可抵达(不可访问)。这意味着 本地主机不知道如何抵达一个远程主机。换言之,目前没有已知的路由可抵达那个目标主机。 

 

是不是服务器地址有问题? 

要添加评论,请登录注册