Client's BufferedReader not recieving line from Server, using sockets (Java) - java

I'm trying to make a chat application between a server and clients which are seperate classes. I'm not copying the whole code, but this is the part I'm not sure is set up correctly:
Server:
ServerSocket s = null;
Socket c = null;
s = new ServerSocket(5002);
c = s.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()))
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(c.getOutputStream()));
out.flush();
String line;
line = in.readLine();
out.write("#W|Welcome");
line = in.readLine();
out.write("#W|Welcome");
line = in.readLine();
out.write("#W|Welcome");
Client :
Socket socket = new Socket("localhost", 5002);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream ()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line;
out.flush();
out.write("#J|test");
line = in.readLine();
out.write("#J|test");
line = in.readLine();
After the client does out.println(), the server's in.readLine() gets the line. But when it's the other way around, the client keeps waiting at in.readLine(). (I used the debugger and watched the server execute out.println() and go past it, while the client is still stuck at in.readLine().
Are my data streams set up correctly or is there probably an error in my code somewhere else? I'm not sure how to check in the debugger if the streams are connected correctly.

[Quoting my comment above:]
There is nothing here that [reads or] writes lines.
That remains true. All you have is:
out.write("#W|Welcome");
etc.
don't forget to call newLine() as necessary
You forgot.

Related

Socket Println never sending data

I have code on an android (Xamarin) device as such:
socket = new Socket();
socket.Connect(new InetSocketAddress(IP_ADDR, PORT));
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.InputStream));
PrintWriter writer = new PrintWriter(socket.OutputStream, true);
string userInput = "";
while ((userInput = reader.ReadLine()) != null)
{
writer.Println(userInput);
}
The server side is a Windows server. I am reading data fine. But Println is never sending the data. I have tried .Flush() afterward, to no avail. I even considered possible newline differences and changed the code to just .Print() with 13 and 10 after, then a flush. Still nothing. Any thoughts? TIA
Moments later, I discovered the answer.
Instead of 13, 10, I send the char representation, ala:
writer.Print(userInput);
writer.Print('\r');
writer.Print('\n');
writer.Flush();

Java sockets - Why do I need a PrintWriter to make this work?

I'm trying to learn socket programming in Java but unfortunately I'm running into some behaviour that I don't understand. I have a very simple client program that connect to a server socket and sends some text that gets echoed back. Said client program looks like this:
try(
Socket socket = new Socket("127.0.0.1", 5001);
OutputStreamWriter writer = new OutputStreamWriter(socket.getOutputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
){
String userInput = "";
while (!userInput.toLowerCase().equals("quit")) {
userInput = stdIn.readLine();
writer.write(userInput);
writer.flush();
System.out.println("Server response: " + reader.readLine());
}
}
catch(Exception e) {e.printStackTrace();}
When I run this program the first line that I enter gets sent to the server but after that I can enter as many lines as I want and nothing gets sent. I also never see anything printed out by the System.out.println() line.
But if I switch out the OutputStreamWriter for a PrintWriter everything works as it should! Here's the code with PrintWriter:
try(
Socket socket = new Socket("127.0.0.1", 5001);
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
){
String userInput = "";
while (!userInput.toLowerCase().equals("quit")) {
userInput = stdIn.readLine();
writer.println(userInput);
System.out.println("Server response: " + reader.readLine());
}
}
catch(Exception e) {e.printStackTrace();}
Anyone have any idea why the first of the above two programs acts weird while the second one works? If anyone can tell me what the difference between writing with an OutputStreamWriter vs a PrintWriter is then that might tell me what's going on.
Note that the difference between write() and println() is that println() adds a linebreak after the data while write() does not.
So if your server uses readLine() to receive the data with a client using write() it might wait forever for the end of the line to read without receiving it.
So writer.write(userInput + "\n") might do the trick.

Java: ServerSocket reading data from a client

I am working on a simple programm for a university course. Here is the code that I have problems with:
//everything before this is unrelevant
String message = "";
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while((message = br.readLine()) != null) {
System.out.println(message);
}
//everything after this is irrelevant
I also have a thread for accepting incoming connections and some other stuff that is irrelevant. The problem that I have is that I can read one message and after that nothing happens. I guess the readLine() method is the problem but I am not really sure how to solve it.
It even says in our assignment to use
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
to receive data.
Your client is only sending one line. You therefore don't need the loop. You should read one line and then do whatever you're supposed to do with it.

Java TCP socket blocks at readLine

I'm working on a TCP client/server application and face the issue that the client is always blocking at br.readLine(). I tried to add a \n, but it did not solve the problem. Also a char array is blocking, when I only use read instead of readLine.
Client:
BufferedReader brInput = new BufferedReader(new InputStreamReader(System.in));
String send = brInput.readLine();
Socket socket = new Socket(host, port);
BufferedReader brSend = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter pr = new PrintWriter(socket.getOutputStream());
pr.println(send);
pr.flush();
System.out.println(brSend.readLine()); // is blocking
socket.close();
Server:
ServerSocket serverSocket = new ServerSocket(port);
while (true) {
Socket socket = serverSocket.accept(); // blocks until request is received
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
if (line.isEmpty()) break;
}
PrintWriter pw = new PrintWriter(socket.getOutputStream());
pw.write("Hello world\n");
pw.flush();
pw.close();
socket.close();
}
Your code as written does this:
The client writes one line, and then tries to read one.
The server reads multiple lines until it either gets an empty line, or the end-of-stream. Then it writes a line.
The problem is that server is waiting for the client to do something that it isn't going to do:
the client won't send an empty line (unless it read one from standard input),
the client won't close the stream ... until it gets the response from the server.
Hence the client is waiting for the server and the server is waiting for the client. Deadlock.
There are various ways to solve this. One simple way would be to change this (in the client)
println(send);
to this
println(send); println();
However, the one problem here is that your "protocol" does not cope with the case wants to send an empty line as data. That is because you are implicitly using an empty line (from the client) to mean "message completed".

