1
public static void main(String args[]){

    byte[] message = ...
    Socket socket = ...
    DataOutputStream dOut = new DataOutputStream(socket.getOutputStream());

    dOut.write(message);  //#1

    dOut.close();
    socket.close();
}

Let's assume that the line #1 will put the data into buffer waiting to flush to remote machine. After that the stream and socket are closed.

We assume that, in the sending process, there is some unknown problem happens in network, and our operating system will resend the packet that was in the buffer until the TCP re-tranmission timeout.

I am wondering that how I can catch this exception in Java program? Because the code above already send out data to buffer and probably closed the stream and socket (and probably exit the Java main body), left all the other job (TCP-related, re-tranmission) to operating system.

My question is, will the TCP re-tranmission (we assume packet lost) continue even Java program exit? What is the best method to catch the re-tranmission timeout error?

2
  • There is no such thing as 'Java TCP retransmission timeout', or indeed 'Java TCP'. There is just the platform's TCP stack, and its behaviour.
    – user207421
    Commented Jul 14, 2013 at 8:11
  • Hi @EJP, I know that dOut.write will only be blocked if the TCP send buffer is full. Let's assume that, dOut.write is blocked because buffer is full now, and the network has some problem in sending data, so TCP keep on re-transmit the data. In this case, will dOut.write throw exception after TCP exceed the maximum re-transmission time? Thanks for help.
    – Sam YC
    Commented Jul 15, 2013 at 9:02

1 Answer 1

2

TCP will continue to try to cleanly shutdown the connection even after the program exits. It is generally recommended that the application perform the shutdown itself. Basically, you perform the following sequence of steps:

  1. Shutdown the TCP connection in the send direction triggering the normal close sequence. If the protocol prohibits the other side from sending any data, you can shutdown the connection in the receive direction as well, however, if you do this and the other side sends any data, it may cause the other side to detect an abnormal shutdown as the data it sent will be lost.

  2. Continue to read from the connection until you detect a clean or abnormal shutdown from the other end. If all goes well, you will detect a clean shutdown as soon as you finish receiving any data the other side has sent.

  3. Close the handle or delete the object/reference to the connection. The actual TCP connection is already shut down.

4
  • hi, I don't exactly understand the first line "TCP will continue to try to cleanly shutdown the connection even after the program exits". Is it saying that, operating system will continue sending (until remote machine receive data or timeout) even Java program already exited?
    – Sam YC
    Commented Jul 14, 2013 at 10:46
  • @GMsoF: That's up to the implementation. It may be the operating system. It may be a separate TCP stack that's not part of the operating system. It may be the JVM. But that is the most common practice. To be safe, the application should manage the shutdown to ensure it gets the behavior it wants. Commented Jul 14, 2013 at 10:56
  • It can't be the JVM after the JVM exits.
    – user207421
    Commented Jul 14, 2013 at 21:41
  • @EJP: That the JVM can exit is an implementation detail. The JVM could be implemented in hardware. Implementation details are really not that interesting. Commented Jul 14, 2013 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.