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
No related merge requests found
__pycache__
\ No newline at end of file
# TCP Connection Sim
TCP connection simulation with UDP and ARQ
\ No newline at end of file
TCP connection simulation with UDP and ARQ
Run server
```
python server.py [PORT]
```
Run client
```
python client.py [PORT]
```
\ No newline at end of file
import socket
import sys
from common import *
SERVER_PORT = 10000
HOST_NAME = socket.gethostname()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST_NAME, SERVER_PORT))
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
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 = ""
while True:
msg = s.recv(8)
msg, address = s.recvfrom(SEG_SIZE)
log("SERVER", msg.decode("utf-8"))
if len(msg) <= 0:
break
full_msg += msg.decode("utf-8")
msg.decode("utf-8")
print(full_msg)
\ No newline at end of file
print(full_msg)
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 sys
from common import *
# def CONSTANT
SERVER_PORT = 10000
HOST_NAME = socket.gethostname()
HOST_ADDRESS = socket.gethostbyname(HOST_NAME)
SERVER_PORT = 10000 # default
MSG = "Welcome to the server"
# functions
def send_file(clientsocket, address) :
def send_file(Socket:socket, address) :
# 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}.")
clientsocket.close()
# 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.bind((HOST_NAME, SERVER_PORT))
s.listen(5)
print(f"Server listening on {HOST_ADDRESS}:{SERVER_PORT}\n")
s.bind((HOST_NAME, BROADCAST_PORT))
print(f"Server listening on {HOST_ADDRESS}:{BROADCAST_PORT}\n")
# Listening client request
client_list = []
while True:
clientsocket, address = s.accept()
print(f"[!] Client {address} found")
client_list.append((clientsocket, address))
print(f"[?] Listen more? (y/n) ", end="")
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
......@@ -35,10 +38,10 @@ while True:
# Print client list
print()
print(f"{len(client_list)} client(s) found:")
for client, address in client_list:
for address in client_list:
print(f"--- {address}")
print()
# send file to all client in the list
for client, address in client_list:
send_file(client, address)
\ No newline at end of file
for address in client_list:
send_file(s, address)
\ 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