4

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.

JoeCool
  • 4,392
  • 11
  • 50
  • 66
  • 1
    You can sidestep the problem by choosing a random port, and printing it out on execution. – Tordek Jun 17 '09 at 17:55
  • @Tordek Good point, but I'm just going to note here for posterity that if you do that you have to choose from a safe range to ensure you don't stumble upon any standard ports – JoeCool Jun 17 '09 at 17:56
  • 1
    Well, yeah... sidestepping carelessly can make you step on dog poo... – Tordek Jun 17 '09 at 18:05

3 Answers3

12

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.

Cogsy
  • 5,584
  • 4
  • 35
  • 47
  • That's a good idea. Another alternative is, code your program for accepting the port number as input to avoid a recompile. To actually release the port like you want, you need to write a Ctrl-C handler and code the close there. – nik Jun 17 '09 at 18:00
  • (Unrelated) What was somebody thinking to make that fourth argument a pointer to an int instead of an int? – Kai Jun 17 '09 at 18:03
  • That's true, the above solution does not actually release the socket, but it does allow it to be re-bound on the next execution. – Cogsy Jun 17 '09 at 18:04
  • @Kai: (get|set)sockopt support all sorts of options on all sorts of sockets. Some values for the 2nd and 3rd argument result in the 4th argument being treated as a pointer to a structure or a string. – ephemient Jun 17 '09 at 18:13
  • It's ok if it doesn't technically release the socket, as long as the port can be reused, because that still solves my problem. Thanks! – JoeCool Jun 17 '09 at 18:37
2

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:

 
Community
  • 1
  • 1
Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
1

I assume the program you're writing is a server, so you need to use a known port. If that's the case, you should use the SO_REUSE_ADDR option on the socket as pointed out by Cogsy.

If on the other hand you're writing a client sw, then you should avoid choosing a particular port, allowing the system to hand you a random one.

Metiu
  • 1,677
  • 2
  • 16
  • 24