Python | Difference between process and thread
Multithreaded chat
Implement chat on a computer.
import socket, sys
import threading
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('192.168.31.199', 8080))
def send_msg():
while True:
msg = input('Please enter what you want to send:')
s.sendto(msg.encode('utf8'), ('192.168.31.199', 9090))
if msg == 'exit':
break
def recv_msg():
while True:
# The data type of data is a tuple
# The 0th element in the tuple is the received data
# The first element in the tuple is the sender's ip address and port number
data, addr = s.recvfrom(1024)
print('Received a message from {}address{}port:{}'.format(addr[0], addr[1], data.decode('utf8')),
file=open('Message record.txt', 'a', encoding='utf8'))
t1 = threading.Thread(target=send_msg)
t2 = threading.Thread(target=recv_msg)
t1.start()
t2.start()
Related Articles
-
A detailed explanation of Hadoop core architecture HDFS
Knowledge Base Team
-
What Does IOT Mean
Knowledge Base Team
-
6 Optional Technologies for Data Storage
Knowledge Base Team
-
What Is Blockchain Technology
Knowledge Base Team
Explore More Special Offers
-
Short Message Service(SMS) & Mail Service
50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00