CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: TCP socket problem - IP:Port already in use.

  1. #1
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    TCP socket problem - IP:Port already in use.

    Hello everyone, thanks for reading!


    I have a little question about TCP sockets and how to define which port will be used for the connection. So far I just defined port 3000 as a default port and I have a local service running (client) that connects using (localhost):3000 to my server. Since the socket depends on source IP, source Port, destination IP and destination Port it's pretty unlikely the combination will repeat. However, unlikely doesn't mean impossible, and of course it happened. It so happens that TeamViewer (a remote desktop app) also seems to use a local socket and (yes!) it's also using port 3000...
    The result is a SocketException when I try to open my local TcpListener (Only one usage of each socket address (protocol/network address/port) is normally permitted)


    Now, it would be easy to just try to open the listener on say 3001 or 3002 or whatever port is available, but
    1st, that would mean that the client side (may be local or remote) will have to cycle through a range of ports to connect to the server, which would maybe mean my client will take a long time to connect (in computer times) and
    2nd, how big a range should I set to avoid the (veeeeery unlikely I know, but still possible) scenario where they are also in use by other apps? A small range would mean a faster cycling of the options, a large range would prevent possible collisions with other apps using the same port.


    Is there a protocol for knowing which port someone might be using? Or how I should set my server/client sides to find each other? I've been reading some TCP standards and I could not find much so far, other than a first handshake and then a port switch like FTP, but that would mean a connection is already established.


    Any ideas? Thanks for any help!

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,710

    Re: TCP socket problem - IP:Port already in use.

    I would start with reading the common info about socket port numbers:
    List of TCP and UDP port numbers
    Victor Nijegorodov

  3. #3
    Join Date
    Mar 2001
    Posts
    2,527

    Re: TCP socket problem - IP:Port already in use.

    So why not just use a different port if your configuration already uses 3000. Either that or find a way to change the port of TeamViewer. There must be a configuration method.
    ahoodin
    To keep the plot moving, that's why.

  4. #4
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    Re: TCP socket problem - IP:Port already in use.

    Victor, the TCP/UDP port list is where I got the 1024-65535 port range that should be used for user applications. That's why I decided to use port 3000. There is no specification on which one should be used and nothing guarantees it will be available. In fact, if you look at that list, you'll see many ports are "taken" and sometimes more than one application use them. Even if I decide to select a number that is not taken from that list, that does not guarantee it will always be available. If some other app decides to start using it tomorrow and my end users so happen to need that application, my app won't connect. This is not a problem of availability, it's a problem of collision prevention and how to resolve it. I have one solution in mind (first post) but it means possible long connection times which I would like to avoid.

    Ahoodin, changing the port is not the problem and I'm sure I could change TeamViewer too, but that doesn't mean the new selected number will be a safe choice. And as I stated in my first post, I could try opening my TcpListener on port 3000, 3001, 3002 until one succeeds, but that would mean the predefined port will have changed and my clients will have to cycle through an also predefined range of ports to try to connect to the host. As far as I know, there's no way of letting the clients know which was the successful port since no connection is made. Hence, my question, is there a better way to do this? Or is it just a brute force trial and error procedure?

    Thank you for your help!

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,710

    Re: TCP socket problem - IP:Port already in use.

    Quote Originally Posted by Nikel View Post
    Victor, the TCP/UDP port list is where I got the 1024-65535 port range that should be used for user applications.
    Why 1024 - ...?
    The range of port numbers from 1024 to 49151 are the registered ports. They are assigned by IANA for specific service upon application by a requesting entity.[1] On most systems, registered ports can be used by ordinary users.
    ...
    The range 49152–65535 (215+214 to 216−1) contains dynamic or private ports that cannot be registered with IANA.[174] This range is used for private, or customized services or temporary purposes and for automatic allocation of ephemeral ports.
    Victor Nijegorodov

  6. #6
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    Re: TCP socket problem - IP:Port already in use.

    Tell that to TeamViewer lol... True nevertheless, so I'll change my default to a port number >49152. That still won't contain a socket collition though.

  7. #7
    Join Date
    Mar 2001
    Posts
    2,527

    Re: TCP socket problem - IP:Port already in use.

    I suppose you could publish your server port in the ftp directory and clients could download a file that describes the port.
    ahoodin
    To keep the plot moving, that's why.

Tags for this Thread

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




On-Demand Webinars (sponsored)