diff --git a/src/client/client.py b/src/client/client.py new file mode 100644 index 0000000..1723508 --- /dev/null +++ b/src/client/client.py @@ -0,0 +1,44 @@ +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, connected = True): + while connected: + try: + data = b'' + while True: + chunk = socket.recv(4096) + data += chunk + if len(chunk) < 4096: + break + if data: + print(str(data.decode('utf-8'))) + except (socket.error, ConnectionResetError) as e: + print("You have been disconnected from the server. Error: " + e.strerror) + connected = 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 (socket.error, ConnectionRefusedError) as e: + print("Could not make a connection to the server. Error: " + e.strerror) + 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..85c94fb --- /dev/null +++ b/src/server/server.py @@ -0,0 +1,72 @@ +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 = b'' + while True: + chunk = self.socket.recv(4096) + data += chunk + if len(chunk) < 4096: + break + except (socket.error, ConnectionResetError) as e: + print("Client " + str(self.address) + " has disconnected") + self.signal = False + connections.remove(self) + break + if data != b"": + 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()