Can two applications listen to the same port?

前端 未结 17 881
天涯浪人
天涯浪人 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:30

    Just to share what @jnewton mentioned. I started an nginx and an embedded tomcat process on my mac. I can see both process runninng at 8080.

    LT<XXXX>-MAC:~ b0<XXX>$ sudo netstat -anp tcp | grep LISTEN
    tcp46      0      0  *.8080                 *.*                    LISTEN     
    tcp4       0      0  *.8080                 *.*                    LISTEN   
    
    0 讨论(0)
  • 2020-11-22 04:34

    If at least one of the remote IPs is already known, static and dedicated to talk only to one of your apps, you may use iptables rule (table nat, chain PREROUTING) to redirect incomming traffic from this address to "shared" local port to any other port where the appropriate application actually listen.

    0 讨论(0)
  • Yes.

    From this article:
    https://lwn.net/Articles/542629/

    The new socket option allows multiple sockets on the same host to bind to the same port

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

    If by applications you mean multiple processes then yes but generally NO. For example Apache server runs multiple processes on same port (generally 80).It's done by designating one of the process to actually bind to the port and then use that process to do handovers to various processes which are accepting connections.

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

    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.

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