Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Socket.connect()连接,但没有服务器套接字侦听_Java_Sockets_Tcp_Serversocket - Fatal编程技术网

Java Socket.connect()连接,但没有服务器套接字侦听

Java Socket.connect()连接,但没有服务器套接字侦听,java,sockets,tcp,serversocket,Java,Sockets,Tcp,Serversocket,我尝试TCP holepunching的另一个障碍。 我的服务器位于NAT后面,我正在循环调用socket.connect()到移动电话的NAT公共IP 在我的手机上,我按下一个按钮来运行socket.connect()到服务器的NAT公共IP。手机说它连接到服务器的公共IP和端口。但服务器似乎没有意识到这一点 如果在服务器上运行serverSocket而不是socket.connect(),则手机无法连接 这把我弄糊涂了。如果没有serverSocket侦听,为什么手机对socket.conn

我尝试TCP holepunching的另一个障碍。 我的服务器位于NAT后面,我正在循环调用socket.connect()到移动电话的NAT公共IP

在我的手机上,我按下一个按钮来运行socket.connect()到服务器的NAT公共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

为什么不在服务器代码中使用accept方法:

我在Windows中使用了资源监视器(perfmon),移动电话与笔记本电脑之间的连接肯定成功。TCP连接列在java.exe下面。但出于某种原因,serverSocket.accept只是说“accept超时”。不知道为什么。我也把它放在了循环中,但没有接受任何东西。超时问题可能有两个原因:防火墙阻止了连接,或者你将手机连接到了错误的端口。我认为问题来自防火墙。但是如果是防火墙,为什么资源监视器会说tcp连接已经建立?或者防火墙可以在tcp连接建立后阻止它吗?我可以建议的是,当您使用accept方法时,将作为参数返回的套接字放在thread类中,而不是在您将创建的thread类的run方法中实现所需的代码。因为在accept方法中根本不返回套接字。它会导致超时的异常(由于某种原因,没有可侦听的内容)