-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
55 lines (22 loc) · 915 Bytes
/
server.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import socket
localIP = "127.0.0.1"
localPort = 20001
bufferSize = 1024
# Create a datagram socket
UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
# Bind to address and ip
UDPServerSocket.bind((localIP, localPort))
print("UDP server up and listening")
# Listen for incoming datagrams
while(True):
bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)
message = bytesAddressPair[0]
address = bytesAddressPair[1]
clientMsg = "Message from Client: {}".format(message.decode())
clientIP = "Client IP Address: {}".format(address)
print(clientMsg)
#print(clientIP)
msgFromServer = input("Enter your message :")
bytesToSend = str.encode(msgFromServer)
# Sending a reply to client
UDPServerSocket.sendto(bytesToSend, address)