2

i'm learning java and i faced some problems with sockets. I developed a simple client-server app - kind of knock-knock, it performs 4 steps:

  1. client sends some message to server
  2. server recieves them and saves to file
  3. server sends back to client some other messages
  4. client recieves them and also saves to file

Problem appears on step #4: client doesn't recieve messages and never gets out the loop:

while ((inStr = in.readLine()) != null) {
    writer.println(inStr);
}

where in is type of BufferedReader:

    try {
        socket = new Socket(ipAddress, 4444);
        out = new PrintWriter(socket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

On server side messages are sent:

    try {
        socket = srvSocket.accept();
        out = new PrintWriter(socket.getOutputStream(), true);          
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));                
    } catch (IOException e) {
        e.printStackTrace();
    }

...

    out.println("test from server #1");
    out.println("test from server #2");

on client side i watched in.ready() - it returns false. On server side i watch out.checkError() - it returns true;

What am i doing wrong - why is the stream empty ?

Any help ia appreciated! :)

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
Sergio
  • 33
  • 1
  • 4

2 Answers2

2

You are using public PrintWriter(OutputStream out, boolean autoFlush) which will flush automatically on new line or println. It does not autoflush after every write. You have to flush after every write.

Here is javadoc for the autoFlush param of the constructor: A boolean; if true, the println, printf, or format methods will flush the output buffer

Op De Cirkel
  • 28,647
  • 6
  • 40
  • 53
  • Thanks, but tried to flush() after every println() - the same result – Sergio Dec 19 '11 at 23:38
  • if i replace loop with [code]inStr = in.readline(); System.out.println(inStr);inStr = in.readline(); System.out.println(inStr);[/code] in order to read 2 lines from stream - it works great – Sergio Dec 20 '11 at 00:17
-1

This might/might not solve your problem. But try keeping everything within Try Catch block. For eg: your ServerSocket initialization, writer blocks etc. If some error occurs, you might not be able to use writer anyhow, so there is no point in initializing it.
 You might try writing to standard output stream for debugging instead of a file. Below code for Server/ Client is a minor variant of yours and its working.

Server:

    Socket socket;
    ServerSocket srvSocket;
    BufferedReader in;
    PrintWriter out;
    try {
        srvSocket=new ServerSocket(4444);
        socket = srvSocket.accept();
        out = new PrintWriter(socket.getOutputStream(), true);          
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out.println("test from server #1");
        out.println("test from server #2");
    } catch (IOException e) {
        e.printStackTrace();
    }

Client

    Socket socket;
    BufferedReader in;
    PrintWriter out;
    String inStr;
    try {
        socket = new Socket("127.0.0.1", 4444);
        out = new PrintWriter(socket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        while ((inStr = in.readLine()) != null) {
            System.out.println(inStr);
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
Sumit
  • 706
  • 1
  • 8
  • 16