如何为一台机器分配多个IP地址?这是可能的,这是任何现代操作系统的标准功能。这也是 StackOverflow 上的题外话。在 Linux 中,持久配置它的方式可能非常特定于发行版。 ip addr add $IP_ADDRESS dev $INTERFACE 是通用方法,但在重新启动或网络重新配置后将无法生存。
如何允许应用程序实例使用选定的一个机器地址?这非常简单——您必须将服务器套接字绑定到一个地址。通常您只需提供一个端口号(对于标准 C IPv4 套接字 API,IP 地址设置为 INADDR_ANY),但您可以使用计算机上的任何 IP 地址。详细信息取决于您使用的 API。
I think you are talking about two different problems:
How to assign multiple IP addresses to a single machine? That is possible, that is a standard functionality of any modern operating system. And that is also off-topic on StackOverflow. In Linux the way to persistently configure it can be very distribution-specific. ip addr add $IP_ADDRESS dev $INTERFACE is the general way, but won't survive a reboot or network reconfiguration.
How to allow an application instance use a selected one of the machine addresses? That is quite easy – you must bind your server socket to an address. Usually you will only provide a port number (the IP address set to INADDR_ANY for the standard C IPv4 socket API), but you can use any IP address on the machine there. Details depend on the API you are using.
如果您正在进行面向未来的编程,您的程序将处理 IPv4 和 IPv6。在这种情况下,您可以在一台 PC 上拥有多个 IPv6 地址,并且可以告诉每个实例绑定到不同的地址。
If you are programming future-oriented, your program deals with IPv4 as well as IPv6. In this case, you can have several IPv6 addresses on one PC and maybe tell each instance to bind to a distinct address.
发布评论
评论(3)
我认为你正在谈论两个不同的问题:
如何为一台机器分配多个IP地址?这是可能的,这是任何现代操作系统的标准功能。这也是 StackOverflow 上的题外话。在 Linux 中,持久配置它的方式可能非常特定于发行版。
ip addr add $IP_ADDRESS dev $INTERFACE
是通用方法,但在重新启动或网络重新配置后将无法生存。如何允许应用程序实例使用选定的一个机器地址?这非常简单——您必须将服务器套接字绑定到一个地址。通常您只需提供一个端口号(对于标准 C IPv4 套接字 API,IP 地址设置为 INADDR_ANY),但您可以使用计算机上的任何 IP 地址。详细信息取决于您使用的 API。
I think you are talking about two different problems:
How to assign multiple IP addresses to a single machine? That is possible, that is a standard functionality of any modern operating system. And that is also off-topic on StackOverflow. In Linux the way to persistently configure it can be very distribution-specific.
ip addr add $IP_ADDRESS dev $INTERFACE
is the general way, but won't survive a reboot or network reconfiguration.How to allow an application instance use a selected one of the machine addresses? That is quite easy – you must bind your server socket to an address. Usually you will only provide a port number (the IP address set to
INADDR_ANY
for the standard C IPv4 socket API), but you can use any IP address on the machine there. Details depend on the API you are using.我们通过使用一些虚拟机解决了这个问题。每个虚拟机都有自己的 IP,它的行为就像一个独立的操作系统。
//编辑:
VirtualBox是我最喜欢的:http://www.virtualbox.org/
We solved that by using some Virtual Machines.. Every VM has its own IP and it behaves like a standalone OS.
//Edit:
VirtualBox is my favourite: http://www.virtualbox.org/
如果您正在进行面向未来的编程,您的程序将处理 IPv4 和 IPv6。在这种情况下,您可以在一台 PC 上拥有多个 IPv6 地址,并且可以告诉每个实例绑定到不同的地址。
If you are programming future-oriented, your program deals with IPv4 as well as IPv6. In this case, you can have several IPv6 addresses on one PC and maybe tell each instance to bind to a distinct address.