TCP连接拒绝连接

时间:2013-05-15 06:31:33

标签: java sockets tcpclient tcpserver

我有一个标准的客户端/服务器模型,服务器成功绑定,但客户端无法连接并继续说连接已被拒绝。 (堆栈跟踪高于客户端代码)

我已将计算机放在DMZ上,以便所有端口打开并禁用所有防火墙。

以下代码适用于localhost和本地网络地址,但不适用于我的外部IP

对于此实例,serverIP字符串为0.0.0.0,但实际上它是我的真实IP地址。

canyouseeme.org 成功:我可以在端口上的0.0.0.0上看到您的服务(25565) 您的ISP未阻止端口25565 仅在服务器运行时成功

编辑:我向服务器添加了一行代码,以查看谁正在发送数据以及是否正在接收数据。

当canyouseeme.org ping 25565时,它会发送空数据,但它会给我发送的IP地址是正确的,从而指向客户端错误

客户端堆栈跟踪

java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at Main.Client.run(Client.java:29)
    at java.lang.Thread.run(Unknown Source)

客户端代码

    synchronized (runner)
    {
        while (true)
        {
            String sentence;
            String modifiedSentence;
            try
            {
                BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
/*LINE 29:*/                Socket clientSocket = new Socket(serverIP, 25565);
                DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
                BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                sentence = inFromUser.readLine();
                outToServer.writeBytes(sentence + '\n');
                modifiedSentence = inFromServer.readLine();
                System.out.println("FROM SERVER: " + modifiedSentence);
                clientSocket.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }

        }
    }

服务器代码

synchronized (runner)
    {
        while (true)
        {
            String clientSentence;
            String capitalizedSentence;
            ServerSocket welcomeSocket = null;
            try
            {
                welcomeSocket = new ServerSocket(25565);

                while (true)
                {
                    Socket connectionSocket = welcomeSocket.accept();
                    BufferedReader inFromClient =
                            new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
                    DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
                    clientSentence = inFromClient.readLine();
                    if(clientSentence == null) clientSentence = "Recieved Null Packet";
                    clientSentence = connectionSocket.getInetAddress().getHostAddress().replace("/", "") + " " + clientSentence;
                    System.out.println("Received: " + clientSentence);
                    capitalizedSentence = clientSentence.toUpperCase() + '\n';
                    outToClient.writeBytes(capitalizedSentence);
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
                try
                {
                    welcomeSocket.close();
                }
                catch (IOException e1)
                {
                    e1.printStackTrace();
                }
            }
        }
    }

0 个答案:

没有答案