Search code examples
python-3.xmultithreadingsocketsservertcpclient

Server to Client Multithread Python


I am trying to create a simple server and client program. The client will request time sync from the server and server will answer with the current Epoch Time.

I am trying to implement the server as multithread. When I did for single-thread it worked fine, but now I don't think is working because I keep getting the following message:

line 21, in run connectionSocket.send(ts.encode())

BrokenPipeError: [Errno 32] Broken pipe

Here is my code

Client1:

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


Multithread server

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



Solution

  • My best guess is you are missing the return statement in the server script. It needs a few more fixes, but this should work - run this code:

    Client

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

    Server

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