gpt4 book ai didi

linux - 使用多宿主配置本地虚拟网络

转载 作者:太空宇宙 更新时间:2023-11-04 04:19:57 24 4
gpt4 key购买 nike

我需要测试具有超过 64K 连接的应用程序。我想使用同一个主机。

今天我启动了监听 127.0.0.1 的服务器并从同一台计算机连接到它,但当然它仅限于 64K 连接。

我想模拟一种情况,比如我有一台服务器和许多客户端连接到特定单个 IP 上的单个服务器。

Server Listen: 1.2.3.4

Client Connect to 1.2.3.4 From 2.1.2.1
Client Connect to 1.2.3.4 From 2.1.2.2
Client Connect to 1.2.3.4 From 2.1.2.3
Client Connect to 1.2.3.4 From 2.1.2.4

因此,我需要设置一个具有多宿主的虚拟网络,以便客户端能够从多个地址和监听的服务器进行连接。

如何配置?在 Linux 上?

最佳答案

第 1 步:将地址添加到环回;我会将 10.0.0.1、10.0.0.2、10.0.0.3、10.0.0.4 和 10.0.0.5 添加到 lo

[mpenning@tsunami ~]$ ip addr show lo
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
[mpenning@tsunami ~]$ sudo ip addr add 10.0.0.1/24 dev lo
[mpenning@tsunami ~]$ sudo ip addr add 10.0.0.2/24 dev lo
[mpenning@tsunami ~]$ sudo ip addr add 10.0.0.3/24 dev lo
[mpenning@tsunami ~]$ sudo ip addr add 10.0.0.4/24 dev lo
[mpenning@tsunami ~]$ sudo ip addr add 10.0.0.5/24 dev lo
[mpenning@tsunami ~]$ sudo ip addr show lo
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet 10.0.0.1/24 scope global lo
inet 10.0.0.2/24 scope global secondary lo
inet 10.0.0.3/24 scope global secondary lo
inet 10.0.0.4/24 scope global secondary lo
inet 10.0.0.5/24 scope global secondary lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
[mpenning@tsunami ~]$

第 2 步:Increase max connections

第 3 步:启动大量连接...

from socket import socket, AF_INET, SOCK_STREAM
import select

class Server(object):
def __init__(self, addr="0.0.0.0", port=2525):
self.listen_addr = addr
self.listen_port = port
self.server = socket(AF_INET, SOCK_STREAM)
self.server.setblocking(0)
self.server.bind(self.socket_tuple)
self.server.listen(5)

def __repr__(self):
return "TCP Server listening on %s:%s" % (self.listen_addr,
self.listen_port)

@property
def socket_tuple(self):
return (self.listen_addr, self.listen_port)

def close(self):
self.server.close()

class Client(object):
def __init__(self, addr="0.0.0.0"):
self.listen_addr = addr
self.server_addr = None
self.server_port = None
self.client = socket(AF_INET, SOCK_STREAM)
self.client.setblocking(0)
finished = False
while not finished:
try:
### The point of my answer is here...
self.client.bind((addr,0)) # <- Bind to specific IP and rnd port
finished = True
except:
pass

def __repr__(self):
return "TCP Client %s->%s:%s" % (self.listen_addr,
self.server_addr, self.server_port)

def connect(self, socket_tuple=("0.0.0.0",0)):
self.server_addr = socket_tuple[0]
self.server_port = socket_tuple[1]
self.client.connect_ex(socket_tuple)

def close(self):
self.client.close()

READ_ONLY = select.POLLIN | select.POLLPRI | select.POLLHUP | select.POLLERR
clients = list()
servers = list()
for server_addr, port in [('10.0.0.1', 2525), ('10.0.0.2', 2526)]:
ss = Server(addr=server_addr, port=port)
servers.append(ss)
for client_addr in ['10.0.0.3', '10.0.0.4', '10.0.0.5']:
for ii in xrange(0, 25000):
cc = Client(addr=client_addr)
connection = cc.connect(socket_tuple=ss.socket_tuple)
finished = False
print " %s conns" % len(clients)
while not finished:
poller = select.poll()
poller.register(ss.server, READ_ONLY)
# Map file descriptors to socket objects...
fd_to_socket = {ss.server.fileno(): ss.server,}
events = poller.poll(1)
for fd, flag in events:
s = fd_to_socket[fd]
if flag & (select.POLLIN|select.POLLPRI):
if s is ss.server:
# server socket is ready to accept a connection
connection, client_address = s.accept()
connection.setblocking(0)
fd_to_socket[connection.fileno()] = connection
poller.register(connection, READ_ONLY)
finished = True
clients.append(cc)
print cc

print "Total Clients:", len(clients)
for cc in clients:
cc.close()
for ss in servers:
ss.close()

根据您的喜好调整上述值...但是,您应该记住,调整 Linux 服务器以接受如此多的连接可能具有挑战性...但此时,这不是您要问的问题...

关于linux - 使用多宿主配置本地虚拟网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14136180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com