我正在编写一个使用线程池的客户端/服务器应用程序,以便多个客户端可以同时与服务器通信并向其发送命令:
但它不起作用,即使使用非常简单的客户端:
这是我的客户:
    if(args.length==0) {
        host="127.0.0.1";
        port=11111;
    } else {
        host=args[0];
        String portString = args[1];
        try {
            port = Integer.parseInt(portString);
        } catch(Exception e) {
            e.printStackTrace();
            port = 11111;
        }
    }
    try {
        System.out.println("Client connects to host=" + host + " port=" + port);
        Socket skt = new Socket(host, port);//line 30 in the exception
        BufferedReader myInput = new BufferedReader(new InputStreamReader(skt.getInputStream()));
        PrintWriter myOutput = new PrintWriter(skt.getOutputStream(), true);
        myOutput.println("Hello I am the Client!");
        String buf=myInput.readLine();
        System.out.println(buf + "=buf");
        if(buf!=null) {
            System.out.println("Client receives " + buf + " from the Server!");
            buf=myInput.readLine();
            System.out.println(buf);
        }
        myOutput.close();
        myInput.close();
        skt.close();
但我得到:
客户端连接到主机 = 127.0.0.1 端口 = 11111 java.net.ConnectException:连接被拒绝:在 java.net.DualStackPlainSocketImpl.socketConnect(未知来源)的 java.net.DualStackPlainSocketImpl.socketConnect(未知来源)的 java.net.DualStackPlainSocketImpl.connect0(本机方法)连接。 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.(Unknown Source) at java.net.Socket.(Unknown Source ) at client.Client.main(Client.java:30)//看代码处的注释
所以我认为我的服务器可能是错误的,因为我在客户端看不到它:
ServerMain.java
public static void main(String[] args) {
    try {
        clientlistener = new ClientListener(server);
        //serversocket = new ServerSocket();
    } catch (IOException e) {
        System.out.println("Could not listen on tcpPort: " + tcpPort);
        e.printStackTrace();
        System.exit(-1);
    } 
}
在这里,我用一个新的打开客户端监听器
private static Server server = new Server();
并希望将其添加到 serverSocket 而不是 threadPool:
public ClientListener(Server server) throws ServerException{
    this.server=server;
     try {
            serverSocket = new ServerSocket(server.getTcpPort());
        } catch (IOException e) {
            throw new ServerException(e.getMessage());
        }
    System.out.println(String.format("Accepting new clients " +
            "on %s", serverSocket.getLocalSocketAddress()));
}
/**
 * Spawns new threads on incoming connection requests from clients.
 * */
@Override
public void run() {
    while(true){
        try {
            ClientCommunicator cc = new ClientCommunicator(serverSocket.accept(), pool);
            threadPool.execute(cc);
            clientComms.add(cc);
        } catch (SocketException e) {
            System.out.println(e.getMessage());
            return;
        } catch (IOException e) {
            System.out.println(e.getMessage());
            return;
        }
    }
}
ClientCommunicator 只是一个从命令行读取的类。
我的问题是:
我的套接字连接出了什么问题?
我打开一个并从客户那里接受它?
非常感谢你的回答!!!