From 99a69bb1190a1fb9ae8c0fcc7cdeec62fe16f8be Mon Sep 17 00:00:00 2001 From: Faris Hasim <farishasimsyauqi@gmail.com> Date: Mon, 22 Nov 2021 05:19:54 +0700 Subject: [PATCH] restructure server --- .gitignore | 3 +- README.md | 2 +- server.py | 83 +++++++++++++++++++++++++++++------------------------- 3 files changed, 47 insertions(+), 41 deletions(-) diff --git a/.gitignore b/.gitignore index ed8ebf5..aceb14a 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 382d986..226adbc 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 ccd7242..e1a5cd7 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 -- GitLab