java socket read() problem

D

David Gavini

Hello,

I have a problem with my java Socket object. I hope someone can point
me in the right direction. My problem:

I have a standard java Socket (TCP) connected to a remote host and
port on a private network serving a a client process. Unfortunately I
do not have access to the server code (held by a 3rd party), however
my client process requests various services from the server. On one
particular request I perform 3 reads:

myRead(int size) {
byte[] data = new byte[size];
InputStream input = this.socket.getInputStream();
input.read(data);
}

sock.read(102);
sock.read(14);
sock.read(3008);

However my 3rd read only ever captures 1333 bytes (i.e. stops after
reading 1449 bytes). Note that I set the receiveBufferSize to 100Kb.

The server has no issues, since other companie's client process can
read the data fully, so I assume the problem is on my side.

I have tried the following code:
//DataInputStream di = new DataInputStream(input);
//di.readFully(data);

But this is even worse, it doesn't read any data at all! I have tried
creating my own server process to test with using ServerSockets and
the test proved fine, reading all the data that was sent.

I was wondering if there are any limitations on java sockets and read
methods?
Has anyone else seen the problem?
What happens when the socket buffer is full? Do the packets get
dropped or is data overwritten?

Many thanks in advance for your help!
Regards,
David Gavini
 
S

Sergio Juan

The InputStream read() method does not ensure that the buffer that you pass
to it is fully completed, it may depend on many issues.

What I usually do is get the number of bytes read (the read(byte[]) function
returns it) and loop until I have got all that I need.

Regards.
 
G

Gordon Beaton

I have a standard java Socket (TCP) connected to a remote host and
port on a private network serving a a client process. Unfortunately I
do not have access to the server code (held by a 3rd party), however
my client process requests various services from the server. On one
particular request I perform 3 reads:

myRead(int size) {
byte[] data = new byte[size];
InputStream input = this.socket.getInputStream();
input.read(data);
}

sock.read(102);
sock.read(14);
sock.read(3008);

However my 3rd read only ever captures 1333 bytes (i.e. stops after
reading 1449 bytes). Note that I set the receiveBufferSize to 100Kb.

The server has no issues, since other companie's client process can
read the data fully, so I assume the problem is on my side.

I have tried the following code:
//DataInputStream di = new DataInputStream(input);
//di.readFully(data);

But this is even worse, it doesn't read any data at all!

You you mean that it blocks indefininately, or that it returns without
having read anything?

Realize that if you pass a 100kB buffer to readFully(), it won't
return until 100kB has been read or the remote closes the connection.
If you are expecting exactly 3008 bytes at this point, you need to
pass that information to readFully() by using a smaller buffer or
invoking the method with size and offset arguments.
I have tried creating my own server process to test with using
ServerSockets and the test proved fine, reading all the data that
was sent.

I was wondering if there are any limitations on java sockets and
read methods?

What you have described is typical behaviour for TCP. The sender TCP
packages the data and sends it as IP datagrams of a particular size,
usually a little less than 1500 bytes at a time. When you read from
the incoming stream, you will tend to get data in chunks of about that
size. If you want more, you need to loop around read(), or use
something like DataInputStream.readFully(), which loops for you.

TCP does not preserve message boundaries, that is up to the
application.
What happens when the socket buffer is full? Do the packets get
dropped or is data overwritten?

If you don't read fast enough to keep up with the sender, the flow
control mechanisms in TCP cause the sender to block until you read
some of the data. Nothing will get dropped or overwritten.

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top