Android - Connect()/ Socket(ip,port)没有任何关系

时间:2016-03-09 14:54:51

标签: java android c++ sockets

编辑:我已经设法从python客户端连接服务器,这意味着ANDROID连接存在问题。我一直试图在过去2小时内弄明白 - connect()函数和socket()函数,什么都不做!我吓坏了。

问题:, 我正在开发一个Android应用程序,它基本上与用我的笔记本电脑在c ++上运行的服务器通信(我的笔记本电脑操作系统是Windows 8)。

我的客户端只有在我连接到家庭网络时才连接到服务器。当我连接到其他网络时,我的客户端抛出异常:

**用电缆连接的服务器计算机**

java.net.SocketTimeoutException: failed to connect to /192.168.43.60 (port 5898) after 3000ms

Android代码:

Thread th = new Thread(new Runnable() {
public void run() {

       Socket client_socket = new Socket(); // Creates a socket and binds it to available port.
       Log.d("INFO", "Created Socket");

       for (int retries = 0; retries < 3; retries++) {
          try {
              client_socket.connect(new InetSocketAddress("127.0.0.1", 5898), 3000); // Connect to the server.
              break;
          } catch (java.net.SocketTimeoutException e) {
              Log.d("INFO", "Connect() failed with an error.");
              e.printStackTrace();
              continue;
          }
        }
   }
   Log.d("INFO", "Connecting to server on " + ProtCodes.serverIP + ":" + ProtCodes.serverPORT);
};

th.start();
th.join();

服务器端:

using namespace std;
int main(int argc, char* argv[])
{
    Server* server;
    SOCKADDR_IN sockAddrIn;
    CLIENT_INFO client_info;
    int iResult = 0, number_of_connected_clients = 0, clientLen = sizeof(sockAddrIn);
    std::list<SOCKADDR_IN>* connected_clients; //list of connected clients SOCKADDRS structs (for ip & port)
    t1 = clock();
    try { //validates the PORT argument
        if (atoi(argv[0]) > 1024 && atoi(argv[0]) < 65535)
            server = new Server(atoi(argv[0]));
        else
            server = new Server(DEFAULT_PORT);
    }catch (...){
        server = new Server(5898);
    }

    iResult = listen(server->getSocket(), SOMAXCONN);
    if (iResult == SOCKET_ERROR) {
        printf("listen failed with error: %d\n", WSAGetLastError());
        closesocket(server->getSocket());
        WSACleanup();
        return TRUE;
    }

    cout << endl << "@Server_log: Connected!" << endl;
    cout << "@Server_log: Launching MySQL server, version: " << string(mysql_get_client_info()) << endl;
    cout << "@Server_log: Server is up running on " << get_ipv4_address() << ":" << ntohs(server->get_server_info().sin_port) << endl;
    cout << "@Server_log: Waiting for requests..." << endl;
    cout << "*******************************************" << endl << endl;
    std::thread server_command(&handle_server_commands, server); //thread for     server commands
    server_command.detach();

    /* Accept a client socket */
    while (number_of_connected_clients < MAX_CONNECTIONS)
    {
        memset(&sockAddrIn, 0, sizeof(sockAddrIn));
        clientLen = sizeof(sockAddrIn);
        ClientSocket = accept(server->getSocket(), (struct sockaddrn*)&sockAddrIn, &clientLen);
        if (ClientSocket == INVALID_SOCKET)
        {
            printf("accept failed with error: %d\n", WSAGetLastError());
            continue;
        }

        /* Print connected client info */
        client_info.ip = inet_ntoa(sockAddrIn.sin_addr);
        client_info.port = ntohs(sockAddrIn.sin_port);
        cout << endl;
        get_current_time();
        cout << "Client connected on " << client_info.ip << " : " << client_info.port << " !" << endl;
        cout << endl << ">> ";
        connected_clients = server->get_connected_clients();
        connected_clients->push_back(sockAddrIn); //push client SOCKADDR     struct into server's clients list
        std::thread a(&(handle_user_requests), ClientSocket, server, client_info); //thread for clients requests handle
        a.detach();
    }

    closesocket(ClientSocket);
    closesocket(server->getSocket());
    WSACleanup();
}


非常感谢!
伊詹

0 个答案:

没有答案