-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.py
69 lines (25 loc) · 1.16 KB
/
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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)
#UDPServerSocket2 = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
# Bind to address and ip
UDPServerSocket.bind((localIP, localPort))
#UDPServerSocket2.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]
m=message.decode()
clientMsg = "Message from Client "+m[len(m)-1]+": "+m[0:len(m)-1]
clientIP = "Client IP Address: {}".format(address)
print(clientMsg)
#print(clientIP)
msgFromServer = input("Enter your message for client "+m[len(m)-1]+": ")
bytesToSend = str.encode(msgFromServer)
# Sending a reply to client
UDPServerSocket.sendto(bytesToSend, address)