-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
33 lines (25 loc) · 902 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
import socket
import sys
import threading
# declare ip address and port
UDP_IP_ADDRESS = '127.0.0.1'
UDP_PORT_NO = 2410
# create a socket
serverSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# bind the socket to address
serverSock.bind((UDP_IP_ADDRESS, UDP_PORT_NO))
# allows sockets to bind() to the same IP:port
serverSock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
serverSock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
# print additional message
print("\n!!!Server is ready!!!")
print("\nYour chats will be recorded here")
# handle message receiving
while True:
# receive message from clients
message, address = serverSock.recvfrom(1024)
message_send = message.decode("utf-8")
# print message
print(message_send)
# send broadcast message to all clients
serverSock.sendto(message_send.encode("utf-8"), ('<broadcast>', UDP_PORT_NO))