I am developing netty tcp client service.
My Question
After my service calls channel.close(), is there any way to know if server has also called channel.close()? In other words, Is there any way to know if the 4-way close handshake is complete?
Background
My service communicates with one target device (server side) which have some constraints as below
- It allows only one client connection.
- If there is already a connection, close the new connection immediately.
- Even though the connection is in half closed, it consider previous connection as active connection.
and My code runs roughly as follows.
while(...) {
channel = bootstrap.connect(...).sync().channel(); // 1 line
channel.writeAndFlush(command); // 2 line
answer = waitForAnswer(); // 3 line
channel.close().sync(); // 4 line
}
When a new connection is established in the second loop, the server closes the new connection because the connection used in the first loop is still in the half-closed state.
If you look at the packet capture below, you can see that (1) in Half Closed state, (2) a new connection is established, and (3) the server notifies bad connection state and forcibly closing the new connection.
So we can simply wait until the 4-way handshake is finished by adding sleep as shown below.
while(...) {
channel = bootstrap.connect(...).sync().channel(); // 1 line
channel.writeAndFlush(command); // 2 line
answer = waitForAnswer(); // 3 line
channel.close().sync(); // 4 line
Thread.sleep(100); // 5 line
}
but I want a better way.