Find the answer to your Linux question:
Results 1 to 2 of 2
Hi I have made a simple TCP Server application, in the usual way (socket(), bind(), listen(), accept()). After the transactions are done, i terminate the server nicely: Code: shutdown(iSock, SHUT_RDWR); ...
  1. #1
    Just Joined!
    Join Date
    Sep 2007
    Posts
    18

    TCP: how to completely close a listening socket [SOLVED]

    Hi

    I have made a simple TCP Server application,
    in the usual way (socket(), bind(), listen(), accept()).
    After the transactions are done, i terminate the
    server nicely:
    Code:
    shutdown(iSock, SHUT_RDWR);
    close(iSock);
    But when i want to start the application again immediately,
    i cannot use the same port number to bind my listening port to -
    i get the error corresponding to "Address already in use".
    Only about a minute after i stopped the application, the port
    is free again.

    Does anybody know how a listening port can be made available
    again immediately? Is this possible at all?

    Thank you
    Jody
    Last edited by jody; 12-23-2009 at 10:00 AM. Reason: solved

  2. #2
    Just Joined!
    Join Date
    Sep 2007
    Posts
    18
    I have found the solution:
    call
    Code:
    int reuse = 1;
    setsockopt(iSock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int));
    before calling bind() in the creation of the server socket.

    Jody

Posting Permissions

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