python-1802-和卓 15:12:36
1……
from threading import Thread
class SenderThread(Thread):
def__init__(self,socket,destaddress,d estport):
Thread.__init__(self) self.socket = socket self.dest_address = destaddress self.dest_port = destport
def run(self):
while True:
msg = input("我说:") #键盘输入 self.socket.sendto(msg.encode(),(self.dest_address,self.dest_port))
2……
from threading import
Thread class ReceiverThread(Thread): 接收线程 def __init__(self,socket): Thread.__init__(self) self.socket = socket
def run(self):
while True:
msg,addr = self.s.recvfrom(1024) print(addr,"说:",msg.decode())
3……
import socket
from chat_sender import SenderThread
from chat_receiver import ReceiverThread
s = socket.socket(type=socket.SOCK_DGRAM) #实例化支持UDP协议的socket对象 s.bind(("localhost",8888)) SenderThread(socket,"localhost",6666).start() #启动发送线程 ReceiverThread(s).start() #启动接收线程
网友评论