user648129
user648129

Reputation:

How to solve TCP server address already in use error

I am writing a TCP concurrent server which will fork a child process to deal with every new connection. Suppose ClientA is interacting with ChildA while server is still listening on same port. In case we kill server with say SIGINT signal, ClientA and ChildA keep on interacting.

So, in that situation if I restart my server, it obviously throws Address already in use on bind function call.

What are all possible solutions to this issue and which one is usually followed?

Upvotes: 1

Views: 1563

Answers (2)

mmlb
mmlb

Reputation: 980

2 options.

  1. kill ChildA processes when server is killed.

  2. close the fd used to listen/bind in ChildA as soon as the fork is done.

Upvotes: 2

user207421
user207421

Reputation: 311023

Set the socket option SO_REUSEADDR on the listening socket before you bind it.

Upvotes: 1

Related Questions