-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEchoClient.py
More file actions
21 lines (16 loc) · 820 Bytes
/
Copy pathEchoClient.py
File metadata and controls
21 lines (16 loc) · 820 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import socket
import Constants
server_port = int(input(f"Please enter server port: "))
serverAddressPort = (Constants.SERVER_DOMAIN_NAME, server_port)
xClientAddressPort = ("127.0.0.1", Constants.XCLIENT_UDP_PORT)
bufferSize = 1024
# Create a UDP socket at client side, (ipv4, UDP)
UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
# Initial message
UDPClientSocket.sendto(f"\0\0\0\0{serverAddressPort[0]}\0\0\0\0{serverAddressPort[1]}".encode("ascii"), xClientAddressPort)
while True:
# Send to server using created UDP socket
bytesToSend = str.encode(input("Please write your message: "))
UDPClientSocket.sendto(bytesToSend, xClientAddressPort)
msgFromServer = UDPClientSocket.recvfrom(bufferSize)[0].decode("ascii")
print(f"Message from Server: {msgFromServer}")