0

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.

he

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.

Jsing
  • 35
  • 1
  • 1
  • 5
  • is there a disconnect() method on the channel? it is listed [here](https://netty.io/4.1/api/io/netty/channel/Channel.html). I think this is the one you want instead of close. – Effie Dec 22 '21 at 09:37
  • Thak you for comment. but your suggestion is not working. the same problem occurs. [disconnect()](https://netty.io/4.1/api/io/netty/channel/ChannelOutboundInvoker.html#disconnect--) says : "Request to disconnect from the remote peer and notify the ChannelFuture once the operation completes, either because the operation was successful or because of an error." It's hard to understand exactly what this means. – Jsing Dec 24 '21 at 04:28

0 Answers0