# 用同一个套接字接收数据
import socket
class Send_msg(object):
def main(self):
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_socket.bind(('', 8888))
try:
dest_ip = input('请输入对方的ip:')
dest_port = int(input('请输入对方的端口:'))
except Exception as e:
print('请输入正确的ip和端口')
while True:
send_data = input('请输入要发送的内容:')
udp_socket.sendto(send_data.encode('gbk'), (dest_ip, dest_port))
recv_data = udp_socket.recvfrom(1024)
print(recv_data[0].decode('gbk'))
udp_socket.close()
if __name__ == '__main__':
sm = Send_msg()
sm.main()