0
我有一個python套接字服務器/客戶端實現。服務器從隊列中讀取數據並將數據推送到客戶端。python隊列讀取相同的數據
客戶端讀取並顯示它。當我運行這段代碼時,客戶端總是隻顯示隊列中的前10次。每次同樣的10次都發送給客戶。
這是我在客戶端的數據。
Client2 received data: "test-msg---5", "test-msg---6", "test-msg---7", "test-msg---8", "test-msg---9"]["test-msg---0", "test-msg---1", "test-msg---2", "test-msg---3", "test-msg---4", "test-msg---5", "test-msg---6", "test-msg---7", "test-msg---8", "test-msg---9"]["test-msg---0", "test-msg---1", "test-msg---2", "test-msg---3", "test-msg---4", "test-msg---5", "test-msg---6", "test-msg---7", "test-msg---8", "test-msg---9"]["test-msg---0", "test-msg---1", "test-msg---2", "test-msg---3", "test-msg---4", "test-msg---5", "test-msg---6", "test-msg---7", "test-msg---8", "test-msg---9"]["test-msg---0", "test-msg---1", "test-msg---2", "test-msg---3", "test-msg---4", "test-msg---5", "test-msg---6", "test-msg---7", "test-msg---8", "test-msg---9"]["test-msg---0", "test-msg---1", "test-msg---2", "test-msg---3", "test-msg---4", "test-msg---5", "test-msg---6", "test-msg---7", "test-msg---8", "test-msg---9"]["test-msg---0", "test-msg---1", "test-msg---2", "test-msg---3", "test-msg---4", "test-msg---5", "test-msg---6", "test-msg---7", "test-msg---8", "test-msg---9"]["test-msg---0", "test-msg---1", "test-msg---2", "test-msg---3", "test-msg---4", "test-msg---5", "test-msg---6", "test-msg---7", "test-msg---8", "test-msg---9"]["test-msg---0", "test-msg---1", "test-msg---2", "test-msg---3", "test-msg---4", "test-msg---5", "test-msg---6", "test-msg---7", "test-msg---8", "test-msg---9"]["test-msg---0", "test-msg---1", "test-msg---2", "test-msg---3", "test-msg---4", "test-msg---5", "test-msg---6", "test-msg---7", "test-msg---8", "test-msg---9"]["test-msg---0", "test-msg---1", "test-msg---2", "test-msg---3", "test-msg---4", "test-msg---5", "test-msg---6", "test-msg---7", "test-msg---8", "test-msg---9"]["test-msg---0", "test-msg---1", "test-msg---2", "test-msg---3", "test-msg---4", "test-msg---5", "test-msg---6", "test-msg---7", "test-msg---8", "test-msg---9"]["test-msg---0", "test-msg---1", "test-msg---2", "test-msg---3", "test-msg---4", "test-msg---5", "test-msg---6", "test-msg---7", "test-msg---8", "test-msg---9"]
Server代碼: -
# server2.py
import socket
from threading import Thread
from SocketServer import ThreadingMixIn
import Queue
import json
TCP_IP = 'localhost'
TCP_PORT = 9999
BUFFER_SIZE = 1024
q = Queue.Queue()
for i in range(50000):
print 'put data'
q.put("test-msg---" + str(i))
def queue_get_all(q):
items = []
maxItemsToRetreive = 10
for numOfItemsRetrieved in range(0, maxItemsToRetreive):
try:
if numOfItemsRetrieved == maxItemsToRetreive:
break
items.append(q.get_nowait())
except Empty, e:
print 'Queue empty'
return items
class ClientThread(Thread):
def __init__(self,ip,port,sock):
Thread.__init__(self)
self.ip = ip
self.port = port
self.sock = sock
print " New thread started for "+ip+":"+str(port)
def run(self):
# filename='mytext.txt'
# f = open(filename,'rb')
while True:
# l = f.read(BUFFER_SIZE)
l = queue_get_all(q)
while (l):
self.sock.sendall(json.dumps(l))
#print('Sent ',repr(l))
# l = f.read(BUFFER_SIZE)
if not l:
f.close()
self.sock.close()
break
tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpsock.bind((TCP_IP, TCP_PORT))
threads = []
while True:
tcpsock.listen(5)
print "Waiting for incoming connections..."
(conn, (ip,port)) = tcpsock.accept()
print 'Got connection from ', (ip,port)
newthread = ClientThread(ip,port,conn)
newthread.start()
threads.append(newthread)
for t in threads:
t.join()
客戶端代碼
# Python TCP Client A
import socket
host = socket.gethostname()
host = 'localhost'
port = 9999
BUFFER_SIZE = 2000
MESSAGE = raw_input("tcpClientA: Enter message/ Enter exit:")
tcpClientA = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpClientA.connect((host, port))
while MESSAGE != 'exit':
# tcpClientA.send(MESSAGE)
data = tcpClientA.recv(BUFFER_SIZE)
print " Client2 received data:", data
MESSAGE = raw_input("tcpClientA: Enter message to continue/ Enter exit:")
tcpClientA.close()