简体   繁体   English

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

[英]How to read input from a socket using threads JAVA

I am writing a simple socket programming application that uses a server and one client. 我正在编写一个使用服务器和一个客户端的简单套接字编程应用程序。 I am trying to start a thread (from client file) that reads input from the socket input stream so that i can write messages to the server and read them back and print them to the screen at the same time. 我试图启动一个线程(从客户端文件),该线程从套接字输入流中读取输入,以便我可以将消息写入服务器,然后将它们读回并同时将它们打印到屏幕上。 However, when i run my code, my code gets stuck at 但是,当我运行代码时,我的代码卡在了

message = in.readLine();

in InputReader.java file and reads no input? InputReader.java file并且不读取任何输入?

My code is as follows, please help. 我的代码如下,请帮忙。

SimpleServer1.java 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 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 <-- what i am trying to run my thread with 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();}

}

Ultimately, I would like to both read and write from the socket streams using threads. 最终,我想使用线程从套接字流读取和写入。

Thanks in advance :) 提前致谢 :)

Cobezz Cobezz

I believe you have to flush the stream you are writing to after you have written to it. 我相信您必须在写入流后冲洗掉正在写入的流。 You appear to flush the stream as soon as you create it, which won't have any effect. 您似乎在创建流后就立即对其进行刷新,这不会产生任何效果。

in SimpleServer1.java you must be add out.flush(); 在SimpleServer1.java中,您必须添加out.flush();。

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

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

out.flush(); 了out.flush();

System.out.println(clientConnection.getInetAddress() + ": " + message); System.out.println(clientConnection.getInetAddress()+“:” +消息);

if (message.equalsIgnoreCase("bye")){ 如果(message.equalsIgnoreCase(“ bye”)){

System.out.println("Closing connection..."); System.out.println(“正在关闭连接...”);

break; 打破;

} }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM