How to listen on multiple IP addresses?

后端 未结 6 1203
别那么骄傲
别那么骄傲 2021-02-15 16:15

If my server has multiple IP addresses assigned to it, and I would like to listen to some (or all) of them, how do I go about doing that?

Do I need to create a new socke

6条回答
  •  青春惊慌失措
    2021-02-15 17:00

    If you want to listen on all IPv4 and IPv6 addresses, use this code:

    var listener = new TcpListener(IPAddress.IPv6Any, port);
    listener.Server.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
    

    IPv6Any tells Windows to listen on the IPv6 stack. Setting the socket option to false tells Windows to not limit itself to the IPv6 stack, but rather to also listen on the IPv4 stack. The default is to only listen on the stack explicitly specified.

提交回复
热议问题