Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 887
ispiro Avatar asked Jun 16 '14 15:06

ispiro


People also ask

Can I use port 80 for multiple applications?

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.

Can multiple applications listen on the same port?

The short answer is “no, not on the same host." The longer answer is that this is by design, the basic rationale being consistency.


2 Answers

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.

like image 128
usr Avatar answered Oct 12 '22 12:10

usr


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

like image 33
lc. Avatar answered Oct 12 '22 12:10

lc.