-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
32 lines (24 loc) · 1.1 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
import socket
from abc import ABC, abstractmethod
import datetime
import pickle
from threading import Thread
class ServerUDP:
BUFFER_SIZE = 1024
def __init__(self, port):
self.__udp_server_socket = socket.socket(family=socket.AF_INET, type= socket.SOCK_DGRAM)
self.__udp_server_socket.bind(('localhost', port))
self.__connections = []
def listening(self):
while True:
self.__message_from_client, self.__client_adress = self.__udp_server_socket.recvfrom(ServerUDP.BUFFER_SIZE)
self.__connections.append(self.__client_adress)
if len(self.__connections) == 2:
self.__udp_server_socket.sendto(bytes(f'{self.__connections[1]}','utf-8'), self.__connections[0])
self.__udp_server_socket.sendto(bytes(f'{self.__connections[0]}','utf-8'), self.__connections[1])
self.__connections = []
else:
self.__udp_server_socket.sendto(bytes(f'Waiting another user', 'utf-8'), self.__connections[0])
if __name__ == "__main__":
server = ServerUDP(28886)
server.listening()