Can two applications listen to the same port?

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

提交回复
热议问题