Skip to content
Snippets Groups Projects
Commit 99a69bb1 authored by Faris Hasim Syauqi's avatar Faris Hasim Syauqi
Browse files

restructure server

parent 8359bc29
Branches
No related merge requests found
__pycache__
\ No newline at end of file
__pycache__
dump*
\ No newline at end of file
# TCP Connection Sim
TCP connection simulation with UDP and ARQ
TCP connection simulation with UDP and Go-back-N-ARQ Protocol.
Run server
```
......
......@@ -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
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment