socket_ chat

# Udp chat 

Import socket


class Udp_chat (Object):

DEF main (Self):
# Create a udp socket
UDP_SOCKET = socket.socket (socket.AF_INET, socket.SOCK_DGRAM)
# bind
udp_socket.bind (( '', 8088 ))
# ip and port to bind the other party
dest_ip = input ( 'Please enter each other's ip:')
dest_port = int (the iNPUT ( 'Please enter each other's ports:'))

the while True:
input_word the iNPUT = ( 'Please enter your selection: receiving a message sends a message 2. the system 3. exit ')
IF input_word ==' 1 ':
the send_data = iNPUT (' Please enter the message you want to send: ')
udp_socket.sendto (send_data.encode (' . 8-UTF '), (DEST_IP, dest_port))
elif input_word ==' 2 ':
recv_data = udp_socket.recvfrom(1024)
print(recv_data[0].decode('gbk'))
elif input_word == '3':
exit()
udp_socket.close()


if __name__ == '__main__':
udp_chat = Udp_chat()
udp_chat.main()

Guess you like

Origin www.cnblogs.com/victorstudy/p/11224292.html