0

我的 Java 服务器只识别我输入的第一行。我不知道为什么。我输入了多行文本,例如

Kerv
Yeah
Only

但是我的 Java 服务器只打印“Kerv”,是的,只有消失了。我的代码有问题吗?

服务器端代码:

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

public class TestTCP
{
    private static final int PORT = 1234;

    private static ServerSocket serverSocket;
    private static Socket clientSocket;
    private static InputStreamReader inputStreamReader;
    private static BufferedReader bufferedReader;
    private static String message;

    public static void main(String[] args)
    {
        try
        {
            serverSocket = new ServerSocket(PORT, 0, InetAddress.getLocalHost());

            System.out.println("IP:  " + serverSocket.getInetAddress() + "  Port:  " +  serverSocket.getLocalPort());

        } catch (IOException e)
        {
            System.out.println("Could not listen on port: 4444");
        }

        System.out.println("Server started. Listening to the port 4444");

        while (true)
        {
            try
            {
                clientSocket = serverSocket.accept(); // accept the client connection
                inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
                bufferedReader = new BufferedReader(inputStreamReader); // get the client message
                message = bufferedReader.readLine();

                while (message != null) {
                System.out.println(message);
                message = bufferedReader.readLine();
                                        }

                System.out.println(message);
                inputStreamReader.close();
                clientSocket.close();

            } catch (IOException ex)
            {
                System.out.println("Problem in message reading");
            }
        }
    }
}
4

0 回答 0