python socket
from socket import * HOST = 'localhost' BUFSIZE = 1024 PORT = 21567 def client_socket(): ADDR = (HOST, PORT) tcpclisock = socket(AF_INET, SOCK_STREAM) tcpclisock.connect(ADDR) while True: data = raw_input('>') if not data: break tcpclisock.send(data) data = tcpclisock.recv(BUFSIZE) if not data: break print(data) tcpclisock.close() if __name__ == '__main__': client_socket()
TCPserver端代码:
from socket import * from time import ctime import threading HOST = '' PORT = 21567 BUFSIZE = 1024 ADDR = (HOST, PORT) def server(address, size): tcpSerSock = socket(AF_INET, SOCK_STREAM) tcpSerSock.bind(address) tcpSerSock.listen(5) while True: print("waiting for connecting!") tcpcliSock, addr = tcpSerSock.accept() print('...connect from:', addr) while True: data = tcpcliSock.recv(size) if not data: break tcpcliSock.send('[%s] %s' % (ctime(), data)) tcpcliSock.close() tcpSerSock.close() if __name__ == '__main__': threads = [] for i in range(5): ADDR = (HOST, PORT + i) t = threading.Thread(target = server, args = (ADDR, BUFSIZE)) threads.append(t) t.start()
client端代码: