The code I have written runs but I need it us UDP not TCP. Also I just need someone to check my work and make sure that it works properly. The python code needs to be run with command line so you can add more than one client.
The directions:
1. The chat is performed between 2 clients and not the server.
2. The server will first start up and choose a port number. Then the server prints out its IP address and port number.
3. The client then will start up and create a socket based on the information provided by the server.
4. Now the client and the server can chat with each other by sending messages over the network.
5. The client/server MUST be able to communicate with each other in a continuous manner. (e.g. One side can send multiple messages without any replies from the other side)
6. There is no requirement on what language you use to implement this project, but your program should call upon the socket API for UDP to realize the functionality.
7. You should demonstrate your project using two machines (one for the client and one for the server. Pick your virtual machine as the client to communicate with your actual machine, which is server in our project).
8. You need to open at least 2 cmd window in your virtual machine to demonstrate multiple machine communicate with your server machine (actual machine)
The code:
Server.py
import threading import socket host = '127.0.0.1' port =12000 server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((host, port)) server.listen() clients = [] nicknames =[] def broadcast(message): for client in clients: client.send(message) def handle(client): while True: try: message = client.recv(1024) broadcast(message) except: index = clients.index(client) clients.remove(client) nickname = nicknames(index) broadcast(f'{nickname} left the chat.'.encode('ascii')) nicknames.remove(nickname) break def recive(): while True: client, address= server.accept() print(f"Connected with {str(address)}") client.send('Name'.encode('ascii')) nickname =client.recv(1024).decode('ascii') nicknames.append(nickname) clients.append(client) print(f'Name of the client is {nickname}|') broadcast(f'{nickname} joined chat'.encode('ascii')) client.send('Connected to the server'.encode('ascii')) thread = threading.Thread(target=handle, args=(client,)) thread.start() print("Server is listing....") recive()
Client.py
import socket import threading nickname = input("Enter a name: ") client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect(('127.0.0.1', 12000)) def receive(): while True: try: message = client.recv(1024).decode('ascii') if message == 'NICK': client.send(nickname.encode('ascii')) else: print(message) except: print("An error ocurred ") client.close() break def write(): while True: message = f'{nickname}: {input("")}' client.send(message.encode('ascii')) receive_thread = threading.Thread(target=receive) receive_thread.start() write_thread = threading.Thread(target=write) write_thread.start()
Answer :
I have run this code is working correctly. But it con not run on command line until and unless we set the path.
First set the path where your python file is located.
After this open the command line and run the same on it.
This is your code which works fine.
import threading import socket host = '127.0.0.1' port =12000 server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((host, port)) server.listen() clients = [] nicknames =[] def broadcast(message): for client in clients: client.send(message) def handle(client): while True: try: message = client.recv(1024) broadcast(message) except: index = clients.index(client) clients.remove(client) nickname = nicknames(index) broadcast(f'{nickname} left the chat.'.encode('ascii')) nicknames.remove(nickname) break def recive(): while True: client, address= server.accept() print(f"Connected with {str(address)}") client.send('Name'.encode('ascii')) nickname =client.recv(1024).decode('ascii') nicknames.append(nickname) clients.append(client) print(f'Name of the client is {nickname}|') broadcast(f'{nickname} joined chat'.encode('ascii')) client.send('Connected to the server'.encode('ascii')) thread = threading.Thread(target=handle, args=(client,)) thread.start() print("Server is listing....") recive()
Client.py
import socket import threading nickname = input("Enter a name: ") client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect(('127.0.0.1', 12000)) def receive(): while True: try: message = client.recv(1024).decode('ascii') if message == 'NICK': client.send(nickname.encode('ascii')) else: print(message) except: print("An error ocurred ") client.close() break def write(): while True: message = f'{nickname}: {input("")}' client.send(message.encode('ascii')) receive_thread = threading.Thread(target=receive) receive_thread.start() write_thread = threading.Thread(target=write) write_thread.start()
Get Answers For Free
Most questions answered within 1 hours.