简体   繁体   中英

How to read from standard input and from a socket at the same time (java multithreaded socket programming)

This program prompts the user with three options:

Enter an option ('m', 'f', 'x'): 

(M)essage (send)

(F)ile (request) 

e(X)it 

I want to only focus on the 'M' and 'm' options. (Adding a "receive" option is not allowed for this unfortunately)

Screenshot of code running (error on server side): “ m”后应跟“输入您的信息”,但不要


Screenshot of code running (successful client side): 之所以有效,是因为客户端在键入“ m”后首先发送了一条消息


The problem is that the BufferedReader readStandardInput and the BufferedReader readFromClient cannot execute at the same time (or at least read the same line of code at the same time). I would like to know how to make them both execute simultaneously.

Code:

try{
    OutputOptions();
    String sendingMessage = "";
    BufferedReader readFromClient = null;
    String fromClient = "";
    if(isConnected == true){
        readFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        fromClient = readFromClient.readLine();
        System.out.println("Recieved from client: " + fromClient);

      //The  4 lines above this comment are recieving a message from the client
     //the lines below this comment are reading from standard input
     //I want to be able to do both the above and the below lines at the same time (reading two types of input, standard and socket),
     //In other words, once the client sends a message after typing "m",
    //it should appear on the server side, but if i decide to type "m" first 
    //on the server side, then the client will also have to be able to
    //read that message, then afterwards be able to take in standard input

        BufferedReader readStandardInput = new BufferedReader(new InputStreamReader(System.in));
        option = readStandardInput.readLine();
        if (option.length() == 1){
              if(option.equals("m") || option.equals("M")){
                  System.out.println("Enter your message: ");
                  StandardOutput();

              }
              if(option.equals("m") || option.equals("M")){
                 System.out.println("Enter your message: ");
                 StandardOutput();

              }
              if(option.equals("f") || option.equals("F")){
                 FileTransferReceive();
              }
              if(option.equals("x") || option.equals("X")){
                 System.exit(0);
              }
              else{
                  StandardOutput();
              }
           }
           else{
                StandardOutput();
           }
        }

     //}
  }catch (IOException e){
        e.printStackTrace();
  }
  finally{ }    

Full code

Earlier version of code

My question is: How do I make the server in the first screenshot be able to enter an option "m" and read a line from the client socket at the same time?

Java standard input stream and java.net.Socket are blocking (so once you called read on either of socket.getInputStream or System.in your app will wait till it receives something), meaning you can't implement simultaneous reads in the same thread from those.

If you set up another thread(s) for serving client connection(s) you would be able to receive data from clients while waiting for the console input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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