Java 多客户端-服务器消息不显示

Java multi-client -server message doesnt display

我要求客户端编写登录名,服务器应将其显示在屏幕上,作为来自客户端的整个消息的第一个词。 问题是当服务器从客户端接收消息时屏幕上没有任何显示。

public class Client {
    public static void main(String[] args) throws IOException {
        try {
            Scanner scanner = new Scanner(System.in);
            String ip = "127.0.0.1";
            Socket socket = new Socket(ip,7010);
            OutputStreamWriter out = new OutputStreamWriter(socket.getOutputStream());
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            System.out.println("Write your login:");
            String toSend;
            String arr[];
            while (true) {
                toSend = scanner.nextLine();
                arr = toSend.split(" ",2);
                System.out.println(Arrays.toString(arr));
                out.write("$login " + arr[0]);
                out.flush();
            }
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }
}

以上是客户端代码

public class Server {
    public static ArrayList<ClientHandler> clients = new ArrayList<>();
     public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(7010);
        while (true) {
            Socket socket = null;
            try {
                socket = serverSocket.accept();
                System.out.println("A new client is connected");
                OutputStreamWriter out = new OutputStreamWriter(socket.getOutputStream());
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                System.out.println("Assigning new thread for this client");
                ClientHandler client = new ClientHandler(socket,out,in);
                client.start();
                clients.add(client);
                System.out.println("Hello");
            } catch (IOException exception) {
                socket.close();
                exception.printStackTrace();
            }
        }
    }
}
class ClientHandler extends Thread {

    private Socket socekt;
    private OutputStreamWriter out;
    private BufferedReader in;

    public ClientHandler(Socket socket, OutputStreamWriter out, BufferedReader in) {
        this.socekt = socket;
        this.out = out;
        this.in = in;
    }
    @Override
    public void run() {
        String receivedName;
        try {
            receivedName = in.readLine();
            System.out.println(receivedName);
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }
}

第二次编辑代码,服务器仍然不打印登录信息

Tray 使客户端套接字和您的 ClientHandler 保持活动状态。 将 System.out.println("Write your login:"); 之后的所有内容包装到 while 循环中。还要在输出中添加“\n”,因为您使用的是 readline。接下来在循环的末尾添加 out.flush()

String toSend;
String arr[];
while (true) {
    toSend = scanner.nextLine();
    arr[] = toSend.split(" ", 2);
    System.out.println(Arrays.toString(arr)); // debug
    out.write("$login " + arr[0] +  + "\n");
    out.flush();
}

关于 ClientHandler Server 创建一个全局 ArrayList 并添加所有已创建的客户端。像这样,

public class Server {
 ArrayList<ClientHandler> clients = new ArrayList<>();
 
 public void add(ClientHandler c) {
     clients.add(c);
     System.out.println("Server added client!");
 }
 
 public static void main(String[] args) throws IOException {
    Server server = new Server();
    ServerSocket serverSocket = new ServerSocket(7010);
    Socket socket = null;
    while (true) {
        try {
            socket = serverSocket.accept();
            OutputStreamWriter out = new OutputStreamWriter(socket.getOutputStream());
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            ClientHandler client = new ClientHandler(socket,out,in);
            client.start();
            server.add(client);
        } catch (IOException exception) {
            socket.close();
            exception.printStackTrace();
        }
    }
}

并在 ClientHandler 中删除 public static ArrayList clients;ClientHandler.clients.add(receivedName);

关于输出我个人比较喜欢用OutputStreamWriter。我这样声明它们;

BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
clientSocket.setSoTimeout(30000); // some time out
OutputStreamWriter out = new OutputStreamWriter(clientSocket.getOutputStream());