Client/Server Programming

I am practicing a simple java program where I am demonstrating simple client server interaction. The fist part of message from server gets transferred. Then program just continues to run and does not execute? Do we need to create a new socket for each individual traffic?
Server code
server = new ServerSocket(4587);
System.out.print("Starting the Server on port " + server.getLocalPort() + "\n");
System.out.println("Waiting for client...");
Socket client = server.accept();
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
BufferedReader br1 = new BufferedReader(new InputStreamReader(client.getInputStream()));
br.write("Hello, you are connected to Server. What is your name?");
br.write("\n");
br.flush();
while((s=br1.readLine())!=null)
{
}
br.write("Thank you ");
br.newLine();
br.flush();
}
Client code
String stdin;
System.out.println("Attempting to connect to " + hostname + ":" + port);
client = new Socket("localhost", 4587);
System.out.println("Connection Established");
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
while ((stdin = br.readLine()) != null) {
System.out.println(stdin);
}
BufferedWriter br1 = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
br1.write("Mike");
br1.write("\n");
br1.flush();
while ((stdin = br.readLine()) != null) {
System.out.println(stdin);
}
Server Output
Starting the Server on port4587
Waiting for client....
Client Output
Attempting to connect to :123
Connection Established
Hello you are connected to Server, What is ur name
If this could help..after this both loop
Your server will first create a connection with the client through the accept method. If you wish to have multiple clients you will need to change your code accordingly to accept that.
On the client side, you're using \n to delineate the end of a message. This will work fine. Every time you send a new message use \n to indicate the end of the message.
On the server side, you should continue reading from I/O until you see the \n. At that point you have received the entire message. Process it and than start listening again.
Edit:
Since you are waiting for the name of the client, you could simply do the following on the server:
BufferedWriter bout = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
BufferedReader bin = new BufferedReader(new InputStreamWriter(client.getInputStream()));
// Wait for incoming name from client.
String name = bin.readline();
System.out.println(name);
// Send a reply.
bout.write("Thank you\n");
bout.flush();
Similarly, on the client (assuming bin and bout are defined the same as above):
// Send name to server.
bout.write("Name\n");
bout.flush();
// Get a response from the server and print to console.
String response = bin.readline();
System.out.println(response);
This is because BufferedReader has a default buffer = 8K when in reading process and this process is block I/O, so this will hang in that point. You should read the full message from client by server side.
Your problem is with the loop on the client side. It will be stuck in the loop as it waits to readlines sent from the server infinitely. (ie, after reading the first line from the server, it will expect more lines from the server and wait to read them).
To exit the loop you need to send an EOF signal or end of stream signal (according to the docs: http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#readLine%28%29)

Categories