From 732ee35cbf52e2d2d6e168d1e7642ef249e126bd Mon Sep 17 00:00:00 2001 From: PavelLinearB Date: Wed, 19 Feb 2025 11:39:37 +0200 Subject: [PATCH 1/3] add server and client --- src/client/client.py | 38 +++++++++++++++++++++++++ src/server/server.py | 67 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 src/client/client.py create mode 100644 src/server/server.py diff --git a/src/client/client.py b/src/client/client.py new file mode 100644 index 0000000..2a0a6ad --- /dev/null +++ b/src/client/client.py @@ -0,0 +1,38 @@ +import socket +import threading +import sys + +#Wait for incoming data from server +#.decode is used to turn the message in bytes to a string +def receive(socket, signal): + while signal: + try: + data = socket.recv(32) + print(str(data.decode(utf-8))) + except: + print(You have been disconnected from the server) + signal = False + break + +#Get host and port +host = input(Host: ) +port = int(input(Port: )) + +#Attempt connection to server +try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.connect((host, port)) +except: + print(Could not make a connection to the server) + input(Press enter to quit) + sys.exit(0) + +#Create new thread to wait for data +receiveThread = threading.Thread(target = receive, args = (sock, True)) +receiveThread.start() + +#Send data to server +#str.encode is used to turn the string message into bytes so it can be sent across the network +while True: + message = input() + sock.sendall(str.encode(message)) diff --git a/src/server/server.py b/src/server/server.py new file mode 100644 index 0000000..bf132bc --- /dev/null +++ b/src/server/server.py @@ -0,0 +1,67 @@ +import socket +import threading + +#Variables for holding information about connections +connections = [] +total_connections = 0 + +#Client class, new instance created for each connected client +#Each instance has the socket and address that is associated with items +#Along with an assigned ID and a name chosen by the client +class Client(threading.Thread): + def __init__(self, socket, address, id, name, signal): + threading.Thread.__init__(self) + self.socket = socket + self.address = address + self.id = id + self.name = name + self.signal = signal + + def __str__(self): + return str(self.id) + + str(self.address) + + #Attempt to get data from client + #If unable to, assume client has disconnected and remove him from server data + #If able to and we get data back, print it in the server and send it back to every + #client aside from the client that has sent it + #.decode is used to convert the byte data into a printable string + def run(self): + while self.signal: + try: + data = self.socket.recv(32) + except: + print(Client + str(self.address) + has disconnected) + self.signal = False + connections.remove(self) + break + if data != : + print(ID + str(self.id) + : + str(data.decode(utf-8))) + for client in connections: + if client.id != self.id: + client.socket.sendall(data) + +#Wait for new connections +def newConnections(socket): + while True: + sock, address = socket.accept() + global total_connections + connections.append(Client(sock, address, total_connections, Name, True)) + connections[len(connections) - 1].start() + print(New connection at ID + str(connections[len(connections) - 1])) + total_connections += 1 + +def main(): + #Get host and port + host = input(Host: ) + port = int(input(Port: )) + + #Create new server socket + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.bind((host, port)) + sock.listen(5) + + #Create new thread to wait for connections + newConnectionsThread = threading.Thread(target = newConnections, args = (sock,)) + newConnectionsThread.start() + +main() From f1d02587531a098b3acaa94fe376e8d6f2b82fcb Mon Sep 17 00:00:00 2001 From: Pavel Vaks <129676672+PavelLinearB@users.noreply.github.com> Date: Thu, 20 Feb 2025 20:51:11 +0200 Subject: [PATCH 2/3] Update client.py --- src/client/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/client.py b/src/client/client.py index 2a0a6ad..0dc7c51 100644 --- a/src/client/client.py +++ b/src/client/client.py @@ -2,7 +2,7 @@ import threading import sys -#Wait for incoming data from server +#Wait for incoming data from the server #.decode is used to turn the message in bytes to a string def receive(socket, signal): while signal: From d762061ca14cef70b24235b131fadae36832ac20 Mon Sep 17 00:00:00 2001 From: Pavel Vaks <129676672+PavelLinearB@users.noreply.github.com> Date: Tue, 25 Feb 2025 15:07:08 +0200 Subject: [PATCH 3/3] Update client.py --- src/client/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/client.py b/src/client/client.py index 0dc7c51..c5ff82e 100644 --- a/src/client/client.py +++ b/src/client/client.py @@ -18,7 +18,7 @@ def receive(socket, signal): host = input(Host: ) port = int(input(Port: )) -#Attempt connection to server +#Attempt connection to the server try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((host, port))