标签:init ... pcl net mil break list none int
客户端连接服务端之后, 服务端会生成与客户端交换信息的socket。
在服务端实现多线程: 为每个连接创建一个线程进行信息交换。
import threading from socket import * from time import ctime HOST=‘127.0.0.1‘ PORT = 1111 BUFSIZE= 1024 ADDR = (HOST,PORT) tcpSerSock = socket(AF_INET, SOCK_STREAM) tcpSerSock.bind(ADDR) tcpSerSock.listen(4) class MyThread(threading.Thread): def __init__(self, tcp_client): threading.Thread.__init__(self) self.clientSocket = tcp_client def run(self): while True: data = self.clientSocket.recv(1024) print data if not data: break self.clientSocket.send(‘[%s] %s‘ % (ctime(), data)) if data == None: tcpCliSock.close() while True: print ‘waiting for connection...‘ tcpCliSock, addr = tcpSerSock.accept() print ‘...connection from:‘, addr MyThread(tcpCliSock).start() tcpSerSock.close()
标签:init ... pcl net mil break list none int
原文地址:https://www.cnblogs.com/lijins/p/10193249.html