How do multiple applications listen on same port (80)?

12,645

Solution 1

How can that be? Simply...it's not. Only one application will listen on each port. – Adriano Repetti

Right. When Skype listens on those ports before I start my web-server, the server fails. It took me a while to find out why.

Only one app can listen on a socket in a sane way. The OS allows multiple apps to listen on the same port if you specify special options but that's insane. Accepted connections are then dispatched to different applications in an unspecified (i.e. random) way.

IIS can run multiple web-apps on the same port because it opens the port once in kernel mode and dispatches connections to its worker processes.

Solution 2

I do not believe it is ever possible for multiple sockets to listen on the same (TCP) port. If you try to bind a socket to a port that is already open, you will get an error.

I believe Skype gets around the problem you describe by using their own servers as a rendezvous point. The simple explanation being:

  • Alice starts her client, connects to the central server, and informs it of what port she is listening on.
  • Bob starts his client and likewise informs the central server.
  • Now, Alice wants to connect to Bob, but doesn't know which port to send packets to.
  • Alice will then query the central server for Bob's port number.
  • With this information, a direct connection is then established with Bob using that port.

The logic can of course extend to learning the other party's IP address as well as even obtaining public keys.

Note that there's actually a bit more involved with most modern peer-to-peer applications, Skype being no exception. The problem being that most computers are now behind at least one NAT router. Getting two devices each behind their own router to connect to each other is known as NAT traversal - the most common technique having a central coordinating server instruct both clients to simultaneously connect to each other. For more information on this, I recommend Steve Gibson's Security Now!, episode #42

Share:
12,645
ispiro
Author by

ispiro

Updated on June 05, 2022

Comments

  • ispiro
    ispiro almost 2 years

    Many questions relating to port 80 being used have answers saying that there are many programs that use it as their default port. This post mentions some: Skype, IIS, Apache...

    Since only one application can listen on any one port at a time - How can that be? And if the answer is that that's only their default port - how will an application know it has to send information to a different port? For example - if iis will listen on port 81 because Skype is listening on 80 - how will anyone requesting a web page know to send the request to theip:81 as opposed to theip:80?

    My goal is to have a robust way of setting up a connection between programs, when any hard coded port might fail due to some application already listening on it. The port will only need to be used once in order to communicate what dynamic port will be used for the rest of the session. This is a problem for both network connections and for connecting several applications on the same computer.

    Registering with IANA is not always possible, and won't even necessarily solve the problem - someone might still be listening on a registered port. And obviously the solution of "hope for no collisions" - just doesn't cut it.

    (I do understand that a connection has two sockets (and a protocol) and therefore one socket can have multiple connections. My question is about listening on a socket in order to establish the connection.)

    What I would expect, is there to exist some service on the OS (Windows) that I could register my application with, and receive all incoming traffic with some signature - even if it's simply some magic string. Or perhaps some port where multiple applications can listen concurrently - and all would get every incoming message. But I haven't found anything like that so far.

  • ispiro
    ispiro almost 10 years
    You mean that both Alice and Bob initiate a connection with Skype telling it what port they're using. Did I understand you correctly?
  • ispiro
    ispiro almost 10 years
    Thanks. Your anecdote about Skype and the web server drives the point home. I'm not accepting yet, though, on the off chance that someone will come up with an "Aha!" solution.
  • Pacerier
    Pacerier over 8 years
    @ispiro, The standard thread for this topic is: stackoverflow.com/q/14388706/632951