Thread: Address already in use

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    351

    Address already in use

    Hi All,

    Got a problem restarting my server:

    bind: Address already in use

    I start it like:
    Code:
                           srv.sin_addr.s_addr = INADDR_ANY;
                            srv.sin_port = htons( port);
                            srv.sin_family = AF_INET;
    
                            sock_opt = 1;
                            if(setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(void *)&sock_opt,sizeof(sock_opt)) == -1)
                            {
                                    perror("setsockopt(SO_REUSEADDR)");
                                    close(s);
                                    return 3;
                            }
    
                            if(bind(s,(struct sockaddr *)&srv,sizeof(srv)) == -1)
                            {
                                    perror("bind");
                                    close(s);
                                    return 4;
                            }
    
                            if(listen(s,3) == -1)
                            {
                                    perror("listen");
                                    close(s);
                                    return 5;
                            }
    So it should be reusing the socket.

    Had a look in nestat and:
    Code:
    tcp       25      0 localhost:5580          localhost:40353         CLOSE_WAIT
    tcp       48      0 localhost:5580          localhost:40352         CLOSE_WAIT
    tcp       25      0 localhost:5580          localhost:40361         CLOSE_WAIT
    tcp       48      0 localhost:5580          localhost:40360         CLOSE_WAIT
    Connections in CLOSE_WAIT state.

    Any ideas how I can get round this?

    Cheers, rotis23

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    so_resuse should do the trick.

    Do you have something like
    memset(&(srv.sin_zero), '\0', 8);

    I presume sock_opt is an int?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Hi Hammer,

    When you say so_resuse, you are agreeing with SO_REUSEADDR?

    Hmm. I don't memset anything, only this before previously posted code:
    Code:
    int s, sock_opt, client, cli_size, pid, res, result = 0;
    struct sockaddr_in srv, cli;
    
    //fork code
    
                            //signal handler code
    
                            s = socket(AF_INET, SOCK_STREAM, 0);
                            if(s == -1)
                            {
                                    perror("socket");
                                    return 2;
                            }
    Should I be more careful here?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>When you say so_resuse, you are agreeing with SO_REUSEADDR?
    Yes

    >>memset
    Yes, you should be memset()'ing.
    Sample:
    http://personal.windsofstorm.net/tserver.c
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    I see.

    Thanks Hammer.

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    I had a look around after your last post and noticed that there are:

    SO_REUSEADDR
    SO_REUSEPORT

    From this thread I guess SO_REUSEPORT is not needed because it allows multiple processes to bind to the same address:

    http://www.unixguide.net/network/socketfaq/4.11.shtml

    I'm also not sure if this is implemented on RedHat Linux systems or if it's just BSD.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    So, are you fixed or not?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Not sure - going to do some load testing tomorrow and see if I can reproduce.

    I've added the memset though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does this do (Windows API)?
    By EVOEx in forum Windows Programming
    Replies: 4
    Last Post: 12-19-2008, 10:48 AM
  2. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  3. I thought pointers were pointers...
    By keira in forum C Programming
    Replies: 19
    Last Post: 08-15-2007, 11:48 PM
  4. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM