Sign In/My Account | View Cart  

advertisement

AddThis Social Bookmark Button

Article:
  Networking and the BSD Sockets API
Subject:   Address in use
Date:   2002-12-28 20:29:13
From:   johnts
Great article, as usual. One thing I noticed, not really having to do with the code. When I run the server (and testing the client), I use Ctrl-C to quit the server. If I try to run the server again I get an error: "bind: Address already in use". Looks like the socket isn't being released right away. If I wait a bit, it will work again. I'm sure it's some TCP protocol issue, but thought I'd mention it.
Full Threads Oldest First

Showing messages 1 through 2 of 2.

  • Address in use
    2002-12-28 22:24:50  jasontm [View]

    you need to use setsockopt() to set the SO_REUSEADDR option. that way the socket won't stay bound to a port after your program has terminated. Unix holds onto them by default just in case your program launches in the near future. there's a delay that you remove by setting SO_REUSEADDR. :)

    use the mighty Google to find out more.. i'm too tired to launch into a tutorial. :P

    good luck..
    • Address in use
      2002-12-29 17:44:43  johnts [View]

      Got it to work, using code from Beej's Guide page mentioned in another post - there was the exact description of the problem too. I added:

      int yes=1;
      if (setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) < 0) {
      perror( "setsockopt" );
      exit(1);
      }

      and I can start the server application right after stopping it without an error.