我相信這裏的問題是使用RandomPort
。每個客戶端和服務器都需要在同一端口上發送/接收才能工作。而且,for ClientIP in ClientIPList(): if ClientIP == IP[0] and data == 'Done':
循環有點多餘且不必要。它可以替換爲if ip[0] in clientIpList:
,並放置在其上方的if data == 'Done':
內。
其他一些想法;不要將名稱與您導入的名稱命名爲相同的名稱(如socket = socket.socket(..)
),因爲那樣您將無法再使用導入的庫。除非客戶端/服務器都在同一個系統上運行或者在同一個子網內運行,否則settimeout(0.5)
是簡短的方法。
我將您的代碼與python socket
documentation中的一些example code合併,並提出了一些可以輕鬆適應您的需求的方法。這裏是腳本;下面粘貼運行服務器和12個客戶端的輸出。
server.py:
#!/usr/bin/python
# server.py
import sys
import socket
import time
HOST = ''
PORT = 50008
CLIENT_IPS = ["10.10.1.11"]
## No longer necessary if the nested loop isn't needed
#class MyException(Exception):
# pass
def main():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((HOST, PORT))
sock.listen(0)
finishedCount = 0
while 1:
data = 'None'
IP = [0, 0]
try:
client, ip = sock.accept()
data = client.recv(1024)
print "%s: Server recieved: '%s'" % (time.ctime(), data)
if data == 'Done':
print "%s: Server sending: 'Thanks'" % time.ctime()
client.send('Thanks')
if ip[0] in CLIENT_IPS:
finishedCount += 1
print "%s: Finished Count: '%d'" % (time.ctime(), finishedCount)
if finishedCount == 12:
#raise MyException
break
except Exception, e:
print "%s: Server Exception - %s" % (time.ctime(), e)
#except MyException:
# print "%s: All clients accounted for. Server ending, goodbye!" % time.ctime()
# break
# close down the socket, ignore closing exceptions
try:
sock.close()
except:
pass
print "%s: All clients accounted for. Server ending, goodbye!" % time.ctime()
if __name__ == '__main__':
sys.exit(main())
client.py:從服務器在同一時間運行
[ 10:52 [email protected] ~/SO/python ]$ for x in {1..12}; do ./client.py $x && sleep 2; done
Fri Nov 18 10:52:44 2011: Client 1: Sending - 'Done'..
Fri Nov 18 10:52:44 2011: Client 1: Recieved - 'Thanks'
Fri Nov 18 10:52:44 2011: Client 1: Finished, goodbye!
Fri Nov 18 10:52:46 2011: Client 2: Sending - 'Done'..
Fri Nov 18 10:52:46 2011: Client 2: Recieved - 'Thanks'
Fri Nov 18 10:52:46 2011: Client 2: Finished, goodbye!
Fri Nov 18 10:52:48 2011: Client 3: Sending - 'Done'..
Fri Nov 18 10:52:48 2011: Client 3: Recieved - 'Thanks'
Fri Nov 18 10:52:48 2011: Client 3: Finished, goodbye!
Fri Nov 18 10:52:50 2011: Client 4: Sending - 'Done'..
Fri Nov 18 10:52:50 2011: Client 4: Recieved - 'Thanks'
Fri Nov 18 10:52:50 2011: Client 4: Finished, goodbye!
Fri Nov 18 10:52:52 2011: Client 5: Sending - 'Done'..
Fri Nov 18 10:52:52 2011: Client 5: Recieved - 'Thanks'
Fri Nov 18 10:52:52 2011: Client 5: Finished, goodbye!
Fri Nov 18 10:52:54 2011: Client 6: Sending - 'Done'..
Fri Nov 18 10:52:54 2011: Client 6: Recieved - 'Thanks'
Fri Nov 18 10:52:54 2011: Client 6: Finished, goodbye!
Fri Nov 18 10:52:56 2011: Client 7: Sending - 'Done'..
Fri Nov 18 10:52:56 2011: Client 7: Recieved - 'Thanks'
Fri Nov 18 10:52:56 2011: Client 7: Finished, goodbye!
Fri Nov 18 10:52:58 2011: Client 8: Sending - 'Done'..
Fri Nov 18 10:52:58 2011: Client 8: Recieved - 'Thanks'
Fri Nov 18 10:52:58 2011: Client 8: Finished, goodbye!
Fri Nov 18 10:53:01 2011: Client 9: Sending - 'Done'..
Fri Nov 18 10:53:01 2011: Client 9: Recieved - 'Thanks'
Fri Nov 18 10:53:01 2011: Client 9: Finished, goodbye!
Fri Nov 18 10:53:03 2011: Client 10: Sending - 'Done'..
Fri Nov 18 10:53:03 2011: Client 10: Recieved - 'Thanks'
Fri Nov 18 10:53:03 2011: Client 10: Finished, goodbye!
Fri Nov 18 10:53:05 2011: Client 11: Sending - 'Done'..
Fri Nov 18 10:53:05 2011: Client 11: Recieved - 'Thanks'
Fri Nov 18 10:53:05 2011: Client 11: Finished, goodbye!
Fri Nov 18 10:53:07 2011: Client 12: Sending - 'Done'..
Fri Nov 18 10:53:07 2011: Client 12: Recieved - 'Thanks'
Fri Nov 18 10:53:07 2011: Client 12: Finished, goodbye!
[ 10:53 [email protected] ~/SO/python ]$
輸出::從運行12個客戶
#!/usr/bin/python
# client.py
import sys
import time
import socket
import random
HOST = '10.10.1.11'
PORT = 50008
def main(n):
while 1:
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send('Done')
print "%s: Client %d: Sending - 'Done'.." % (time.ctime(), n)
data = s.recv(1024)
print "%s: Client %d: Recieved - '%s'" % (time.ctime(), n, data)
if data == 'Thanks':
break
except Exception, e:
print "%s: Client %d: Exception - '%s'" % (time.ctime(), n, e)
time.sleep(2 + random.random() * 5)
finally:
try:
s.shutdown(socket.SHUT_RDWR)
except:
pass
finally:
try:
s.close()
except:
pass
print "%s: Client %d: Finished, goodbye!" % (time.ctime(), n)
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1].isdigit():
sys.exit(main(int(sys.argv[1])))
輸出
[ 10:52 [email protected] ~/SO/python ]$ ./server.py
Fri Nov 18 10:52:44 2011: Server recieved: 'Done'
Fri Nov 18 10:52:44 2011: Server sending: 'Thanks'
Fri Nov 18 10:52:44 2011: Finished Count: '1'
Fri Nov 18 10:52:46 2011: Server recieved: 'Done'
Fri Nov 18 10:52:46 2011: Server sending: 'Thanks'
Fri Nov 18 10:52:46 2011: Finished Count: '2'
Fri Nov 18 10:52:48 2011: Server recieved: 'Done'
Fri Nov 18 10:52:48 2011: Server sending: 'Thanks'
Fri Nov 18 10:52:48 2011: Finished Count: '3'
Fri Nov 18 10:52:50 2011: Server recieved: 'Done'
Fri Nov 18 10:52:50 2011: Server sending: 'Thanks'
Fri Nov 18 10:52:50 2011: Finished Count: '4'
Fri Nov 18 10:52:52 2011: Server recieved: 'Done'
Fri Nov 18 10:52:52 2011: Server sending: 'Thanks'
Fri Nov 18 10:52:52 2011: Finished Count: '5'
Fri Nov 18 10:52:54 2011: Server recieved: 'Done'
Fri Nov 18 10:52:54 2011: Server sending: 'Thanks'
Fri Nov 18 10:52:54 2011: Finished Count: '6'
Fri Nov 18 10:52:56 2011: Server recieved: 'Done'
Fri Nov 18 10:52:56 2011: Server sending: 'Thanks'
Fri Nov 18 10:52:56 2011: Finished Count: '7'
Fri Nov 18 10:52:58 2011: Server recieved: 'Done'
Fri Nov 18 10:52:58 2011: Server sending: 'Thanks'
Fri Nov 18 10:52:58 2011: Finished Count: '8'
Fri Nov 18 10:53:01 2011: Server recieved: 'Done'
Fri Nov 18 10:53:01 2011: Server sending: 'Thanks'
Fri Nov 18 10:53:01 2011: Finished Count: '9'
Fri Nov 18 10:53:03 2011: Server recieved: 'Done'
Fri Nov 18 10:53:03 2011: Server sending: 'Thanks'
Fri Nov 18 10:53:03 2011: Finished Count: '10'
Fri Nov 18 10:53:05 2011: Server recieved: 'Done'
Fri Nov 18 10:53:05 2011: Server sending: 'Thanks'
Fri Nov 18 10:53:05 2011: Finished Count: '11'
Fri Nov 18 10:53:07 2011: Server recieved: 'Done'
Fri Nov 18 10:53:07 2011: Server sending: 'Thanks'
Fri Nov 18 10:53:07 2011: Finished Count: '12'
Fri Nov 18 10:53:07 2011: All clients accounted for. Server ending, goodbye!
[ 10:53 [email protected] ~/SO/python ]$
提示:你不應該使用大寫字母作爲變量的第一個字母。該語法應該總是留給類名稱。問題:爲什麼客戶端在1循環中?爲什麼不只發一次說「我完成了」然後繼續?數據是否第一次發送? – chown
啊 - 很高興知道。謝謝。 – nodapic
我把它放在while循環中,因爲我擔心服務器拒絕連接,因爲他已經處理了其他客戶端。這是我的努力,以確保我們有一個積極的握手... – nodapic