Untitled

 avatar
user_7384651
plain_text
a month ago
729 B
1
Indexable
Never
import socket
import time

MAXLINE = 1024

sd = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_addr = ('localhost', 5678)
print("Client has connected to Server.")
print("Waiting 10 secs on purpose...")
time.sleep(10)

while True:
    time.sleep(3)
    str_data = "How are you?"
    sd.sendto(str_data.encode(), server_addr)

    try:
        data, _ = sd.recvfrom(MAXLINE)
        print("Received every 3 secs:", data.decode())
    except socket.error as e:
        err_code = e.errno
        if err_code != socket.errno.WSAEWOULDBLOCK and err_code != 0:
            print("Winsock error code:", err_code)
            print("Disconnected!")
            sd.close()
            break

sd.close()
Leave a Comment