首页 > 解决方案 > 客户端服务器连接错误从 CMD 运行客户端类,但使用 netcat 命令可以正常工作

问题描述

我有一个服务器类,它扩展了 Thread,等待端口 8818 上的连接:

public void run() {
        // Create our server and create / accept incoming connections
        try {
            ServerSocket serverSocket = new ServerSocket(serverPortNumber);
            while (true) {
                // Listen for incoming connections and craete connection with accept
                System.out.println("Waiting for client connection.... on localhost port " + serverPortNumber
                        + ". \n Client connect via netcat localhost 8818");

                Socket clientSocket = serverSocket.accept();// returns a client Socket object
                System.out.println("Conenction established with " + clientSocket);

                // Each new connection will be handled on a new thread and can run concurrenlty
                ManageMultipleConnections multipleConnections = new ManageMultipleConnections(this, clientSocket);
                clientList.add(multipleConnections);
                multipleConnections.start();
            }
        } catch (IOException error) {
            error.printStackTrace();
        }

如果启动 CMD,并使用 netcat 建立连接,如下所示:

nc localhost 8818

一个套接字从返回

Socket clientSocket = serverSocket.accept();

然后将 clientSocket 添加到 MangeMultipleCONnections 类,该类也是一个线程。

 ManageMultipleConnections multipleConnections = new ManageMultipleConnections(this, clientSocket);
                        clientList.add(multipleConnections);
                        multipleConnections.start();

然后 ManageMultipleConeections 获取客户端的输入和输出流并处理通信:

clientInput = clientSocket.getInputStream();
        clientoutput = clientSocket.getOutputStream();
        writer = new PrintWriter(clientoutput, true);

对于多聊天服务器 - 客户端,这一切都按预期工作,其中每个客户端连接都是通过 netcat 建立的。

但是,如果我尝试通过在 CMD 上运行 java 客户端类来在同一 localhost 端口 8818 上建立与严重者的连接:

java testClient

一旦 serverSocket.accept() 执行,连接立即关闭。客户端连接类:

public static void main(String[] args) {
        // connects to a well know time server on port 13 and gets data back in tme

        Socket socket;

        String hostname;

        if (args.length > 0) {
            hostname = args[0];
        }else {
            hostname = "localhost";
        }


        try {
            System.out.println("Trying to connect to " + hostname + " on port " + 8818);
            socket = new Socket(hostname, 8818);
            System.out.println("Connected on " + socket.getInetAddress() + " " );
        }catch(UnknownHostException e) {
            System.out.println("Cant connect to " + hostname);
        }catch(IOException e) {
            e.printStackTrace();
        }

    }

例外:

java.net.SocketException:对等方重置连接:套接字写入错误

因此,我无法再通过 CMD 与客户端通信。任何输入表示赞赏。

更多信息:在 MangageMultipleConnections 线程中抛出异常:

clientInput = clientSocket.getInputStream();
        clientoutput = clientSocket.getOutputStream();
        writer = new PrintWriter(clientoutput, true);

try {
            if (loginMessage == null) {
                clientoutput.write((message).getBytes());
            } else {
                clientoutput.write((message).getBytes());
                clientoutput.write((loginMessage).getBytes());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

标签: javaconnectionclient-server

解决方案


推荐阅读