Data not sent after flush() with a DataOutputStream on a Socket

Discussion in 'Java' started by Florent Coué, Aug 7, 2003.

  1. Hi,

    when I write and flush some data (several packets => several write()) on a
    DataOutputStream associated to a Socket, from a certain quantity of data,
    the last sent data are lost.

    Is there any problem with the method flush() of OutputStream ?

    Do you another solution that have a sleep() before closing socket ?

    Here is a example of my program :

    ====================
    Socket socket = new Socket();
    socket.connect(new InetSocketAddress(adresseIP, port), timeout);
    DataOutputStream output = new
    DataOutputStream(socket.getOutputStream());
    ...


    for (int i = 0; i < listData.size(); j++)
    {
    output.write( (byte[])listData.elementAt(i) );
    }
    output.flush();

    output.close();
    socket.close();
    =======================

    Thanks for help
    Florent
    Florent Coué, Aug 7, 2003
    #1
    1. Advertising

  2. On Thu, 7 Aug 2003 17:56:48 +0200, Florent Coué wrote:
    > when I write and flush some data (several packets => several
    > write()) on a DataOutputStream associated to a Socket, from a
    > certain quantity of data, the last sent data are lost.


    I suspect that the problem is at the far end of the connection. What
    does the receiver of the data look like?

    You could also try socket.shutdownOutput(), wait for EOF on the
    *InputStream* associated with the socket (indicating that the remote
    has closed), and finally socket.close().

    /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, Aug 7, 2003
    #2
    1. Advertising

  3. Florent Coué

    EJP Guest

    The problem here is that you aren't taking any notice of the return
    value of write(). It is not guaranteed to write the number of bytes you
    ask it to, and you have to cope with the resulting situation.

    "Florent Coué" wrote:
    >
    > Hi,
    >
    > when I write and flush some data (several packets => several write()) on a
    > DataOutputStream associated to a Socket, from a certain quantity of data,
    > the last sent data are lost.
    >
    > Is there any problem with the method flush() of OutputStream ?
    >
    > Do you another solution that have a sleep() before closing socket ?
    >
    > Here is a example of my program :
    >
    > ====================
    > Socket socket = new Socket();
    > socket.connect(new InetSocketAddress(adresseIP, port), timeout);
    > DataOutputStream output = new
    > DataOutputStream(socket.getOutputStream());
    > ...
    >
    > for (int i = 0; i < listData.size(); j++)
    > {
    > output.write( (byte[])listData.elementAt(i) );
    > }
    > output.flush();
    >
    > output.close();
    > socket.close();
    > =======================
    >
    > Thanks for help
    > Florent
    EJP, Aug 8, 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. Florent Coué
    Replies:
    0
    Views:
    497
    Florent Coué
    Aug 8, 2003
  2. JScoobyCed

    DataOutputStream/DataInputStream

    JScoobyCed, Aug 10, 2004, in forum: Java
    Replies:
    2
    Views:
    906
    Michael Borgwardt
    Aug 11, 2004
  3. Digital Puer
    Replies:
    3
    Views:
    442
    Mike Schilling
    Mar 15, 2006
  4. Replies:
    1
    Views:
    789
    John B. Matthews
    Sep 15, 2008
  5. Rafal\(sxat\)

    DataOutputStream how make wirte dev/null

    Rafal\(sxat\), Jun 8, 2009, in forum: Java
    Replies:
    7
    Views:
    536
    Kevin McMurtrie
    Jun 14, 2009
Loading...

Share This Page