0

我需要测试一个连接数超过 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 上?

4

1 回答 1

0

第 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 步:增加最大连接数

第 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 服务器以接受如此多的连接可能具有挑战性...但此时,这不是您要问的问题...

于 2013-01-03T11:20:08.587 回答