java - Socket's PrintWriter doesn't send until closed -


i have client code looks like:

socket s = new socket(server.gethostname(), server.getport()); printwriter p = new printwriter(s.getoutputstream()); p.println(message); p.flush(); s.shutdownoutput();  bufferedreader br = new bufferedreader(new inputstreamreader(s.getinputstream())); string newline; stringbuffer response = new stringbuffer(); while((newline = br.readline()) != null)     response.append(newline); system.out.println(response.tostring()); p.close(); br.close(); 

and server code looks like:

bufferedreader br = new bufferedreader(new inputstreamreader(s.getinputstream())); string nextline; stringbuffer request = new stringbuffer();  system.out.println("starting read...."); string nextline; while((nextline = br.readline()) != null){     system.out.println(nextline);     request.append(nextline); } system.out.println("message recived!!"); system.out.println("request: " + request);  printwriter p = new printwriter(s.getoutputstream()); p.println("hello, fileclient!"); system.out.println("message sent!!");  p.close(); br.close(); 

before put line s.shutdowninput() server code hang @ br.readline(). way managed fix close printwriter how, either through p.close() or through current way doesn't shutdown socket closing printwriter through p.close() does. after that, interaction between client , server perfect.

why printwriter or bufferedreader not send/receive until printwriter closed somehow?

it doesn't have printwriter. application protocol error on part.

  • the server looping reading lines until end of stream.

  • the client sending 1 line , not closing socket, no end-of-stream got sent (until added shutdown).

  • the server responding.

  • the client reading.

so client doesn't read until server gets out of loop, , server doesn't out of loop because client reading not closing.

so make mind. server should read 1 line.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -