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

F

Florent Coué

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
 
G

Gordon Beaton

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
 
E

EJP

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.
 

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,767
Messages
2,569,573
Members
45,046
Latest member
Gavizuho

Latest Threads

Top