Java网络服务器 - 客户端错误

tr0*_*n0m 5 java connection networking

我正在尝试为(简单的,对于初学者)服务器客户端编程,只是为了建立连接并查看它是否有效.确实如此.只要我留在自己的四面墙/网络内.一旦我尝试通过我的路由器IP,客户端就会产生一个强大的错误消息.所有常见的嫌疑人都已被淘汰:路由器端口转发已打开,防火墙不会干扰(嗯,当我关闭它时它仍然无法工作),并且canyouseeme.org说它可以建立与我选择的连接服务器运行时的端口.

这是服务器的源代码,因为我发现可以通过一点telnet来通过命令行.当我尝试建立连接时,它只是说Could not open connection to the host, on port 49163:Connection failed

服务器:

import java.net.*;
import java.io.*;

public class ChatServer {
    public static void main(String[] args) throws IOException {

        ServerSocket server = null;

        try {
            System.setProperty("java.net.preferIPv4Stack" , "true");
            server = new ServerSocket(49163);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 49163.");
            System.exit(1);
        }

        Socket client = null;
        try {
            client = server.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        PrintWriter out = new PrintWriter(client.getOutputStream(), true);
        BufferedReader in = new BufferedReader(newInputStreamReader(client.getInputStream()));
        String inputLine;
        String outputLine;

        out.println("Connection established");
        while ((inputLine = in.readLine()) != null) {
            if (inputLine.equals("exit")) {
                break;
            }
        outputLine = inputLine;
        out.println(outputLine);
        }
        out.close();
        in.close();
        client.close();
        server.close();
        System.out.println("Server offline");
    }
}
Run Code Online (Sandbox Code Playgroud)

哦,是的,我更喜欢在Stackoverflow上使用的IP4vStack,但它似乎对我不起作用.

出于某种原因,我无法包含客户端的代码,我不断收到有关格式化的消息,我只是想不出来.但我看到它的方式是我甚至无法通过命令行连接(当我尝试连接到"真正的"IP时),因此客户端不必参与.但对于想要查看错误的人:

java.net.ConnectException: Connection refused: connect
  at java.net.PlainSocketImpl.socketConnect(Native Method)
  at java.net.PlainSocketImpl.doConnect(Unknown Source)
  at java.net.PlainSocketImpl.connectToAddress(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 ChatClient.main(ChatClient.java:12)
Run Code Online (Sandbox Code Playgroud)