贴代码,server端:
import socket # s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) host = '' port = 6074 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.bind((host,port)) s.listen(1) while 1: conn,addr = s.accept() while 1: print ('connected by',addr) data = conn.recv(1024) if not data:break conn.sendall(data) conn.close()
客户端:
import socket # s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) host = '' port = 6074 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.bind((host,port)) s.listen(1) while 1: conn,addr = s.accept() while 1: print ('connected by',addr) data = conn.recv(1024) if not data:break conn.sendall(data) conn.close()
for循环是在一个线程下执行,不同于多线程。