diff --git a/.gitignore b/.gitignore index ed8ebf583f771da9150c35db3955987b7d757904..aceb14ae647538e82bbe00657126d58105b09bbf 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -__pycache__ \ No newline at end of file +__pycache__ +dump* \ No newline at end of file diff --git a/README.md b/README.md index 382d98627ecd953f715293da5bee199f26cdc4d1..226adbcbbe41bca21410163a073053c848e22dbe 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # TCP Connection Sim -TCP connection simulation with UDP and ARQ +TCP connection simulation with UDP and Go-back-N-ARQ Protocol. Run server ``` diff --git a/server.py b/server.py index ccd7242e8b4036be524cf209f43d1888a3f56ee9..e1a5cd76a7a51b3a37c66e2a9f1a63e838fbfcc1 100644 --- a/server.py +++ b/server.py @@ -6,42 +6,47 @@ from common import * SERVER_PORT = 10000 # default MSG = "Welcome to the server" -# functions -def send_file(Socket:socket, address) : - # To Change - Socket.sendto(bytes(MSG, "utf-8"), address) - Socket.sendto(bytes("Here is your file, Thank you.", "utf-8"), address) - print(f"[!] Message sent to {address}.") - -# main -SERVER_PORT = sys.argv[1] -s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) -s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) -s.bind((HOST_NAME, BROADCAST_PORT)) -print(f"Server listening on {HOST_ADDRESS}:{BROADCAST_PORT}\n") - -# Listening client request -client_list = [] -while True: - message, address = s.recvfrom(SEG_SIZE) - log("!", f"Client {address} found") - log("CLIENT", message.decode("utf-8")+"\n") - msg = f"Hello client, server here on {HOST_ADDRESS}:{SERVER_PORT}" - s.sendto(bytes(msg, "utf-8"), address) - client_list.append(address) - - log("?", "Listen more? (y/n) ", end="") - choice = input() - if choice == "n": - break - -# Print client list -print() -print(f"{len(client_list)} client(s) found:") -for address in client_list: - print(f"--- {address}") -print() - -# send file to all client in the list -for address in client_list: - send_file(s, address) \ No newline at end of file +class Server(): + + def __init__(self, port=10000, filepath="input"): + self.SERVER_PORT = port + self.FILE_PATH = filepath + self.clients = [] + + self.init_socket() + self.listen() + for client in self.clients : + self.handshake(client) + self.send_file(client) + + def init_socket(self): + self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + + def listen(self): + self.socket.bind((HOST_NAME, BROADCAST_PORT)) + log("!", f"Server listening on broadcast address\n") + + while True: + message, address = s.recvfrom(SEG_SIZE) + log("!", f"Client {address} found") + # log("CLIENT", message.decode("utf-8")+"\n") + # msg = f"Hello client, server here on {HOST_ADDRESS}:{SERVER_PORT}" + # s.sendto(bytes(msg, "utf-8"), address) + self.clients.append(address) + + log("?", "Listen more? (y/n) ", end="") + choice = input() + if choice == "n": + break + + def handshake(self, client_addr): + # To-DO + return + + def send_file(self, client_addr): + # To-Do + return + +if __name__=="__main__": + Server(port=sys.argv[1]); \ No newline at end of file