Results 1 to 8 of 8

Thread: C server client program "Cannot bind socket" after being run 10 times

  1. #1
    Join Date
    Jan 2005
    Location
    Toronto, Ontario, Canada
    Beans
    2,204

    C server client program "Cannot bind socket" after being run 10 times

    If I run the error.sh shell script 10 times or more I eventually get the "Cannot bind socket" anyone know why this is? All the program is trying to do is to make a socket connection and then terminate.

    It should just keep connecting no?

    Thanks
    Attached Files Attached Files
    Edward A Robinson -- www.earobinson.org

  2. #2
    Join Date
    Jul 2007
    Location
    Texas
    Beans
    71
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: C server client program "Cannot bind socket" after being run 10 times

    Hi!
    I've been goofing around with it a bit, but have not solved the problem....

    I get "Cannot bind socket: [98]:[Address already in use]" for the error.

    Code:
        // Bind listen socket to listen port.
        server_address.sin_family = AF_INET;
        server_address.sin_addr.s_addr = htonl(INADDR_ANY);
        server_address.sin_port = htons(listen_port);
    
        if (bind(*listen_socket, (struct sockaddr *) &server_address, sizeof(server_address)) < 0) {
            printf("Cannot bind socket: [%d]:[%s]\n", errno, strerror(errno));
            return(1);
        }
    
        // Wait for connections from clients.
        listen(*listen_socket, 5);
        return(0);

  3. #3
    Join Date
    Jul 2007
    Location
    Texas
    Beans
    71
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: C server client program "Cannot bind socket" after being run 10 times

    Ok. I was thinking there must be some data still lingering in the queue so I added a recv() and it appears to be working ok now...

    In Server.c -
    Code:
    int main(int argc, char** argv) {
        char data[1024];
    
        if (init_listen_socket(&listen_socket, PORT) == 1) {
            return(1);
        }
    
        connect_to_client();
    
        recv(socket_fd, data, sizeof(data), 0);
    
        //close(listen_socket);
        //close(socket_fd);
    
        return(0);
    }

  4. #4
    Join Date
    Mar 2006
    Beans
    199

    Re: C server client program "Cannot bind socket" after being run 10 times

    From Beej's guide to networking:

    Sometimes, you might notice, you try to rerun a server and bind() fails, claiming "Address already in use." What does that mean? Well, a little bit of a socket that was connected is still hanging around in the kernel, and it's hogging the port. You can either wait for it to clear (a minute or so), or add code to your program allowing it to reuse the port, like this:

    PHP Code:
    int yes=1;

    // lose the pesky "Address already in use" error message
    if (setsockopt(listener,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
        
    perror("setsockopt");
        exit(
    1);

    So the sequence should be
    socket()
    setsockopt()
    bind()

  5. #5
    Join Date
    Jan 2005
    Location
    Toronto, Ontario, Canada
    Beans
    2,204

    Re: C server client program "Cannot bind socket" after being run 10 times

    Thanks this seems to work perfectly
    Edward A Robinson -- www.earobinson.org

  6. #6
    Join Date
    Jan 2005
    Location
    Toronto, Ontario, Canada
    Beans
    2,204

    Re: C server client program "Cannot bind socket" after being run 10 times

    Question is there something similiar I should be doing on the client side?
    Edward A Robinson -- www.earobinson.org

  7. #7
    Join Date
    Mar 2006
    Beans
    199

    Re: C server client program "Cannot bind socket" after being run 10 times

    You shouldn't need to do that on the client side, as it only comes into play when you try to bind().

  8. #8
    Join Date
    Dec 2006
    Location
    Glasgow, Scotland
    Beans
    470
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: C server client program "Cannot bind socket" after being run 10 times

    There shouldn't be any problem with clients. The client port is chosen automatically, typically from 49152-65535, so called ephemeral ports. The client won't reuse the same port in quick succession.

    Since a socket is an address/port pair, you only get reuse errors with servers. With clients the ephemeral port makes the socket different each time.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •