java socket read() problem

Discussion in 'Java' started by David Gavini, Jul 7, 2003.

  1. David Gavini

    David Gavini Guest

    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
     
    David Gavini, Jul 7, 2003
    #1
    1. Advertising

  2. David Gavini

    Sergio Juan Guest

    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" <> escribió en el mensaje
    news:...
    > 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
     
    Sergio Juan, Jul 7, 2003
    #2
    1. Advertising

  3. 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(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

    --
    [ 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
     
    Gordon Beaton, Jul 7, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    4,922
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    992
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    565
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    683
    Steve Holden
    Feb 1, 2009
  5. Replies:
    19
    Views:
    1,323
    Nigel Wade
    Mar 15, 2012
Loading...

Share This Page