-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUDP__Client.py
More file actions
35 lines (26 loc) · 1.05 KB
/
UDP__Client.py
File metadata and controls
35 lines (26 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import socket
def start_client():
"""Start UDP client and connect to the server"""
server_ip = input("Enter server IP: ")
server_port = int(input("Enter server PORT: "))
# Create UDP socket for the client
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = (server_ip, server_port)
try:
while True:
# Input message from user
message = input("Enter your message (press '!q' to exit): ")
if message.lower() == '!q':
print("Disconnected")
client_socket.sendto(message.encode('utf-8'), server_address)
break
# Send the message to the server
client_socket.sendto(message.encode('utf-8'), server_address)
# Receive response from the server
response, _ = client_socket.recvfrom(1024)
print(f"From server: {response.decode('utf-8')}")
finally:
# Close socket when the client exits
client_socket.close()
if __name__ == "__main__":
start_client()