Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Socket close doesn't wait for data to finish.

Status
Not open for further replies.

KheldarMAS

Programmer
Sep 7, 2001
1
US
Hello all,
I'm working on a servlet that connects to a terminal server, sends out data, and disconnects. Just about everything is fine, except when the socket closes, it throws away and data "buffered" on the outputstream.

A terminal server is basically a network serial port, with the serial port linked to a TCP port on the network. The data is going out the serial port at (usually) 9600 baud. That end of the communications uses Xon/Xoff, so data may block on the serial port. The port has a 2-4k internal buffer.

Here's what I do:

Socket s = new Socket("term_serv", port); // connect
s.setSoLinger(true, 1000); // This is supposed to make the socket "linger" until the data is gone, or 1000 seconds have passed. I took this value all the way to 65535.

OutputStream out = OutputStream(s.getOutputStream); // get the output stream

out.write(data); // Send the data
s.close(); // Close the socket

"data" is a byte array taken from a file.

Any data that makes it out the socket's buffer works just fine. It's just the last "chunk" of data that gets tossed when the socket is closed. Since the terminal server port is using Xon/Xoff, the ts buffer fills up quickly, and SHOULD stop data coming in from the TCP socket. When I programmed in other languages, the socket usually had a flush routine that waited for the output pipe to clear out, or the close routine waited. Java uses the OutputStream, but the documentation says that OutputStream.flush() does nothing. I tried it anyways.

When debugging, if I don't let it get to the s.close(), all of the data goes out fine. I've tried different OutputStream wrappers. I tried dropping the SendBufferSize of the socket way down (which helped some: more data made it out, but still not all of it).

To me, it looks like there is no way from withing Java to wait for the data on the socket's write pipe to complete. I don't want to take the SendBufferSize to 1, because it'll slow down the flow rate, increase network traffic, and I could still lose that last character.

Just fo kicks, I wrote to another file instead of a Socket, and no data is lost. Seems to be specific to the Socket.

Anyone have any ideas? I've spent the past 4 days on this problem, and am just about out of ideas.

Oh, Java 2 SDK 1.2.2 (in case that's important).

Thanks,
Matthew A. Siekierski
matt@thedra.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top