【问题标题】:Server to Client Multithread Python服务器到客户端多线程 Python
【发布时间】:2021-12-01 04:49:27
【问题描述】:

我正在尝试创建一个简单的服务器和客户端程序。客户端将向服务器请求时间同步,服务器将回答当前的纪元时间。

我正在尝试将服务器实现为多线程。 当我为单线程执行此操作时,它运行良好,但现在我认为它不起作用,因为我不断收到以下消息:

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


【问题讨论】:

    标签: python-3.x multithreading sockets server tcpclient


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2011-07-29
      • 1970-01-01
      • 2014-12-21
      • 2011-04-28
      • 1970-01-01
      • 1970-01-01
      • 2017-10-10
      • 2019-05-17
      • 2015-01-09
      相关资源
      最近更新 更多