From a1a41ddc65747c26b73342eba55b7ff505bced17 Mon Sep 17 00:00:00 2001 From: Kshitij Date: Tue, 8 Oct 2024 19:11:52 +0530 Subject: [PATCH] Added code for B8 (server + client). --- Codes/Code-B8 (Client).py | 56 ++++++++++++++++++++++++ Codes/Code-B8 (Server).py | 90 +++++++++++++++++++++++++++++++++++++++ README.md | 2 + 3 files changed, 148 insertions(+) create mode 100755 Codes/Code-B8 (Client).py create mode 100755 Codes/Code-B8 (Server).py diff --git a/Codes/Code-B8 (Client).py b/Codes/Code-B8 (Client).py new file mode 100755 index 0000000..5f9b934 --- /dev/null +++ b/Codes/Code-B8 (Client).py @@ -0,0 +1,56 @@ +# TCP (lan) based chatting app + +# -------------------------------------------------------------- +# client.py +# -------------------------------------------------------------- + +# req. libs +import socket +import threading + + +# global +## connection data +host = '127.0.0.1' # server ip +port = 4444 # free/open port +print(f"[#] connecting to {host}:{port}") + +## nickname +nickname = input("[+] enter nickname:") + +## starting client +client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +client.connect((host, port)) +print(f"[+] connected to {host}:{port}") + + +# listening to server & sending nickname +def receive(): + while True: + try: + # receive msg + msg = client.recv(1024).decode('ascii') + + # print msg + if msg == 'NICKNAME': + client.send(nickname.encode('ascii')) + else: + print(msg) + except: + print("[!] error 404\n[#] terminating...") + client.close() + break + +# send msg to server +def write(): + while True: + msg = f"{nickname}: {input('')}" + client.send(msg.encode('ascii')) + + +# starting threads for listening & writing +receive_thread = threading.Thread(target=receive) +receive_thread.start() + +write_thread = threading.Thread(target=write) +write_thread.start() diff --git a/Codes/Code-B8 (Server).py b/Codes/Code-B8 (Server).py new file mode 100755 index 0000000..e2a80fc --- /dev/null +++ b/Codes/Code-B8 (Server).py @@ -0,0 +1,90 @@ +# TCP (lan) based chatting app + +# -------------------------------------------------------------- +# server.py +# -------------------------------------------------------------- + + +# req. libs +import socket +import threading + + + +# global +## connection data +host = '127.0.0.1' # server ip +port = 4444 # free/open port + +## starting server +server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + # AF_INET - using internal socket, rather than unix socket + # SOCK_STREAM - using tcp (& not udp) +server.bind((host,port)) +server.listen() + +## clients & nicknames +clients = [] +nicknames = [] + +print(f"[+] Server is running on {host}:{port}") + + +# send msg to all clients +def broadcast(msg): + for client in clients: + client.send(msg) + + +# handling clients +def handle(client): + while True: + try: + msg = client.recv(1024) + broadcast(msg) + except: + # disconnecting inactive clients + index = clients.index(client) + + # del f/ clients + try: clients.remove(index) + except: pass + client.close() + + # del f/ nicknames + nickname = nicknames.index(index) + nicknames.remove(nickname) + + # display + print(f"[-] '{nickname}' left") + break + + +# listening +def receive(): + while True: + # accept conn + client, addr = server.accept() + print(f"[+] '{str(addr)}' connected") + + # request & store nickname + client.send('NICKNAME'.encode('ascii')) + nickname = client.recv(1024).decode('ascii') + + # store + clients.append(client) + nicknames.append(nickname) + + # display + print(f"[+] '{nickname}' connected") + + # send msg + broadcast(f"[#] {nickname} connected to the server\n".encode('ascii')) + + # handle + client_handler = threading.Thread(target=handle, args=(client,)) + client_handler.start() + + +receive() + diff --git a/README.md b/README.md index 9e016d2..413191f 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,8 @@ This Git repository is a comprehensive resource for the Computer Networks and Se - [Write-up - A7](https://git.kska.io/sppu-te-comp-content/ComputerNetworksAndSecurity/src/branch/main/Write-ups/Write-up%20-%20B7.pdf) ##### B8 - TCP Socket +- [Code-B8 (Client)](https://git.kska.io/sppu-te-comp-content/ComputerNetworksAndSecurity/src/branch/main/Codes/Code-B8%20%28Client%29.py) +- [Code-B8 (Server)](https://git.kska.io/sppu-te-comp-content/ComputerNetworksAndSecurity/src/branch/main/Codes/Code-B8%20%28Server%29.py) - [Handout-B8](https://git.kska.io/sppu-te-comp-content/ComputerNetworksAndSecurity/src/branch/main/Handouts/Handout-B8.pdf) - [Write-up - B8](https://git.kska.io/sppu-te-comp-content/ComputerNetworksAndSecurity/src/branch/main/Write-ups/Write-up%20-%20B8.pdf)