我正在嘗試創建一個簡單的服務器和客戶端程式。客戶端將從服務器請求時間同步,服務器將用當前的紀元時間回答。
我正在嘗試將服務器實作為多執行緒。當我為單執行緒做的時候它作業得很好,但現在我認為不起作用,因為我不斷收到以下訊息:
第 21 行,運行 connectionSocket.send(ts.encode())
BrokenPipeError: [Errno 32] 管道損壞
這是我的代碼
客戶 1:
from socket import *
serverName = '127.0.0.1'
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName, serverPort)) #handshaking between client and server
sentence = 'Hey Server, what is the current time?'
print(sentence)
clientSocket.send(sentence.encode())
currentTime = clientSocket.recv(1024)
print('From Server: ', currentTime.decode())
clientSocket.close()
多執行緒服務器
from threading import Thread
from socketserver import ThreadingMixIn
import calendar
import time
from socket import *
class ClientThread(Thread):
def __init__(self,ip,port):
Thread.__init__(self)
self.ip = ip
self.port = port
print ("New server socket thread started for " ip " : " str(port))
def run(self):
while True :
connectionSocket.recv(2048)
ts = calendar.timegm(time.gmtime())
ts = str(ts)
connectionSocket.send(ts.encode())
#connectionSocket.close() #should I close????
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('', serverPort))
#serverSocket.listen(1)
threads = []
#print('The server is ready to receive')
while True:
serverSocket.listen(1) #should this be inside or outside the loop????
print('The server is ready to receive') #and this?????
(connectionSocket, (ip,port)) = serverSocket.accept()
newthread = ClientThread(ip,port)
newthread.start()
threads.append(newthread)
for t in threads:
t.join()
uj5u.com熱心網友回復:
我最好的猜測是您缺少return服務器腳本中的陳述句。它需要更多修復,但這應該可以作業 - 運行以下代碼:
客戶
from socket import *
serverName = '127.0.0.1'
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
try:
clientSocket.connect((serverName, serverPort))
sentence = 'Hey Server, what is the current time?'
print('Data to send:\n\t', sentence)
clientSocket.send(sentence.encode())
currentTime = clientSocket.recv(1024)
print('Received data:\n\t', currentTime.decode())
except Exception as exc:
print(exc)
finally:
clientSocket.close()
服務器
from threading import Thread
import calendar
import time
from socket import *
class ClientThread(Thread):
def __init__(self, ip, port):
Thread.__init__(self)
self.ip = ip
self.port = port
print("New server socket thread started for " ip ":" str(port))
def run(self):
while True :
print('Receiving data from a client')
data = connectionSocket.recv(2048) # if data is comming to the server, code will go further than this line
print('Received data:\n\t', data)
ts = calendar.timegm(time.gmtime())
ts = str(ts)
print('Sending a data:\n\t', ts)
connectionSocket.send(ts.encode())
return
serverPort = 12000
threads = []
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('', serverPort))
serverSocket.listen(1) # can accept and be connected to one connection at a time
while True:
print('The server is ready to receive')
(connectionSocket, (ip, port)) = serverSocket.accept()
newthread = ClientThread(ip, port)
newthread.start()
threads.append(newthread)
# for t in threads:
# t.join()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/313380.html
