Socket Programming Java

devman

2[H]4U
Joined
Dec 3, 2005
Messages
2,400
Regarding TCP socket (java.net.Socket) in Java. What's the design contract regarding the OutputStream object returned by getOutputStream()? Is it guaranteed to either successfully send the data (meaning the data actually gets to the remote host) or throw an exception? I'm worried about it failing silently in the event of a network failure. The documentation is a little thin which makes me suspect its platform dependent.
 
That was my assumption as well, however I was wondering if anyone knows if it behaves like that in practice. I can see the possibility where the socket's outputstream writes the bytes to OS network stack and then returns successfully meanwhile the network stack fails to deliver the packets because the connection was lost or reset by the peer.

I guess the real question is does the outputstream wait to return until after TCP has done its thing (TCP checks for delivery of packets as part of the specification) and throw an exception if TCP timesout, or just pass it to TCP and return not waiting for timeout and thus not letting me know if the packets were actually delivered.
 
Performance would be slow if it waited for a confirmation of each write before returning, i think you will mainly get IOException's if the stream was terminated before the current write.

I would look at doing it transaction style or something. Rollback if you don't hear about it being successful.

Plus with the stream interface, you don't know how its packetizing things. Maybe you should shift to your own UDP style?
 
Back
Top