I took Computer Networking last semester and did some C programming in linux (using gcc) for my projects. One extremely tedious thing I kept running into was if my program crashed or stalled (which I then would have to hit Ctrl+C to kill it), the network port would still be left open for a minute or so. So if I wanted to immediately run the program again, I would have to first go into the header file, change the port, remake the program, and then finally run it. Obviously, this gets very tedious very fast.
Is there any way to configure it where the port is immediately released as soon as the process is killed? Either via some setting in linux, or in the makefile for my program, or even programmatically in C?
Edit: I'm referring to when writing a server and choosing a specific port to host the program.
Set the the option SO_REUSEADDR
on the socket.
int yes = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
From Beej's Guide to Network Programming.
I bet it's about two minutes :)
As @Cogsy noted, the SO_REUSEADDR
socket option is your friend.
Make yourself familiar with TCP states, it's TIME_WAIT
state that causes you problems:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With