有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java服务器在需要时侦听socket

public class Main {

    public static void main(String[] args) {
        Server server = new Server(9008);
    }
}

public class Server {

    private ServerSocket server;
    private Socket client;

    public Server(int port) {
        try {
            // Create out server with our desired port
            server = new ServerSocket(port);
            // Server started, let the user know
            System.out.println("Server started at port " + port + "...");
        } catch (IOException e) {
            // Unable to start server, print error
            System.out.println("Unable to start server on port " + port + "...");
        } 
        // Start our main server method
        runServer();
    }

    public void runServer() {
        while (true) {
            try {
                // Wait for new clients and accept them
                client = server.accept();
                // Let the user know - print
                System.out.println("New user connected - " + client.getLocalAddress().getHostAddress());
                // Start thread for our client
                Thread clientThread = new Thread(new ClientConnection(client));
                clientThread.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

所以在这一点上一切都很顺利,现在在我的clientThread中问题开始了

    public class ClientConnection implements Runnable {

    private Socket socket;

    public ClientConnection(Socket client) {
        // Set client socket
        this.socket = client;
    }

    public void run() {
        try {
            // Read from our client input
            BufferedReader readClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String line;
            while ((line = readClient.readLine()) != null) {
                System.out.println("Client says - " + readClient.readLine());
            }
        } catch(IOException e) {

        }
    }
}

有没有更好的方法来处理这个问题

我真正的客户

public class Main {

    public static void main(String args[]) {

        try {
            Socket socket = new Socket("localhost", 9008);
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            writer.write("Hello\n");
            writer.flush();
            socket.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我将显示“客户说-空”


共 (1) 个答案

  1. # 1 楼答案

    更新:在InputStream/Reader中读取的方式类似于

    while ((myString = readClient.readLine()) != null) {
       System.out.println(myString);
    }
    

    这样,当连接关闭时,循环将退出

    另外,将try/catch移动到循环之外,或者执行一些错误控制。如果出现异常,您不希望在循环中再次尝试获取

    UPDATE2:如果我的评论不够清晰,请在更新后的代码上执行

            String line;
            while ((line = readClient.readLine()) != null) {
                System.out.println("Client says - " + line);
            }
    

    while处,每次迭代只读取一次,因此如果linenull(这意味着连接已关闭),循环可以退出