Question

I am trying to write a small program, that opens a server, creates a client that connects to this server and receives a message from it.

This is the Code so far

public static void main(String[] args) {
final ServerSocket serverSocket;
try {
    serverSocket = new ServerSocket(12345);
    Thread t = new Thread(){
        public void run(){
                try {
                    Socket server = serverSocket.accept();
                    PrintWriter writer = new PrintWriter(server.getOutputStream(), true);
                    writer.write("Hello World");
                    writer.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        t.start();
        Socket client = new Socket("localhost", 12345);
        BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
        String message = reader.readLine();

        System.out.println("Received " + message);
    } catch (IOException e1) {
        e1.printStackTrace();
    }

}

If i run program it keeps waiting in readLine() - so obviously the client does not receive the message from the server. Has anyone got an idea why this isn' working?

Was it helpful?

Solution

Your reading thread is waiting for a newline in the data stream. Just change the server to use:

writer.write("Hello World\r\n");

and you'll get the result you were expecting. Alternatively, you can just close the server socket, and then readLine will return when it reaches the end of the data stream.

OTHER TIPS

You should put the readline in a loop as follows:

public static void main(String[] args) {
    final ServerSocket serverSocket;
    try {
        serverSocket = new ServerSocket(12345);
        Thread t = new Thread() {
            public void run() {
                try {
                    Socket server = serverSocket.accept();
                    PrintWriter writer = new PrintWriter(server.getOutputStream(), true);
                    writer.write("Hello World");
                    writer.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        t.start();
        Socket client = new Socket("localhost", 12345);
        BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
            // Check this --------------------------------------------------->
            String message = null;
            while ((message = in.readLine()) != null) {   
                    System.out.println("Received " + message);
                    break; //This break will exit the loop when the first message is sent by the server
            }

    } catch (IOException e1) {
        e1.printStackTrace();
    }

}

You can read this documentation for further explanation: http://download.oracle.com/javase/tutorial/networking/sockets/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top