服务器到客户端多线程 Python

Server to Client Multithread Python

我正在尝试创建一个简单的服务器和客户端程序。客户端将从服务器请求时间同步,服务器将使用当前的纪元时间进行响应。

我正在尝试将服务器实现为多线程。 当我为单线程做时它工作正常,但现在我认为它不起作用,因为我不断收到以下消息:

第 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()


我最好的猜测是您在服务器脚本中缺少 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 coming 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()