Confused About Reading and Writing From Input Streams (Sockets) - java

I have a question about reading and writing from input streams related to a socket. For instance, I have
ServerSocket testie = new ServerSocket(0);
Socket putClient = new Socket("localhost",testie.getLocalPort());
InputStream is = putClient.getInputStream();
OutputStream os = putClient.getOutputStream();
os.write("testt".getBytes());
System.out.println(convertStreamToString(is));
but "testt" is oddly never printed. The method convertStreamToString is from
Read/convert an InputStream to a String
public static String convertStreamToString(java.io.InputStream is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
Is there some flaw in my understanding of streams and sockets. I thought that if you wrote to a socket's output stream you can retrieve it from its input stream? The method just hangs indefinitely.

If you write to a socket's output stream you can read from the remote socket's input stream. In your code you are writing to one side of the socket and then trying to read from that same side.
Unfortunately, you haven't even created the remote socket yet. The next thing you will want to do is call accept() on the ServerSocket. This will receive the connection from putClient and create a Socket. This new socket will be the server side socket. Essentially you will have the following:
//Server Thread
ServerSocket testie = new ServerSocket(0);
Socket remoteSocket = testie.accept();
remoteIs = remoteSocket.getInputStream();
remoteOs = remoteSocket.getOutputStream();
System.out.println(new Scanner(remoteIs).next());
//Client Thread
Socket putClient = new Socket("localhost",testie.getLocalPort());
InputStream is = putClient.getInputStream();
OutputStream os = putClient.getOutputStream();
os.write("testt\n".getBytes());
os.flush();
Notice that I said two different threads. The problem is that the call testie.accept() will block until it gets a connection. But also, new Socket("localhost",testie.getLocalPort()) will block until the connection is established. These calls can't be made on the same thread.
UPDATE:
It's probably worth pointing out that I'm not very confident in that convertStringToStream method. You might want to start with something simple like what I've fixed the above to (note the newline in the write call).

Related

Reading all string from a server socket which has not been closed

I have a problem with socket programming in Java.
There is a server which has been written in python like this which I shouldn't not change.
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send('from server\nnewline\0')
data = s.recv(BUFFER_SIZE)
s.close()
Now I want to write a code in Java which read the string from a server. Something like this:
public static String readStr(Socket client) throws IOException {
InputStreamReader inStream = new InputStreamReader(
client.getInputStream());
BufferedReader inBuff = new BufferedReader(inStream);
String answer = new String();
String str = inBuff.readLine();
while (str!=null) {
answer = answer.concat(str + "\n");
str = inBuff.readLine();
}
answer = answer.substring(0, answer.length() - 1);
System.out.println("answer:\n "+answer);
return answer;
}
But it seems that it blocks at line str = inBuff.readLine(); at the last line of the message. I tried the read() method but it was blocked too.
When designing a protocol over tcp, the best way is to include some kind of framer. This is done in the current protocol by the usage of a NUL byte.
When reading the data from the socket, you should first divide it into blocks/frames by some operations, before parsing the individual blocks.
A crude way to divide the packets into blocks is reading until you find a NUL byte, then returning that block as a byte array. (This is not the most efficient implementation)
public byte[] readPacket(InputStream in) throws IOException {
ByteArrayOutputStream tempStr = new ByteArrayOutputStream();
int read;
read=in.read();
while(read > 0){
tempStr.write(read);
read=in.read();
}
if(read == -1)
throw new EOFException();
return tempStr.toByteArray();
}
Because you now have proper frames for your data, you can now easily read the data:
byte[] frame = readPacket(in);
String[] lines = new String(frame, StandardCharsets.UTF8).split("\n");
// Do something with the lines
This is probably because the last line sent by the server does not end and readLine() method only returns when it reaches end of the line. Since you change the server's code. I recommend you use another method for reading from the stream. You may also use InputStreamReader class.
Apart from already mentioned inconsistent message/line ending - once with \n, second with \0, at the server there is no detection of end of the message. So the server will loop as long as the socket is not closed (or shut down for writing) at the client side. And as you have this line before closing the socket:
data = s.recv(BUFFER_SIZE)
in other words the client is waiting for some response from the server. But the server is stuck forever reading the message from the client in a loop.
So you need to either close the socket prior to that recv call or send some message (like empty line) and detect in on the server and eventually exit the loop.

Java communication with TCP socket and PIC stuck in read()

I try to communicate with a java application to a µController in wifi (Flyport).
I have a problem with the java application :
It first create a socket to communicate with the Flyport server, then send a message and receive the Flyport answer.
Everything work fine until the read part. I'm polling the read() function of the BufferedReader until it return -1, but it doesn't. The first read works fine, all the answer are red, but the application stay stuck when it tries to read again.
My code is very simple :
Java application :
try (
Socket clientSocket = new Socket(hostName, portNumber);
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in =
new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
)
{
...//Connection and sending message works fine
}
char[] buffer = new char[500];
while ((in.read(buffer)) != -1) { // first read() works fine, second read() stay stuck...
System.out.println(buffer); // display all answer sent by flyport
}
The code in the flyport :
while(isClientConnected){
//check if client is still connected
...
//read client message
while((RxLen=TCPRxLen(sock))>0)
{
TCPRead(sock,bff,RxLen);
strcat(msg,bff);
}
//write back to the client that the order is received
TCPWrite(sock, msg, strlen(msg));
//process the client order
...
//Write to the client that the process is done
TCPWrite(sock, msg2, strlen(msg2));
}
The java application read msg and msg2 with the first read(). msg and msg2 have "\r\n" at the end.
Doesn't somebody can tell me where I am wrong ?
Is there a function from BufferedReading that tells how much data there is left to read ?
Thanks and regards.
NB : I try with a small buffer in the java application, the problem is the same, read() is stuck when there is nothing left to read...
You're reading from the socket until end of stream, and you're never causing end of stream, as you are never closing the socket at the sender. Either close the socket or don't read until end of stream.

IOUtils copy and write issues

I'm trying to send strings to a server from a client but it doesn't seem to be reading from the input stream.
Client
Scanner scanner = new Scanner(System.in);
Socket connection = new Socket("localhost", 13)
OutputStream out = connection.getOutputStream();
while(true) {
String message = scanner.nextLine();
IOUtils.write(message, out, "UTF-8");
out.flush();
}
Server
ServerSocket server = new ServerSocket(localhost,13);~
Socket connection = server.accept();
InputStream in = connection.getInputStream();
StringWriter writer = new StringWriter();
while(true) {
try {
IOUtils.copy(in, writer);
System.out.println(writer.toString());
} catch(IOException io) {}
}
It reads if I close the stream from the client's outputstream but I am trying to send multiple messages from the client to the server. Could someone please help
You seem to think that each time you call flush() at client-side, the server will know it and be able to know that this is the end of a message. That's not the case. IOUtils.copy() reads everything from a stream of bytes. While the stream end hasn't been reached, copy() won't return.
You can see streams as two sides of a long tube. If you pour 10 buckets of water in the at the end of the tube, all you'll get at the other side is a continuous flow of water.
If you need multiple separate messages, then you need to design a protocol allowing to separate messages, and read until the end of a message has been reached. It could be based on separators for example. Or you could send the length of the message followed by the message itself, to let the server know how many bytes it must read to get the next message.

How to handle sending multiple messages over a socket connection?

I'm a bit of a beginner programmer so it's possible this is quite obvious and I'm overlooking the answer. But on to the question.
I have a two-part program (its a little more complicated than this example, but the situation is the same). The program has multiple messages fired between the client and the server. I have a PrintWriter on the server-side to send messages to the client, and on the client, I have a BufferedReader to read the messages sent.
When this example is run, I'm given two lines as output. The first message is both messages, and the second is NULL. What I am wondering is if there is a way to basically halt the server until I am ready for the second message, so that I can do something on the client-side before the second message is sent.
I am hoping to not use Thread.Sleep, as I would rather the Server wait around until the Client says it is ready.
This is the client:
public class Client{
public void run(){
Socket socket = null;
InputStream in = null;
BufferedReader reader = null;
try{
socket = new socket("LocalHost",1234);
in = socket.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
}
String messageFromServer = "";
try{
messageFromServer=reader.readLine();
}
System.out.println(messageFromServer);
String messageFromServer = "";
try{
messageFromServer=reader.readLine();
}
System.out.println(messagefromServer);
//close everything
}
}
This is the server:
public class Server{
public void run(){
ServerSocket server = null;
Socket client = null;
try{
server = new ServerSocket(1234);
client = server.accept();
}
PrintWriter writer = null;
OutputStream out = null;
try{
out = client.getOutputStream();
writer = new PrintWriter(out, true);
}
writer.write("Hi I'm a server");
//do some stuff that takes some time, user input, etc. etc.
writer.write("I'm still a server");
//close everything
}
Thanks :)
The problem with the way you currently have you code is the fact that you are using a BufferedReader, but the server is not terminating it's messages with a new line.
When you close the writer, the client is reaching the EOF or EOS and unblocking the read so it appears that both strings are being sent at once...
If you do something like...
writer.write("Hi I'm a server\n");
// This will force the message to be written to the client and picked up ;)
writer.flush();
writer.write("I'm still a server\n");
writer.flush();
Then you will get the messages seperatly...
You can use ObjectInputStream to read Objects instead of Strings.
This way you will read only one Message(String in your case) every call to ObjectInputStream.readObject();
BTW you can read the first message, "do something" and then read the second message. you don't have to read all of the sent messages at once.
If there are no other messages, then your thread will be blocked when trying to read an object from the ObjectInputStream.
Use it like:
ObjectInputStream inputStream = new ObjectInputStream( socket.getInputStream() )

BufferReader unexpected behavour

I'm trying to read data from socket using threading run method:
#Override
public void run() {
while(true){
try{
String o = "";
socketServer = new ServerSocket(port);
System.out.println("Waiting for connection on port "+port+".");
Socket socket = new Socket();
socket = socketServer.accept();
System.out.println("Connection got.");
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
int c;
while(( c = input.read()) != -1)
o += (char)c;
PrintWriter output = new PrintWriter(socket.getOutputStream());
output.println("ok");
output.flush();
output.close();
input.close();
socket.close();
socketServer.close();
textFieldMessage.setText("Сообщение от "+socket.getInetAddress().getCanonicalHostName()+":\n"+o);
}catch(Exception e){
e.printStackTrace();
System.exit(-1);
}
}
}
But it's stucked on while loop. Debugger told that there was no -1 value at the end.
What's wrong here?
Because you're reading from a Socket, you'll never reach the end of the stream unless the socket or stream has been closed. So the while loop will block on input.read() because it won't return -1 until the client closes the stream/socket.
Not really sure what it is you are reading from the socket, but you could try using the BufferedReader's readLine() method. You can either know how many lines you need to read ahead of time, or end the reading with a blank line (like HTTP).
read() method of InputStream blocks until the data is available.
It can return -1 only when the end of the stream is detected.(Though it shouldn't be the case with sockets) So while loop will block if the data is not available i.e your read call will wait for the data to be read.
Note :
But if the value is -1, your program should have exited. So what are you reading ?
The peer isn't closing the socket, so you are never getting the -1, so you are blocking in the read() invocation after the last character received. Your strategy is flawed. You are reading to EOS and then expecting to be able to write a reply. Unless the peer shuts down his socket for output and then reads, this is never going to work. You need to define your application protocol properly. At present you are expecting the peer to do write/close/read. Doesn't make sense.

Categories

Resources