Can I force a socket to close in CLOSE_WAIT state?

I’m troubleshooting some processes that communicate via sockets over our network. One process listens on port 2045. When the process fails (for an unkown reason at this point), I am unable to relaunch the process as I get an error, “binding stream socket:: Address already in use” and
“Error opening socket”. When I run a netstat there are 6 tcp connections using port 2045 all in CLOSE_WAIT state. When everything is working fine, there is just 1 connection on that port and is in the ESTABLISHED state. Is there a way I can force those connections to close so I can relaunch the process much quicker? Is there a way to check them all in the code before I try and listen on that port? Will that mess up the other process on the other end of the socket? Thanks!

This is a common TCP/IP situation. Look it up on, this has been answered numerous time.

Here is a clue for you SO_REUSEADDR and SO_REUSEPORT

Do you need to run it for a specific address/port?

I run it like this:

int sockL;
int r = 1;

setsockopt(sockL, SOL_SOCKET, SO_REUSEADDR, &r, sizeof(r));
setsockopt(sockL, SOL_SOCKET, SO_REUSEPORT, &r, sizeof(r));

and I still get the same problem.