import socket
import threading
import sys
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(('169.254.50.193',8080))
# file = open('chat.txt','w',encoding='utf8')
def send_msg():
while True:
msg = input('请输入你要发送的内容:')
s.sendto(msg.encode('utf8'),('169.254.50.193',9090))
if msg == 'exit':
break
def recv_msg():
while True:
# data 的数据类型是一个元组
# 元组里的第0个元素是接收到的数据
# 元组里的第1个元素是发送方的ip地址和端口号
data, addr = s.recvfrom(1024)
print('接收到了{}地址{}端口号的消息:{}'.format(addr[0],addr[1],data.decode('utf8')),
file = open('chat.txt','a',encoding='utf8'))
t1 = threading.Thread(target=send_msg)
t2 = threading.Thread(target=recv_msg)
t1.start()
t2.start()
01-24
702
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-29
1327
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-28
1541
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-03
01-12
833
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交