Java: ServerSocket reading data from a client - java

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.

Related

When a BufferedReader is created from a socket, will readLine() == null if the connection fails?

This question is similar to Sockets: BufferedReader readLine() blocks, but not exactly.
If I have
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while ((inputLine = bufferedReader.readLine()) != null) {....
and the connection for the socket fails, will I get a null read or will the bufferedReader just hang waiting for input? I have my while loop contained in a try/catch/finally, and it never seems to get to the finally.
From my research, it seems that the only way to deal with this situation is to create a heartbeat process. Is that correct?

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();

Client's BufferedReader not recieving line from Server, using sockets (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.

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".

how does the getOutputStream() of Socket work?

{ Socket s = new Socket("xxx.xx.xx.xx",10004);
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = null;
while((line=bufr.readLine())!=null){
if("over".equals(line))
break;
out.println(line);
String upperStr = bufIn.readLine();
System.out.println(upperStr);
}
s.close();}
so does the out.println(line); mean 1. the string which was entered will appear on the screen and 2.the content will be sent to the server socket at the same time? Thanks, guys.
Your variable out is a PrintWriter, but that doesn't mean that its something that will be printed on the screen. In this case, you gave it something that is the output stream of a socket, so it will print a line to the socket. If you want it to appear on the screen as well, you'll have to call something like System.out.println(line) as well.

Categories