Can two applications listen to the same port?

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

提交回复
热议问题