server 端:
# -*-coding:utf-8-*- import socket import sys reload(sys) sys.setdefaultencoding('utf8') #sever host='192.168.1.8' port =9600 s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.bind((host,port)) s.listen(2) conn,addr=s.accept() print 'Connected by ', addr while 1: data = conn.recv(4096) if not data:break print data conn.sendall(data) conn.close()
client 端:
# -*-coding:utf-8-*- import socket import sys reload(sys) sys.setdefaultencoding('utf8') #sever host='192.168.1.8' port =9600 s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connect((host,port)) s.sendall('hello world') data = s.recv(4096) s.close() print 'Received',repr(data)