-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEchoServer.py
More file actions
28 lines (18 loc) · 713 Bytes
/
Copy pathEchoServer.py
File metadata and controls
28 lines (18 loc) · 713 Bytes
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
import socket
import Constants
localIP = "127.0.0.1"
# Create a datagram socket: (ipv4, UDP)
UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
# Bind to address and ip
server_port = int(input(f"Please enter server port: "))
UDPServerSocket.bind((localIP, server_port))
print("UDP server up and listening")
# Listen for incoming datagrams
while(True):
bytesAddressPair = UDPServerSocket.recvfrom(Constants.BUFFER_SIZE)
message = bytesAddressPair[0]
address = bytesAddressPair[1]
print(f"Message from Client: {message.decode('ascii')}")
print(f"Client IP Address: {address}")
# Sending a reply to client
UDPServerSocket.sendto(message, address)