Multi-threaded implementation of tcp chat server

tcp server:

1  from socket import *
 2  from threading import Thread
 3  
4  def client(socket_client, msg_addr):
 5      print ( " >>> there is a new client connection <<< " )
 6      try :
 7          while True:
 8              #Accept the client send Incoming information 
9              msg = socket_client.recv(1024 )
 10              if msg:
 11                  print ( " %s--> %s " % (msg_addr, msg.decode( ' utf-8 ')))
 12              else :
 13                  print (msg_addr)
 14                  print ( " Client disconnected... " )
 15                  break 
16      except :
 17          socket_client.close()
 18  
19  
20  def main():
 21      #Create a socket Connection, AF_INET means compliance with IPv4 protocol, SOCK_STREAM (stream) means using tcp protocol transmission 
22      #If using UDP protocol transmission, use SOCK_DGRAM (datagram) 
23      server = socket(AF_INET, SOCK_STREAM)
 24  
25      #Reuse binding Set information 
26      #Here, if the server becomes the first time that tcp waves its hands four times, the server will finally wait for 2MSL to accept the ack that the client may send. 
27      #During this time, if the server wants to repeat the execution, it will be occupied before The port and other services are not released, causing the server to not be executed 
28      #Add this sentence here to solve this problem 
29      server.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1 )
 30  
31      msg_server = ( " localhost " , 7788 )
 32      #Bind local 7788 port 
33      server.bind(msg_server)
 34      #Start listening 35 #The value in listen here represents the sum of clients in semi-connected and connected state 36      server.listen(5 )
 37 try :
 38 while True:
 39
     
                           #Establish a connection with the client 
40              # socket_client indicates that a new socket containing tcp three-way handshake information has been created for this client 41 # msg_addr contains the information of this client 42              socket_client, msg_addr = server.accept()
 43 #For each A client connecting to the server creates a separate thread and passes in the above two parameters 44              t = Thread(target=client, args= (socket_client,msg_addr))
 45 #In multi-threading, the created sockets are shared A socket_client, so this socket cannot be closed at this time 46 #The thread is just repeatedly switching the calling function in a certain core of the cpu, so the data is actually one copy, which is also the reason why the data in multiple threads can be shared 47             t.start()
 48 49 finally :
 50         server.close()
 51 52
             
             
             
             
  
       
 if __name__ == "__main__":
53     main()

 

 tcp client:

1  from socket import *
 2  
3 client = socket(AF_INET,SOCK_STREAM)
 4  
5 server_msg = ( " localhost " ,7788 )
 6  #Connect to server 
7  client.connect(server_msg)
 8  
9  while True:
 10      send = input( " To send Text content: " )
 11      if send == ' q ' :
 12          break 
13  
14      else :
 15         client.send((send).encode("utf-8"))
16         print("发送成功!")
17 
18 client.close()

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324969295&siteId=291194637