I got a server waiting for messages and a client sending him messages, when using
PrintWriter out = new PrintWriter(lpSocket.getOutputStream(), true);
String msg;
BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in));
while ((msg = userIn.readLine()) != null)
out.print(msg + (char) 10);
nothing happens, meaning the server just keep waiting for a message that never arrive. When I change the out.print to out.println it works.
I would like to know why
By default, PrintWriter calls flush in println, whereas it doesn't do this in print. You can fix this by adding out.flush() after your out.print call
Related
I have client code that 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 that 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 I put the line s.shutDownInput() the server code would hang at br.readLine(). The way I managed to fix that is to close the PrintWriter some how, either through p.close() or through the current way which doesn't
shutdown the socket like closing the PrintWriter through p.close() does. After that, the interaction between client and server is perfect.
Why does the PrintWriter or the BufferedReader not send/receive until the PrintWriter is closed somehow?
It doesn't have anything to do with PrintWriter. It is an application protocol error on your part.
The server is looping reading lines until end of stream.
The client is sending one line and then not closing the socket, so no end-of-stream got sent (until you added the shutdown).
The server is then responding.
The client is then reading.
So the client doesn't read anything until the server gets out of the loop, and the server doesn't get out of its loop because the client is reading not closing.
So make up your mind. Probably the server should only read one line.
Both sides are behaving exactly as you told them to do. In particular, you instruct the server specifically to read everything the client sends before dispatching any response:
String nextline;
while((nextline = br.readLine()) != null){
System.out.println(nextline);
request.append(nextline);
}
It is important to understand that that will not stop reading until an error or end of stream, where end of stream on a socket corresponds to the other end having been closed and all data having been read. In other words, your server waits for the end of the whole conversation, not the end of a single message, before dispatching a response.
Since that's apparently not what you want, you need to implement a different strategy at the server for determining when to process the data received so far and send a response. If you can be confident that your messages will not contain internal newlines, then that might be as simple as the server performing only one br.readLine() before sending each response.
This question already has answers here:
Java - Understanding PrintWriter and need for flush
(2 answers)
Closed 6 years ago.
I am trying to make a simple quiz between client and server. The server sends an array of questions and waits for a reply from client. The problem is that the client side does not display the array from server nor can take any input. The server has definitely connected to the client, but the client side stays idle.
Server:
OutputStream o =sock.getOutputStream();
PrintWriter pw = new PrintWriter(o);
InputStream is = sock.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
int i=0;
while(i<10)
{
pw.println(array[i]);
String st = br.readLine();
System.out.println(st);
i++;
}
Client:
InputStream istream = sock.getInputStream();
BufferedReader content = new BufferedReader(new InputStreamReader(istream));
String str;
OutputStream ostream=sock.getOutputStream();
PrintWriter pw = new PrintWriter(ostream)
String ans;
for(int j=0;j<10;j++)
{
str=content.readLine();
System.out.println(str);
ans=sc.nextLine();
pw.println(ans);
}
PrintWriter#println doesn't flush text to client by default so you need to call pw.flush() manually each time you want to ensure sending.
To let println automatically flush text to client use PrintWriter(Writer out, boolean autoFlush) constructor with autoFlush parameter set as true, like
PrintWriter pw = new PrintWriter(o, true);
since PrintWriter(OutputStream out) constructor internally invokes this(out, false); so automatic flash is disabled println, printf or format methods.
PrintWriter doesn't flush information itself by default.
You should either add: pw.flush() after pw.println(ans) or create your writer in that way: PrintWriter pw = new PrintWriter(o, true);
I am using following code
OutputStream out = socket.getOutputStream();
PrintWriter output = new PrintWriter(out);
#SuppressWarnings("unused")
byte[] b = message.getBytes("US-ASCII");
String convertedString = Normalizer.normalize(message, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "");
Log.d("ASCII", convertedString);
output.println(convertedString);
Log.e("TCP Message sent", convertedString);
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//read line(s)
String st = input.readLine();
Log.e("TCP Message received", st);
But I am stuck at String st = input.readLine(); It just hangs there and never proceed to next line. Is this correct way of sending message using TCLP And then get response?
You are getting stuck at readLine() because the connection is still open - android is still waiting for more data to come in, and so can't move on.
This is what I did, but whether this is viable may depend on the protocol you are adhering to.
StringBuilder builder = new StringBuilder();
String line;
while ((line = input.readLine()) != ""){
builder.append(line);
}
Log.i("data", builder.toString());
What you need is flushing your output stream after sending message by:
output.flush();
or use auto flush by passing true while creating Output Stream like this:
PrintWriter output = new PrintWriter(out, true);
Hope this helps
I have the basic code for a server:
ServerSocket serverSocket = new ServerSocket(14000);
Socket clientSocket = serverSocket.accept();
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
String incoming;
while((incoming = in.readLine()) != null){
System.out.println("Client Says: " + incoming);
out.println("Client Says: " + incoming);
out.flush();
//if(incoming.equals("HELLO")) break;
}
clientSocket.close();
serverSocket.close();
I'm trying to further understand streams as they're giving me some serious headaches. From what I've read, println methods automatically flush for you, however this line is not delivered to the client unless the flush method is called afterwards? I'm just looking for a nice solid explanation of this?
To enable automatic flushing of the PrintWriter, the second argument of its constructor must be set to true.
I had this issue before, just:
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
That will make the difference. If you do not autoflush you can get some errors or receive data as null, it happened to me some time ago. Best regards.
I've got the following code, I'm trying to send data text by sockets. But when I try to send via PrintWriter, my client does nothing and it stops at in.readLine(). However when I use simply OutputStream and send it as bytes, my client doesn't have any problem with reading. Is it possible to as I want ?
out = new PrintWriter(sock.getOutputStream());
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
while (true) {
System.out.println("SERVER-THREAD: IP "
+ sock.getInetAddress().getHostAddress());
out.write(marshall() + "\n");
Thread.sleep(1000);
}
//Client
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
out = new PrintWriter(sock.getOutputStream());
while (updateList) {
System.out.println("Before");
String inputip = in.readLine();
System.out.println("CLIENT: " + inputip);
//unmarshall(in);
System.out.println("After");
Thread.sleep(1000);
}
PrintWriter buffers the data to be written so it will not do so until the buffer is full. You need to call flush here
out.flush();
When you perform a readLine() it waits until it has a read a whole line. i.e. a new line.
Your send is sending text without a newline so the receive waits for something which will not happen.
A more basic problem is that you are mixing text and binary which is more likely to confuse than be useful.
I suggest you write text with PrintWriter.println() which you can read with BufferedReader.readLine().