POSIX Socket - Find an open port?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: POSIX Socket - Find an open port?

  1. #1
    Join Date
    Oct 2011
    Posts
    97

    POSIX Socket - Find an open port?

    Hi guys, I'm doing some network programming on the school Linux computers, and I need some help with sockets. Essentially, I have two programs: one that will scan for an open port, and one that will connect to the open port the other one found. My problem is that if I open a socket on a port and find out it's open, even after I close it, the other program can't listen on that port because it hasn't finished it's linger time. And for the second program that does the connecting, I'm using the Thrift network, so I have no control over HOW it connects to the socket. I just give a port number and that's it.

    So I'm really stuck here. In C/C++, how can I go about finding an open port on my machine, yet still be able to connect to that port from another program? Any help would be much appreciated.

    Thanks in advance.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: POSIX Socket - Find an open port?

    Your other program just have to wait. netstat can tell you when it's free.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  3. #3
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,110

    Re: POSIX Socket - Find an open port?

    Sounds a bit like a nonsense to me. A server port (if that's what the OP calls 'open port') is normally designed to serve multiple clients. Even if the server can only handle one connection at a time, clients should be able to connect to the server without any waiting time. The server port won't get in a TIME_WAIT or FIN_WAIT state, only the client port may. An exception would be if two clients on the same machine would bind to the same client socket. But clients would normally not explicitly bind their socket to a specific port.

    HTH,
    Richard

  4. #4
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,553

    Re: POSIX Socket - Find an open port?

    Quote Originally Posted by Richard.J View Post
    Sounds a bit like a nonsense to me.
    Agreement with Richard.J. TCP states like TIME_WAIT apply to connections, not ports. And a connection is uniquely identified by the 5-tuple of the two IP addresses of the endpoints, the two ports of the endpoints, and the protocol (here, TCP). Each endpoint is responsible for maintaining its own view of the state of the connection, which is why one endpoint might get to TIME_WAIT and the other to CLOSE_WAIT.

    The only way that TIME_WAIT might be an issue is if a client tries to reestablish a previously closed connection to a server using the exact same outgoing port number (i.e., client-side port number) to connect to the server's listening port. This is avoided in all network stacks through use of ephemeral ports, unless usage of ephemeral ports is explicitly overridden in the client code (for example, by inappropriate use of bind() on the client side).

    Mike

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: POSIX Socket - Find an open port?

    Well in my defence the OP claimed that it's an issue and that's why I responded as I did.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center