-
-
Save TvoozMagnificent/cc127b5d99b2620576d25f870d407b2d to your computer and use it in GitHub Desktop.
Server Side
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import struct | |
import socket | |
from threading import Thread | |
from rich import print | |
print('\n'*100) | |
# server's IP address | |
SERVER_HOST = "0.0.0.0" | |
SERVER_PORT = 5002 # port we want to use | |
separator_token = ": " # we will use this to separate the client name & message | |
# initialize list/set of all connected client's sockets | |
client_sockets = set() | |
# create a TCP socket | |
s = socket.socket() | |
# make the port as reusable port | |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
# bind the socket to the address we specified | |
s.bind((SERVER_HOST, SERVER_PORT)) | |
# listen for upcoming connections | |
s.listen(5) | |
print(f"[*] Listening as {SERVER_HOST}:{SERVER_PORT}") | |
#def listen_for_client(cs): | |
# """ | |
# This function keep listening for a message from `cs` socket | |
# Whenever a message is received, broadcast it to all other connected clients | |
# """ | |
# while True: | |
# try: | |
# # keep listening for a message from `cs` socket | |
# #msg = cs.recv(1024).decode() | |
# buff+=(socket.recv(1024)) | |
# if len(buff)>=size + 4: | |
# if size>0: | |
# callback(buff[:size].decode('utf-8')) | |
# size,=struct.unpack("!L", buff[size:size+4]) | |
# buff=buff[size+4:] | |
# #print(size) | |
# except Exception as e: | |
# # client no longer connected | |
# # remove it from the set | |
# print(f"[!] Error: {e}") | |
# client_sockets.remove(cs) | |
# else: | |
# # if we received a message, replace the <SEP> | |
# # token with ": " for nice printing | |
# msg = msg.replace(separator_token, ": ") | |
# # iterate over all connected sockets | |
# for client_socket in client_sockets: | |
# # and send the message | |
# # print(msg) | |
# x=msg.encode() | |
# client_socket.send(struct.pack('!L', len(x))+x)#send(msg.encode()) | |
# | |
def listen_for_client(socket=s, callback=print): | |
try: | |
buff=b"" | |
size=0 | |
while True: | |
buff+=(socket.recv(1024)) | |
if size == 0: | |
size,=struct.unpack("!L", buff[:4]) | |
buff=buff[4:] | |
if len(buff)>=size: | |
callback(buff[:size].decode('utf-8')) | |
buff=buff[size:] | |
size=0 | |
except Exception as e: | |
print(f"[!] Error: {e}") | |
client_sockets.remove(cs) | |
else: | |
# if we received a message, replace the <SEP> | |
# token with ": " for nice printing | |
msg = msg.replace("<SEP>", ": ") | |
# iterate over all connected sockets | |
for client_socket in client_sockets: | |
# and send the message | |
# print(msg) | |
x=msg.encode() | |
client_socket.send(struct.pack('!L', len(x))+x)#send(msg.encode()) | |
while True: | |
# we keep listening for new connections all the time | |
client_socket, client_address = s.accept() | |
print(f"[+] {client_address} connected.") | |
# add the new connected client to connected sockets | |
client_sockets.add(client_socket) | |
# start a new thread that listens for each client's messages | |
t = Thread(target=listen_for_client, args=(client_socket,)) | |
# make the thread daemon so it ends whenever the main thread ends | |
t.daemon = True | |
# start the thread | |
t.start() | |
# close client sockets | |
for cs in client_sockets: | |
cs.close() | |
# close server socket | |
s.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment