Can two applications listen to the same port?

前端 未结 17 901
天涯浪人
天涯浪人 2020-11-22 03:50

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 othe

相关标签:
17条回答
  • 2020-11-22 04:14

    I have tried the following, with socat:

    socat TCP-L:8080,fork,reuseaddr -
    

    And even though I have not made a connection to the socket, I cannot listen twice on the same port, in spite of the reuseaddr option.

    I get this message (which I expected before):

    2016/02/23 09:56:49 socat[2667] E bind(5, {AF=2 0.0.0.0:8080}, 16): Address already in use
    
    0 讨论(0)
  • 2020-11-22 04:15

    No. Only one application can bind to a port at a time, and behavior if the bind is forced is indeterminate.

    With multicast sockets -- which sound like nowhere near what you want -- more than one application can bind to a port as long as SO_REUSEADDR is set in each socket's options.

    You could accomplish this by writing a "master" process, which accepts and processes all connections, then hands them off to your two applications who need to listen on the same port. This is the approach that Web servers and such take, since many processes need to listen to 80.

    Beyond this, we're getting into specifics -- you tagged both TCP and UDP, which is it? Also, what platform?

    0 讨论(0)
  • 2020-11-22 04:15

    Another way is use a program listening in one port that analyses the kind of traffic (ssh, https, etc) it redirects internally to another port on which the "real" service is listening.

    For example, for Linux, sslh: https://github.com/yrutschle/sslh

    0 讨论(0)
  • 2020-11-22 04:17

    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.

    0 讨论(0)
  • 2020-11-22 04:20

    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).
    0 讨论(0)
  • 2020-11-22 04:20

    Yes and no. Only one application can actively listen on a port. But that application can bequeath its connection to another process. So you could have multiple processes working on the same port.

    0 讨论(0)
提交回复
热议问题