7

The problem: Client doesn't receive any message.

Here is the full code for both client and server:

CLIENT

public class Client extends Socket{


public Client(String hostName, int port) throws UnknownHostException, IOException {
    super(hostName,port);

    BufferedReader in = new BufferedReader(new InputStreamReader(getInputStream()));

    while(true) {
        String line = in.readLine();
        System.out.println("Text received: " + line);
    }

}

SERVER

public final class Server extends ServerSocket{

public Server(int port) throws IOException {
    super(port);

    System.out.println("Server waiting for client 1");
    Socket client1 = accept();
    PrintWriter writer = new PrintWriter(client1.getOutputStream(), true);
    writer.write("Hello user 1");

    System.out.println("Server waiting for client 2");
    Socket client2 = accept();
    PrintWriter writer2 = new PrintWriter(client2.getOutputStream(), true);
    writer2.write("Hello user 2");

    System.out.println("Clients connected");

}
  • I start the server to listen to port 4444
  • I start the clients with hostname of "localhost" and port 4444
5
  • Why should it receive anything? Commented Jul 29, 2016 at 22:16
  • where is getInputStream() defined? Commented Jul 29, 2016 at 22:17
  • @clearlyspam23 Client class extends Socket . Commented Jul 29, 2016 at 22:28
  • Edited my answer. Is the connection going through? Try adding a println() statement after Socket client1 = accept(); and see if it prints. Commented Jul 29, 2016 at 23:17
  • You're reading lines but you aren't writing lines. Add a line terminator, or use println(). NB Your read loop is not adequate. It doesn't test for end of stream. It should be while ((line = in.readLine()) != null) {...}. Commented Jul 30, 2016 at 1:26

1 Answer 1

15

You have to include a newline character at the end of the message, and also flush the PrintWriter if the connection is not immediately altered, forcing an automatic flush:

writer.write("Hello user 1\n");
writer.flush();

EDIT:

It is possible to enable automatic flushing on a PrintWriter using the constructor new PrintWriter(someOutputStream, true)

However, as explained in the documentation:

if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked, rather than whenever a newline character happens to be output

This means that even with automatic flushing, the PrintWriter would still have to be manually flushed after write is called, and a newline character (\n) would still have to be included at the end of the string.

EDIT 2:

Here is a small example of a fully functional client/server application:

Client:

public static void main(String[] args){
    try{
        Socket socket = new Socket(HOST_ADDRESS, PORT);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        System.out.println(bufferedReader.readLine());
        bufferedReader.close();
        socket.close();
    }catch(IOException e){}
}

Server:

public static void main(String[] args){
    try{
        ServerSocket serverSocket = new ServerSocket(PORT);
        Socket socket = serverSocket.accept();
        PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
        printWriter.write("Hello user!\n");
        printWriter.flush();
        printWriter.close();
        socket.close();
        serverSocket.close();
    }catch(IOException e){}
}
Sign up to request clarification or add additional context in comments.

2 Comments

Edited to include a fully functional example
Thank you. You'r edit number 2 is working. It like magic. I have no idea why its working. The same functionality, same code...

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.