Java socket programming writebytes to readline - java

I am trying to get a java server and client communicating. For streaming data to the server I have this code:
Socket ClientSocket = null;
ClientSocket = new Socket(IPAddress, portInt);
DataOutputStream outToClient = new DataOutputStream(ClientSocket.getOutputStream());
outToClient.writeBytes(command);
outToClient.flush();
And for the server I have:
ServerSocket mysocket = new ServerSocket(8081);
Socket connectionsocket = mysocket.accept();
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(connectionsocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionsocket.getOutputStream());
//program hangs here, client not sending
GetRequest = inFromClient.readLine();
System.out.println("Received: " + GetRequest);
These are only short portions of the overall code, I have found that the program hangs on the server side when the readLine(); is reached. I am trying to send data with writeBytes(command); where command is a string. Any suggestions? thanks.

writebytes to readline
Stop right there. If you're using readLine() you're using BufferedReader, which is a Reader, which means you should be using a Writer to talk to it, which means you should be using a BufferedWriter, and as you're reading lines you must write lines, which means writing a line terminator (which you aren't presently doing), which means you should use BufferedWriter.newline().
Or PrintWriter.println(), but don't forget to check for errors, as it swallows exceptions.

Don't directly use readLine
Instead try this
If( inFromClient.ready()){
// read string here.
}
It might be possible that buffer is not ready and you are trying to read. So it can create problem.

Related

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

Difference between printwritter and BufferedReader for Java server sockets

Wanted to know with java sockets whats the difference between the two. Trying to understand how to make a java server socket code using lessons from oracle and this is what it shows at the momment on their tutorials
int portNumber = Integer.parseInt(args[0]);
try (
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
) {
-Reader and -Writers are character-based and streams are byte-based.
In your example. You use writer when you want to write something to the socket and reader when you read something from the socket.
Hank

Java Socket server side cannot read incoming line of text in play framework application

So I'm having some serious problems with Java's server side socket, which accepts connection, but it can't read anything from BufferedReader, which I have put to read the text stream from socket connection. Code for my threads run(), which I'm creating and running at the first time when any page is loaded.
public void run() {
try{
ServerSocket s = new ServerSocket(4100);
System.out.println("New tcp socket created");
Socket socket = s.accept();
System.out.println("New tcp update connection established.");
InputStream din = socket.getInputStream();
PrintWriter outp = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(din));
System.out.println("Streams created");
String inputline = "nothing yet...";
outp.println("hello from server");
while(true){
System.out.println("Got input from client:" + inputline);
inputline = in.readLine();
if(inputline == null || inputline.equals("exit")){
break;
}
}
}
catch(Exception e){
e.printStackTrace();
}
System.out.println("Updater thread exits.");
}
This prints out everything properly, except for Got input from client: + what ever my client sends with PrintWriter which outputs to a socket.
Client side example:
Socket s = new Socket(serverip, serverDownloadsUpdatePort);
OutputStream dout = s.getOutputStream();
PrintWriter outp = new PrintWriter(dout);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println(in.readLine());//This prints out properly, what server sends to client
outp.println("test connection");
outp.println("Can you hear me?");
outp.println("exit");
s.close();
Your client may not be sending end-of-line characters along with its input, causing your server to wait indefinitely at "in.readLine()".
The Javadoc for BufferedReader's readLine method (http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#readLine()) says: "Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed." Make sure that your client is sending input that conforms to this rule.
I was able to see client input using your server with the following client Runnable (but only if I include the "\n"):
public void run() {
try{
Socket writeSocket = new Socket("localhost", 4100);
PrintWriter out =
new PrintWriter(writeSocket.getOutputStream(), true);
out.write("Hello there!\n");
out.flush();
}
catch(Exception e){
e.printStackTrace();
}
}
EDIT: When using println as in the submitter's client example, you don't need to worry about adding "\n", but you do need to flush the socket. One way to make sure this happens is by setting autoFlush=true in the PrintWriter constructor.
I found out that I forgot to set PrintWriter as auto flushable at client side and thats why it didn't work becouse stream didn't got flushed at any time.

Read data from a Java Socket

I have a Socket listening on some x port.
I can send the data to the socket from my client app but unable to get any response from the server socket.
BufferedReader bis = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
String inputLine;
while ((inputLine = bis.readLine()) != null)
{
instr.append(inputLine);
}
This code part reads data from server.
But I can't read anything from server until unless the Socket on the server is closed.
Server code is not under my control to edit something on it.
How can I overcome this from client code.
Thanks
Looks like the server may not be sending newline characters (which is what the readLine() is looking for). Try something that does not rely on that. Here's an example that uses the buffer approach:
Socket clientSocket = new Socket("www.google.com", 80);
InputStream is = clientSocket.getInputStream();
PrintWriter pw = new PrintWriter(clientSocket.getOutputStream());
pw.println("GET / HTTP/1.0");
pw.println();
pw.flush();
byte[] buffer = new byte[1024];
int read;
while((read = is.read(buffer)) != -1) {
String output = new String(buffer, 0, read);
System.out.print(output);
System.out.flush();
};
clientSocket.close();
To communicate between a client and a server, a protocol needs to be well defined.
The client code blocks until a line is received from the server, or the socket is closed. You said that you only receive something once the socket is closed. So it probably means that the server doesn't send lines of text ended by an EOL character. The readLine() method thus blocks until such a character is found in the stream, or the socket is closed. Don't use readLine() if the server doesn't send lines. Use the method appropriate for the defined protocol (which we don't know).
For me this code is strange:
bis.readLine()
As I remember, this will try to read into a buffer until he founds a '\n'. But what if is never sent?
My ugly version breaks any design pattern and other recommendations, but always works:
int bytesExpected = clientSocket.available(); //it is waiting here
int[] buffer = new int[bytesExpected];
int readCount = clientSocket.read(buffer);
You should add the verifications for error and interruptions handling too.
With webservices results this is what worked for me ( 2-10MB was the max result, what I have sent)
Here is my implementation
clientSocket = new Socket(config.serverAddress, config.portNumber);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
while (clientSocket.isConnected()) {
data = in.readLine();
if (data != null) {
logger.debug("data: {}", data);
}
}

Should I have two threads for input/output or use NIO?

I have been working on a (relatively) simple tcp client/server chat program for my networking class. The problem that I am running into is I am using blocking calls, such as read() and writeBytes(). So, whenever I try to send a message to my server, the server does not print it out until it writes one back. For this situation, would using one thread for input and one thread for output be the most sensible solution, or would using NIO serve me better? Just to give you an idea of what my code looks like now, my server is:
ServerSocket welcomeSocket = new ServerSocket(port);
DataOutputStream output;
BufferedReader inFromUser = new BufferedReader( new InputStreamReader(
System.in));
String sentence;
while ((sentence = inFromUser.readLine()) != null) {
Socket connectionSocket = welcomeSocket.accept();
output = new DataOutputStream( connectionSocket.getOutputStream());
output.writeBytes(sentence + "\n");
BufferedReader inFromServer = new BufferedReader( new InputStreamReader(
connectionSocket.getInputStream()));
System.out.println("Client said: " + inFromServer.readLine());
connectionSocket.close();
}
The client code is essentially the same. Thanks for your time!
Just use two threads unless you want to learn about NIO. The Java tutorial has examples of spawning threads to handle client connections to a ServerSocket. Look toward the bottom of "Writing the Server Side of a Socket".

Categories