Can two applications listen to the same port?

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

    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.

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

    Short answer:

    Going by the answer given here. You can have two applications listening on the same IP address, and port number, so long one of the port is a UDP port, while other is a TCP port.

    Explanation:

    The concept of port is relevant on the transport layer of the TCP/IP stack, thus as long as you are using different transport layer protocols of the stack, you can have multiple processes listening on the same <ip-address>:<port> combination.

    One doubt that people have is if two applications are running on the same <ip-address>:<port> combination, how will a client running on a remote machine distinguish between the two? If you look at the IP layer packet header (https://en.wikipedia.org/wiki/IPv4#Header), you will see that bits 72 to 79 are used for defining protocol, this is how the distinction can be made.

    If however you want to have two applications on same TCP <ip-address>:<port> combination, then the answer is no (An interesting exercise will be launch two VMs, give them same IP address, but different MAC addresses, and see what happens - you will notice that some times VM1 will get packets, and other times VM2 will get packets - depending on ARP cache refresh).

    I feel that by making two applications run on the same <op-address>:<port> you want to achieve some kind of load balancing. For this you can run the applications on different ports, and write IP table rules to bifurcate the traffic between them.

    Also see @user6169806's answer.

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

    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.

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

    You can have one application listening on one port for one network interface. Therefore you could have:

    1. httpd listening on remotely accessible interface, e.g. 192.168.1.1:80
    2. another daemon listening on 127.0.0.1:80

    Sample use case could be to use httpd as a load balancer or a proxy.

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

    When you create a TCP connection, you ask to connect to a specific TCP address, which is a combination of an IP address (v4 or v6, depending on the protocol you're using) and a port.

    When a server listens for connections, it can inform the kernel that it would like to listen to a specific IP address and port, i.e., one TCP address, or on the same port on each of the host's IP addresses (usually specified with IP address 0.0.0.0), which is effectively listening on a lot of different "TCP addresses" (e.g., 192.168.1.10:8000, 127.0.0.1:8000, etc.)

    No, you can't have two applications listening on the same "TCP address," because when a message comes in, how would the kernel know to which application to give the message?

    However, you in most operating systems you can set up several IP addresses on a single interface (e.g., if you have 192.168.1.10 on an interface, you could also set up 192.168.1.11, if nobody else on the network is using it), and in those cases you could have separate applications listening on port 8000 on each of those two IP addresses.

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

    You can make two applications listen for the same port on the same network interface.

    There can only be one listening socket for the specified network interface and port, but that socket can be shared between several applications.

    If you have a listening socket in an application process and you fork that process, the socket will be inherited, so technically there will be now two processes listening the same port.

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