Can two applications listen to the same port?

342,449

Solution 1

The answer differs depending on what OS is being considered. In general though:

For TCP, no. You can only have one application listening on the same port at one time. Now if you had 2 network cards, 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.

Edit: Since Linux Kernel 3.9 and later, support for multiple applications listening to the same port was added using the SO_REUSEPORT option. More information is available at this lwn.net article.

Solution 2

Yes (for TCP) you can have two programs listen on the same socket, if the programs are designed to do so. When the socket is created by the first program, make sure the SO_REUSEADDR option is set on the socket before you bind(). However, this may not be what you want. What this does is an incoming TCP connection will be directed to one of the programs, not both, so it does not duplicate the connection, it just allows two programs to service the incoming request. For example, web servers will have multiple processes all listening on port 80, and the O/S sends a new connection to the process that is ready to accept new connections.

SO_REUSEADDR

Allows other sockets to bind() to this port, unless there is an active listening socket bound to the port already. This enables you to get around those "Address already in use" error messages when you try to restart your server after a crash.

Solution 3

Yes.

  1. Multiple listening TCP sockets, all bound to the same port, can co-exist, provided they are all bound to different local IP addresses. Clients can connect to whichever one they need to. This excludes 0.0.0.0 (INADDR_ANY).

  2. Multiple accepted sockets can co-exist, all accepted from the same listening socket, all showing the same local port number as the listening socket.

  3. Multiple UDP sockets all bound to the same port can all co-exist provided either the same condition as at (1) or they have all had the SO_REUSEADDR option set before binding.

  4. TCP ports and UDP ports occupy different namespaces, so the use of a port for TCP does not preclude its use for UDP, and vice versa.

Reference: Stevens & Wright, TCP/IP Illustrated, Volume II.

Solution 4

In principle, no.

It's not written in stone; but it's the way all APIs are written: the app opens a port, gets a handle to it, and the OS notifies it (via that handle) when a client connection (or a packet in UDP case) arrives.

If the OS allowed two apps to open the same port, how would it know which one to notify?

But... there are ways around it:

  1. As Jed noted, you could write a 'master' process, which would be the only one that really listens on the port and notifies others, using any logic it wants to separate client requests.
    • On Linux and BSD (at least) you can set up 'remapping' rules that redirect packets from the 'visible' port to different ones (where the apps are listening), according to any network related criteria (maybe network of origin, or some simple forms of load balancing).

Solution 5

Yes Definitely. As far as i remember From kernel version 3.9 (Not sure on the version) onwards support for the SO_REUSEPORT was introduced. SO_RESUEPORT allows binding to the exact same port and address, As long as the first server sets this option before binding its socket.

It works for both TCP and UDP. Refer to the link for more details: SO_REUSEPORT

Note: Accepted answer no longer holds true as per my opinion.

Share:
342,449
nadiv
Author by

nadiv

Updated on August 21, 2020

