13

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
  • 23,513
  • 30
  • 116
  • 236
  • _How can that be?_ Simply...it's not. Only one application will listen on each port. _How will anyone...know...?_ Configuration. – Adriano Repetti Jun 16 '14 at 15:22
  • The short answer is that the IIS web site that listens to port 80 will not start if skype is already open and listening to port 80. It does not change to 81 or something else, you need to either change it manually or stop the skype process. – Andrew Jun 16 '14 at 15:23
  • Is server and client port relevant for the answer of that question? – Mare Infinitus Jun 16 '14 at 15:48

4 Answers4

3

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.

usr
  • 162,013
  • 33
  • 219
  • 345
  • 1
    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. – ispiro Jun 16 '14 at 20:06
  • @ispiro, The standard thread for this topic is: http://stackoverflow.com/q/14388706/632951 – Pacerier Sep 18 '15 at 09:52
1

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

lc.
  • 105,606
  • 20
  • 147
  • 176
  • 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 Jun 16 '14 at 15:25
0

on the port forwarding setup page i just put a comma and add an other port to open. it wont allow you to set up an additional rule with listen port 80 but it will allow you to trigger multiple ports with that one listen port

-1
  • For TCP, you can only have one application listening on a single port at one time. Now if you had 2 network cards or created a virtual interface, you could have one application listen on the first IP and the second one on the second IP using the same port number.
  • For UDP (Multicasts), multiple applications can subscribe to the same port.

one application listening on a single port that's the reason why ports exist. To allow multiple applications to share the network without conflicts.

But there are ways to do what you requested:

  • You could write a master process, which possesses the port and notifies slave processes using some separation logic.
  • On Linux and BSD you can set up remapping rules that redirect packets from the visible port to different ones(such as listener app), again by using some separation logic(e.g. redirect according to network origin etc.).

Note: For TCP, you multiple applications can listen on the same socket by using SO_REUSEADDR option before binding but what this does is redirect the incoming connection to only one of the listeners.

Tamer Tas
  • 3,282
  • 10
  • 21