我正在寫一個聊天服務器,並在單元測試時遇到以下問題。在我的一個單元測試中,我將許多測試客戶端連接到我的服務器。當連接的用戶數達到511時,服務器停止響應而沒有任何錯誤消息。在這個階段,所有東西都在PC上本地運行。Twisted:到TCP服務器的客戶端連接數有限?
我準備了一個簡單的服務器,測試客戶端和單元測試代碼來粘貼到論壇中。
任何想法爲什麼服務器掛斷? 任何幫助非常感謝
此代碼基本上是從扭曲的簡單聊天教程。 簡單服務器:
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
class Chat(LineReceiver):
def __init__(self, users, userNum):
self.users = users
self.userNum = userNum
def connectionMade(self):
print "Connected to user %d" % (self.userNum)
self.users[self.userNum] = self
def connectionLost(self, reason):
print "Connection to user %d lost" % (self.userNum)
if self.users.has_key(self.userNum):
del self.users[self.userNum]
def lineReceived(self, line):
for user in self.users:
if user == self.userNum:
continue
self.users[user].sendLine("%d - %s" % (self.userNum, line))
class ChatFactory(Factory):
def __init__(self):
self.users = {} # maps user names to Chat instances
self.nUsers = 0
def buildProtocol(self, addr):
self.nUsers += 1
return Chat(self.users, self.nUsers)
def clientConnectionFailed(self, connector, reason):
print 'connection failed:', reason.getErrorMessage()
reactor.stop()
def clientConnectionLost(self, connector, reason):
print 'connection lost:', reason.getErrorMessage()
reactor.stop()
reactor.listenTCP(8123, ChatFactory())
reactor.run()
這是我的測試客戶端。這個客戶通過單元測試實例化幾次。
import socket
HOST = "localhost"
PORT = 8123
class TestClient:
def __init__(self):
self.connected = False
try:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
print("Socket error %s" % msg)
def connect(self):
try:
self.socket.settimeout(10)
self.socket.connect((HOST, PORT))
self.connected = True
except socket.error, msg:
print("Socket error %s" % msg)
self.connected = False
def disconnect(self):
self.socket.close()
def connected(self):
return self.connected
最後單元測試代碼文件:
import unittest
from TestClient import TestClient
class TestSequenceFunctions(unittest.TestCase):
def test_manyUsers(self):
users = []
number_of_users = 1000
for u in range(number_of_users):
# create client
users.append(TestClient())
# connect client to server
users[-1].connect()
# check connected state
self.assertTrue(users[-1].connected, "User %d is not connected" % (u))
# close connection of all users
for user in users:
user.disconnect()
if __name__ == '__main__':
unittest.main()
這是什麼操作系統?可能是你達到操作系統限制? – 2012-04-07 12:01:40
感謝您的回答。我目前正在研究Windows 7 64位。我不確定服務器在什麼操作系統上運行,什麼時候聯機。我怎樣才能找出操作系統的限制(如果有的話)? – Zoli 2012-04-07 12:04:04