0

I am trying to build a simple multi client chat application using java sockets. The way I have gone about doing this is by having a client class that connects to a server class that waits for clients to connect and creates a new thread to deal with that client(Where the socket connection is read and written to). The client also reads from and writes to the socket connection to this thread. However, when the client wrote to the output stream of the socket, the server would not respond. A similar question here was posted:

Can you write to a sockets input and output stream at the same time?

One of the answers here says that you can read and write to a socket at the same time as long as reading from the socket is done on a separate thread.

Here is my client application:

    public class Client {

    Socket socket;

    public static void main(String[] args) {
        new Client();
    }

    public Client() {
        try {
            socket = new Socket("localhost", 4444);

            new Thread() {

                @Override
                public void run() { //read from the input stream

                    try(
                        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    ) {
                        String line;
                        while( (line = in.readLine()) != null ) {
                            System.out.println("Server said: " + line);
                        }
                    } catch(IOException e) {

                    }

                }

            }.start();

            //write to output stream
            try(
                PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
                Scanner userInput = new Scanner(System.in);
            ){
                System.out.println("Enter Something: ");
                if(userInput.hasNextLine()) {
                    out.println(userInput.nextLine());
                }
            } catch (IOException e) {

            }

        } catch(IOException e) {

        }


    }
}

And my server application:

public class Server {
    ServerSocket ss;


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

    public Server() {
        System.out.println("Server Running...");
        try {
            ss = new ServerSocket(4444);

            while(true) {


                Socket socket = ss.accept();
                new Thread() { //create new thread connection to client
                    @Override
                    public void run() {

                        new Thread() { //thread that reads inputstream
                            @Override
                            public void run() {

                                try(
                                    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                                ) {
                                    String line;
                                    while( (line = in.readLine()) != null ) {
                                        System.out.println("Client said: " + line);
                                        //The problem seems to lie here.
                                    }
                                } catch(IOException e) {

                                }

                            }
                        }.start();

                        //write to outputstream
                        try (
                            PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
                        ) {
                            String sendToClient = "Hey, my name is Server007 B)";

                            out.println(sendToClient);

                        } catch(IOException e) {

                        }
                    }
                }.start();

            }


        } catch (IOException e) {}
    }
}

I will run the server, then run the client, on the client side the output is

Server said: Hey, my name is Server007
Enter something: 
Hello! <- enter anything

but the server does not print 'Client said: Hello!' like I expected it to. I hope I made my problem clear enough, thanks.

1 Answer 1

1

Ok, so I figured it out, I will answer my own question in case anyone makes the same mistake. The PrintWriter constructor should be this:

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

Not this:

PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));

Alternatively, I could have done this:

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

I must have just gotten confused between BufferedWriter and PrintWriter :P

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.