Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > java socket read() problem

Reply
Thread Tools

java socket read() problem

 
 
David Gavini
Guest
Posts: n/a
 
      07-07-2003
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(300;

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
 
Reply With Quote
 
 
 
 
Sergio Juan
Guest
Posts: n/a
 
      07-07-2003
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.

"David Gavini" <(E-Mail Removed)> escribió en el mensaje
news:(E-Mail Removed) om...
> 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(300;
>
> 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



 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      07-07-2003
On 7 Jul 2003 02:41:16 -0700, David Gavini wrote:
> 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(300;
>
> 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

--
[ do not send me private copies of your followups ]
g o r d o n . b e a t o n @ e r i c s s o n . c o m
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 1 02-03-2009 06:20 AM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 0 02-01-2009 12:45 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM



Advertisments