【问题标题】:Socket.connect() connects but no serverSocket listeningSocket.connect() 连接但没有 serverSocket 监听
【发布时间】:2015-05-20 12:40:20
【问题描述】:

我尝试 TCP 打孔的另一个障碍。 我的服务器位于 NAT 后面,我正在循环调用 socket.connect() 到手机的 NAT 后公共 IP。

在我的手机上,我按下一个按钮来运行 socket.connect() 到服务器的 NAT'd 公共 IP。手机说连接到服务器的公网IP和端口。但是服务器似乎没有意识到这一点。

如果我在服务器上运行serverSocket而不是socket.connect(),手机连接不上。

这让我很困惑。为什么没有serverSocket监听手机调用socket.connect()会成功?

任何帮助将不胜感激。

这是手机应用程序上的代码。

try {
        Log.d("stop", "line 64");

        Log.d("stop", "line");
        Log.d("localHost: ", myIPAddress.getHostAddress());
        Socket clientSocket = new Socket();
        Log.d("stop", "line 70");
        clientSocket.setReuseAddress(true);
        clientSocket.bind(new InetSocketAddress(myIPAddress.getHostAddress(), myPort));
        Log.d("stop", "line 72");
        clientSocket.connect(new InetSocketAddress(serverIPAddress.getHostAddress(), serverPort), 15000);
        Log.d("connected", clientSocket.toString());



        //BufferedReader inFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 
        //receivedMessage = inFromClient.readLine();
        Log.d("stop", "line 78");


        //globalSocket app = (globalSocket)getApplication();
        //app.setSocket(clientSocket);
        commsock = clientSocket;

    } catch (IOException e) {

        //Toast.makeText(this, "socket caughyt", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
        error1 = "no Server";
        Log.d("error", error1 + e.getMessage());

    }

这是我的服务器应用程序代码的整个循环。

    while(true){

        Thread.sleep(5000);
        try {

                System.out.println("105");
                mobileSocket2 = new Socket();
                mobileSocket2.setReuseAddress(true);
                System.out.println("109");
                //mobileSocket.setReusePort(true);
                //mobileSocket2.setSoTimeout(50);

                mobileSocket2.bind(new InetSocketAddress(myIPAddress.getHostAddress(), myPort));
                System.out.println("bound");

                }
                catch (Exception e) {

                    System.out.println("caught 104: " + e.toString());
                }


        try{

                System.out.println("124");
                System.out.println("connection made 29: " + mobileSocket2);
                mobileSocket2.connect(new InetSocketAddress(mobileAddress.getHostAddress(), mobilePort),5000);
                System.out.println("connection made 239: " + mobileSocket2);



            }
            catch(Exception e){
                System.out.println("exception 2 caught " + e.toString());
                mobileSocket2.close();


            }



    //}//end of while   
} // end of main

【问题讨论】:

    标签: java sockets tcp serversocket


    【解决方案1】:

    为什么不在服务器代码中使用 accept 方法: http://docs.oracle.com/javase/7/docs/api/java/net/ServerSocket.html#accept%28%29

    【讨论】:

    • 我在 Windows 中使用了资源监视器(perfmon),并且从手机连接到笔记本电脑。 TCP 连接在 java.exe 下列出。但由于某种原因, serverSocket.accept 只是说“接受超时”。不知道为什么。我也把它放在循环中,但没有接受任何东西。
    • 超时问题可能有两个原因:防火墙阻止连接或您将手机连接到错误的端口。我认为问题来自防火墙。
    • 但如果它是防火墙,为什么资源监视器说 tcp 连接已经建立?或者防火墙可以在建立tcp连接后阻止它吗?
    • 我可以建议的是,当您使用accept方法时,您将作为参数返回的套接字放在线程类中,而不是在线程类的run方法中实现您需要的代码已创建。
    • 因为在接受方法中根本不返回套接字。它会导致它超时的异常(因为由于某种原因没有什么可听的)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 2012-10-13
    • 1970-01-01
    • 2019-09-27
    • 1970-01-01
    • 2012-08-12
    相关资源
    最近更新 更多