简体   繁体   中英

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?

My code is as follows, please help.

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 <-- what i am trying to run my thread with

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

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();

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

out.println("Echo: " + message);

out.flush();

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

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

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

break;

}

}

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