Can two applications listen to the same port?

前端 未结 17 915
天涯浪人
天涯浪人 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.

提交回复
热议问题