如何监听多个IP地址? [英] How to listen on multiple IP addresses?

查看:88
本文介绍了如何监听多个IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的服务器分配了多个IP地址,并且我想听其中的一些(或全部),我该如何去做?

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?

我需要为每个IP地址创建一个新的套接字并绑定它吗?我可以将多个IP地址绑定到单个套接字吗?IPAddress.Any是否侦听所有IP地址?MSDN库在此问题上还不清楚.

Do I need to create a new socket for each IP address, and bind it? Can i bind multiple ip addresses to a single socket? Does IPAddress.Any listen on all IP addresses? The MSDN library is very unclear on this matter.

推荐答案

您不能将单个套接字绑定到多个端点.第二次您为给定套接字调用 Bind()时,发生 SocketException (无效的参数错误).

You cannot bind a single socket to multiple endpoints. A SocketException (invalid argument error) occurs the second time you call Bind() for a given socket.

正如其他人所说,您可以使用 IPAddress.Any 来侦听本地计算机上的IPv4地址.但是,如果您只想侦听可用IP地址的子集,则必须创建单独的套接字.

As others have said, you can use IPAddress.Any to listen to the IPv4 addresses on the local machine. However, if you only want to listen on a subset of the available IP addresses, you'll have to create separate sockets.

这篇关于如何监听多个IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