0

I'm trying to connect client with server. With local address (127.0.0.1) everything works perfectly fine, but when i try to use my remote address, it doesn't work. I search this thing all over the internet but i couldn't find anything.

Server side:

bool start_server()
{
    int i = 1;
    if(WSAStartup(MAKEWORD(2,0),&WsaDat) != 0)
    {
         cout << "WSA error!" << endl;
         system("pause");
         return FALSE;
    }
    if ( LOBYTE( WsaDat.wVersion ) != 2 || HIBYTE( WsaDat.wVersion ) != 0 )
    {
      cout << "Bad version of winsocket" << endl;
      system("pause");
      WSACleanup();
      return 0;
    }



    serwer = socket(AF_INET, SOCK_STREAM, 0);
    if(serwer == INVALID_SOCKET)
    {
        cout << "Can't create socketa!" << endl;
        system("pause");
        WSACleanup();
        return 0;
    }
    int port;
    cout << "Port input " << endl;
    cin >> port;                                             // 13056
    SOCKADDR_IN SockAddr;
    SockAddr.sin_port =  htons(port);
    SockAddr.sin_family = AF_INET;
    SockAddr.sin_addr.s_addr = INADDR_ANY;
    setsockopt( serwer, SOL_SOCKET, SO_REUSEADDR, ( char * ) &i, sizeof ( i ) );
    if(bind(serwer,(SOCKADDR *)(&SockAddr), sizeof(SockAddr)) == SOCKET_ERROR)
    {
        cout << "Couldn't bind socket" << endl;
        system("pause");
        WSACleanup();
        return 0;
    }
    for(int i = 0; i < MAX_CLIENTS; i++)
    client[i].connected = FALSE;
    if(listen(serwer,SOMAXCONN)==SOCKET_ERROR)
    {
        cout << "Listening error!" << endl;
        system("pause");
        WSACleanup();
        return 0;
    }
    cout << "Serwer created!" << endl;
    unsigned long b = 0;
    ioctlsocket ( serwer, FIONBIO, &b );
    return 1;
}

After this i have accept function on which program is blocking:

accept(serwer,(SOCKADDR *)(&current_client->address),&current_client->address_length);

Client side:

bool start_client()
{
    WSADATA WsaDat;

    if(WSAStartup(MAKEWORD(2,0),&WsaDat) != 0)
        cout << "WSA error!\n";
    if ( LOBYTE( WsaDat.wVersion ) != 2 || HIBYTE( WsaDat.wVersion ) != 0 )
    {
        cout << "Wrong version of winsocket\n";
        WSACleanup();
                    return false;
    }

    SOCKET Klient;

    Klient = socket(AF_INET, SOCK_STREAM, 0);
    if(Klient == INVALID_SOCKET)
            {
        cout << "Can't create socket socketa!\n";
        WSACleanup();
                    return false;
    }
    SOCKADDR_IN SockAddr;
    SockAddr.sin_port = htons(PORT);                        // 13056
    SockAddr.sin_family = AF_INET;
    SockAddr.sin_addr.S_un.S_addr = inet_addr("*********");      // my remote address
    while(connect(Klient, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) != 0) 
    {
        cout << "Trying to connect " << WSAGetLastError() << "\n";
        Sleep(500);
    }
    cout << "Connected!\n";
}

I have added both client and server applications to firewall rules and i have also tried to connect them on couple ports. Server side is listening for sure (here on different port):

enter image description here

4

1 回答 1

1

您的代码没有任何问题。

并非所有路由器和网络地址转换器 (NAT) 都支持发夹,这是从 NAT 后面的专用网络内连接到您的公共 IP 的能力。

如果您的 NAT/路由器确实支持这一点,那么您可能需要在 NAT/路由上配置端口转发规则,以将端口 14354(您的服务器侦听端口)上的所有传入 TCP 流量映射到 192.168.0.10 的内部 IP 地址. 从路由器的角度考虑这一点,如果它看到公共 IP 上的传入连接请求 - 它需要知道私有网络上的哪个主机将该连接转发到。

于 2013-05-03T09:27:21.943 回答