如何使用线程从套接字读取输入 JAVA

How to read input from a socket using threads JAVA

我正在编写一个使用服务器和一个客户端的简单套接字编程应用程序。我正在尝试启动一个从套接字输入流读取输入的线程(从客户端文件),以便我可以将消息写入服务器并读回它们并同时将它们打印到屏幕上。但是,当我 运行 我的代码时,我的代码卡在了

message = in.readLine();

InputReader.java file 中没有读取输入?

我的代码如下,请大家帮忙

SimpleServer1.java

public class SimpleServer1 {
public static void main(String args[]){
    int portNumber = Integer.parseInt(args[0]);

    ServerSocket serverSocket = null;
    Socket clientConnection = null;

    try{
        //setup sockets
        serverSocket = new ServerSocket(portNumber);
        clientConnection = serverSocket.accept();
        System.out.println("Connected to" + clientConnection.getInetAddress());

        //setup streams
        BufferedReader in = new BufferedReader(new InputStreamReader(clientConnection.getInputStream()));
        PrintWriter out = new PrintWriter(clientConnection.getOutputStream());
        out.flush();

        //read input from stream
        String message;
        while((message = in.readLine()) != null){
            //return message to client
            out.println("Echo: " + message);

            System.out.println(clientConnection.getInetAddress() + ": " + message);
            if (message.equalsIgnoreCase("bye")){
                System.out.println("Closing connection...");
                break;
            }
        }
        //close streams
        out.close();
        in.close();
        clientConnection.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}

SimpleClient1.java

public class SimpleClient1{
public static void main(String args[]){
    String hostName = (args[0]);
    int portNumber = Integer.parseInt(args[1]);

    try{
        Socket serverConnection = new Socket(hostName, portNumber);
        PrintWriter out = new PrintWriter(serverConnection.getOutputStream(), true);
        out.flush();

        Thread listener = new Thread(new InputReader(serverConnection));
        listener.start();

        Scanner keyboard = new Scanner(System.in);
        String userInput;
        while((userInput = keyboard.nextLine()) != null){
            out.println(userInput);
            if (userInput.equalsIgnoreCase("bye")){
                break;
            }

        }
        //closing streams
        out.close();
        serverConnection.close();

    }catch(Exception e){
        e.printStackTrace();
    }   
}

InputReader.java <-- 我正在尝试 运行 我的线程

public class InputReader implements Runnable{
private Socket serverConnection;
private BufferedReader in;
public InputReader(Socket socket){
    serverConnection = socket;
    try{
        in = new BufferedReader(new InputStreamReader(serverConnection.getInputStream()));
    }catch(IOException ioE){ioE.printStackTrace();}
}

@Override
public void run() {
    try{
        String message;
        while(true){
            System.out.println("done");
            message = in.readLine();
            System.out.println(message);
        }
    }catch(IOException ioE){ioE.printStackTrace();}

}

最终,我想使用线程从套接字流中读取和写入。

提前致谢:)

科贝兹

我相信您必须在写入后刷新正在写入的流。您似乎在创建流后立即刷新它,这不会有任何影响。

在 SimpleServer1.java 你必须添加 out.flush();

while ((message = in.readLine()) != null) {

out.println("Echo: " + 消息);

out.flush();

System.out.println(clientConnection.getInetAddress() + ": " + message);

if (message.equalsIgnoreCase("bye")){

System.out.println("Closing connection...");

休息;

}

}