对应的代码:
服务端:
import socket phone = socket.socket(socket.AF_INET,socket.SOCK_STREAM) #买手机 phone.bind(('127.0.0.1',8000))#绑定手机卡 phone.listen(5)#开机 print('——>') conn,addr = phone.accept()# 等电话 msg = conn.recv(1024) #收消息 print('客户端发来的消息是:',msg) conn.send(msg.upper())#发消息 conn.close() #挂电话 phone.close() #关机
客户端:
import socket phone = socket.socket(socket.AF_INET,socket.SOCK_STREAM) phone.connect(('127.0.0.1',8000)) phone.send('hello,this is the first netprogram'.encode('utf-8')) data = phone.recv(1024) print('收到从服务端发来的消息:',data)