Comments

  • nadiv
    nadiv over 3 years

    Can two applications on the same machine bind to the same port and IP address? Taking it a step further, can one app listen to requests coming from a certain IP and the other to another remote IP? I know I can have one application that starts off two threads (or forks) to have similar behavior, but can two applications that have nothing in common do the same?

  • user1066101
    user1066101 over 14 years
    "one application listening on a single port" that's the reason why ports exist -- to allow multiple applications to share the network without conflicts.
  • nadiv
    nadiv over 14 years
    both are of interest to me. The platform is windows, but if the answer is different for Linux, it would be nice to know
  • Jed Smith
    Jed Smith over 14 years
    iptables -m statistic --mode random --probability 0.5 is fun.
  • John M
    John M over 14 years
    One listener per port per IP address. Adding another network interface is a way to get a second IP address. Your platform probably supports virtual interfaces which is another way to get two IP addresses with one physical network card.
  • Samuel
    Samuel over 13 years
    What exactly signify "Opens a port"? I understand the sentence but do you know what exactly the system do when it open a port and handle it? I know that when you want to open a port with TCP, you get a stream and that stream is your connection with the remote but I search on the web and don't found a very good explanation.
  • nullException
    nullException over 13 years
    @Samuel: opening a port (in server mode) means getting a file descriptor, and when the system gets a SYN packet to that port number, responds with SYN+ACK and generates an event on the associated file descriptor. the application responds to that event with an accept() call, which creates a new file descriptor associated to the specific stream, leaving the original server descriptor free to get new connections from clients
  • user207421
    user207421 over 11 years
    This answer cannot be considered correct. It entirely overlooks the existence of both SO_REUSEADDR and SO_REUSEPORT.
  • dpb
    dpb about 11 years
    TCP + UDP now works (given a new enough kernel). See the link I added to the answer.
  • Eugen
    Eugen almost 11 years
    Although I was of the same opinion until now, it turns out I was able to bind two different processes to same ip and TCP port! This is possible if you set ServerSocket.setReuseAddress(true) in Java before binding to it. Really unexpected behaviour.
  • John McCarthy
    John McCarthy almost 11 years
    @Eugen also note Java bug 7179799 where this can happen on Windows with multiple versions of Java.
  • user207421
    user207421 almost 11 years
    This answer is not correct unless all the sockets are bound to distinct IP addresses none of which is INADDR_ANY, or unless you are on Windows, where the result is undefined.
  • trusktr
    trusktr over 10 years
    @Eugen, Chris Dail, If a TCP app is using a port, there's no guarantee that another app can not also listen/interact on the same port? In other words, if apache (httpd) is listening on port 80, this does not guarantee that some other malicious app will not listen/interact on port 80? And what about with UDP? Can a malicious app intercept UDP communication of another app using the same port?
  • trusktr
    trusktr over 10 years
    Can you expand on how the data goes to a specific app on the same port? Are there any security concerns to think about when apps use SO_REUSEADDR or SO_REUSEPORT?
  • trusktr
    trusktr over 10 years
    @EJP Can you also take a look at my previous comment?
  • abeyer
    abeyer over 10 years
    The question is about linux, but the answer here is pretty thorough and covers others: stackoverflow.com/a/14388707/230537
  • user207421
    user207421 over 10 years
    If a client connects to IP1:port it talks to the socket which is listening at IO1:port. Similarly for IP2:port etc.
  • user207421
    user207421 over 10 years
    (1) The actual meaning of your answer is 'For TCP, yes, provided ...' (2) Multicast is not a precondition for UDP port sharing, but SO_REUSEADDR is.
  • user207421
    user207421 over 10 years
    There is no such thing a s a multicast socket. There are UDP sockets. Multicast is not a precondition for SO_REUSEADDR.
  • Gokul E
    Gokul E over 10 years
    NOTE you said that initially TWO Programs can LISTEN but cannot start a process/separate memory address rite? the question is about starting or having two processes in a Same Port which is not possible at all.. HENCE a service can be started on a port and one or more clients can listen to the service to that port...
  • user207421
    user207421 over 10 years
    @Gokul Servers listen. Clients connect. Don't confuse the issue by misusing standard terminology.
  • Gokul E
    Gokul E over 10 years
    @EJP yes.. and hopefully I want an answer for the difference between Listening and Connecting.. May be i was bit confused .. am sorry... I thought that when two programs listen to a port then the connectivity will be confused for the client to which program is serving at the port!!!.. is it so? am sorry if i asked wrong..
  • Wolf
    Wolf over 10 years
    have you a link at hand? The opportunity of TCP-UDP coexistence is my very question. Thanks in advance:)
  • user207421
    user207421 over 10 years
    @Wolf Just try it. That's all the proof you really need. My citation is Stevens & Wright: you can't get much better than that.
  • Wolf
    Wolf over 10 years
    Thanks for the response, I need to read even more attentive. You already wrote that UDP and TCP can coexist.
  • warvariuc
    warvariuc over 9 years
    @trusktr, I think he meant this
  • Yang Juven
    Yang Juven over 9 years
    For UDP (Multicasts), multiple applications can subscribe to the same port. If one packet has arrived from client, which application receive it?
  • Maria Ines Parnisari
    Maria Ines Parnisari over 9 years
    Hi everyone, what can I read to dig deeper into this topic? Specifically, I want to understand the 'why' behind what @JohnM said: "One listener per port per IP address".
  • Bruno
    Bruno almost 9 years
    SO_REUSEADDR certainly doesn't let you have two TCP sockets in listening state at the same time, at least on Unix. It's meant to get around the TIME_WAIT state: unixguide.net/network/socketfaq/4.5.shtml . It might work on Windows, but you're not guaranteed that the request will reach the right server anyway).
  • Pacerier
    Pacerier over 8 years
    @Chris, This answer is misleading and should be edited / deleted.
  • Aequitas
    Aequitas over 8 years
    @Chris and what happens if two apps are listening on the same port, I have a memdump where 9 local addresses are listening on the same port
  • user207421
    user207421 about 8 years
    @GokulEzhumalai The difference is that servers listen and clients connect. I've already said that.
  • alexchandel
    alexchandel over 7 years
    2 network cards are not necessary to have 2 IP addresses. Simply have the kernel listen on a second IP address on that interface. You can have as many IP addresses as you want on an interface.
  • Renan C
    Renan C over 7 years
    Did you have a source about this? I need to my article. Thanks
  • Sahil Singh
    Sahil Singh about 7 years
    Nice link, however do not this line written there - The SO_REUSEPORT option is non-standard
  • Shaun Wilson
    Shaun Wilson about 7 years
    Windows has port sharing features. Under Linux you can (unsafely) share the fd (socket descriptor/handle) but it won't really function as a shared port would under Windows. This answer is not entirely true except according to strict "BSD Sockets" which does not define any such sharing. In the case where multiple processes are listening on the same port, when configured to do so, the most-recent listener is the one which receives connections/clients/sockets.
  • Staszek
    Staszek over 6 years
    Totally true. If it was not true, how Wireshark could work?
  • user207421
    user207421 almost 6 years
    @Staszek Wireshark doesn't listen to ports. It operates at the packet level.
  • Staszek
    Staszek almost 6 years
    Oh, that would make sense. Anyway, listening two ports by 2 apps is surely possible.
  • soger
    soger over 5 years
    This is not exactly true, see JNewton's answer.
  • soger
    soger over 5 years
    Yes, thank you, I had no idea what was going on until I realized that two instances of apache was running one with the old and one with the new configuration and I was receiving random responses.
  • user207421
    user207421 over 4 years
    @Javier No it doesn't. Opening a port from the point of view of the server application occurs when you bind the listening socket, or rather bind the socket you are about to listen() on. More probably the question is about opening it in the firewall. Far too many errors here, and all uncorrected in 7 years. Answer also omits the case of binding to different local address with the same port number. It is in fact totally incorrect.
  • Harvey Lin
    Harvey Lin over 4 years
    Is there such a program on windows? I need to have both my local IIS server and ActiveMQ broker listen on port 443
  • jpell
    jpell over 4 years
    @YangJuven what is the answer to your question?
  • tga
    tga over 3 years
    Thanks, would never know why the http server lib I'm using is allowing binding same ports if it's not this answer.
  • dbush
    dbush almost 3 years
    @YangJuven All sockets will receive a multicast packet. For unicast UDP, the behavior depends on the OS.
  • ZekeC
    ZekeC over 2 years
    This is not correct! You can have two applications running on the same port as long as the PATHS to those applications are different.