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

update to broadcast request

parent c46bb6a9
Branches
No related merge requests found
__pycache__
\ No newline at end of file
# TCP Connection Sim # TCP Connection Sim
TCP connection simulation with UDP and ARQ TCP connection simulation with UDP and ARQ
\ No newline at end of file
Run server
```
python server.py [PORT]
```
Run client
```
python client.py [PORT]
```
\ No newline at end of file
import socket import socket
import sys
from common import *
SERVER_PORT = 10000
HOST_NAME = socket.gethostname()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((HOST_NAME, SERVER_PORT)) if len(sys.argv) > 1:
try :
s.bind((HOST_ADDRESS, int(sys.argv[1])))
except TypeError:
pass
except OSError:
log("!", "Port already used, continue using random port")
pass
log("!", "Request sent to broadcast address")
s.sendto(bytes("Hello, client here looking for server", "utf-8"), (HOST_NAME, BROADCAST_PORT))
full_msg = "" full_msg = ""
while True: while True:
msg = s.recv(8) msg, address = s.recvfrom(SEG_SIZE)
log("SERVER", msg.decode("utf-8"))
if len(msg) <= 0: if len(msg) <= 0:
break break
full_msg += msg.decode("utf-8") msg.decode("utf-8")
print(full_msg) print(full_msg)
\ No newline at end of file
import socket
################
### CONSTANT ###
################
BROADCAST_PORT = 9999
HOST_NAME = socket.gethostname()
HOST_ADDRESS = socket.gethostbyname(HOST_NAME)
SEG_SIZE = 32768
################
### FUNCTION ###
################
def log(context:str, info:str, end:str=None):
print(f"[{context}] {info}", end=end)
def cheksum(data:bytes):
# To Change
return data[0]
\ No newline at end of file
import socket import socket
import sys
from common import *
# def CONSTANT # def CONSTANT
SERVER_PORT = 10000 SERVER_PORT = 10000 # default
HOST_NAME = socket.gethostname()
HOST_ADDRESS = socket.gethostbyname(HOST_NAME)
MSG = "Welcome to the server" MSG = "Welcome to the server"
# functions # functions
def send_file(clientsocket, address) : def send_file(Socket:socket, address) :
# To Change # To Change
clientsocket.send(bytes(MSG, "utf-8")) 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}.") print(f"[!] Message sent to {address}.")
clientsocket.close()
# main # main
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) SERVER_PORT = sys.argv[1]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST_NAME, SERVER_PORT)) s.bind((HOST_NAME, BROADCAST_PORT))
s.listen(5) print(f"Server listening on {HOST_ADDRESS}:{BROADCAST_PORT}\n")
print(f"Server listening on {HOST_ADDRESS}:{SERVER_PORT}\n")
# Listening client request # Listening client request
client_list = [] client_list = []
while True: while True:
clientsocket, address = s.accept() message, address = s.recvfrom(SEG_SIZE)
print(f"[!] Client {address} found") log("!", f"Client {address} found")
client_list.append((clientsocket, address)) log("CLIENT", message.decode("utf-8")+"\n")
print(f"[?] Listen more? (y/n) ", end="") 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() choice = input()
if choice == "n": if choice == "n":
break break
...@@ -35,10 +38,10 @@ while True: ...@@ -35,10 +38,10 @@ while True:
# Print client list # Print client list
print() print()
print(f"{len(client_list)} client(s) found:") print(f"{len(client_list)} client(s) found:")
for client, address in client_list: for address in client_list:
print(f"--- {address}") print(f"--- {address}")
print() print()
# send file to all client in the list # send file to all client in the list
for client, address in client_list: for address in client_list:
send_file(client, address) send_file(s, address)
\ No newline at end of file \ 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