'Creating a Multi-client Chatroom/Groupchat in Python
I am having some trouble working on the client side for my groupchat application. I keep on running into this error message regarding inputs for a client since I want to be able to have multiple clients and distinguish them by inputted usernames. I am running the server and clients via terminal. I don't know if I am just writing in the completely wrong place or something else is happening.
```
# Import socket related methods
from socket import *
# Import argv related methods
from sys import *
# Import select method
from select import *
# Client needs server's contact information
if len(argv) != 3:
print("usage:", argv[0], "<server name> <server port>")
exit()
# Get server's whereabouts
serverName = argv[1]
serverPort = int(argv[2])
# Get Client's name
clientName = input('Enter Client Name:')
# Create a socket
sock = socket(AF_INET, SOCK_STREAM)
# Connect to the server
sock.connect((serverName, serverPort)) #changed this do to syntax error in this file and org. two way file
print(serverName, serverPort)# syntax error
# Make a file stream out of socket
sockFile = sock.makefile(mode='r')
# Make a list of inputs to watch for
inputSet = [stdin, sockFile]
# Keep sending and receiving messages from the server
while True:
# Wait for a message from keyboard or socket
readableSet, x, x = select(inputSet, [], [])
# Check if there is a message from the keyboard
if stdin in readableSet:
# Read a line form the keyboard
line = stdin.readline()
# If EOF ==> client wants to close connection
if not line:
print('*** Client closing connection')
break
# Send the line to client
sock.send(line.encode())
# Check if there is a message from the socket
if sockFile in readableSet:
# Read a message from the client
line = sockFile.readline()
# If EOF ==> client closed the connection
if not line:
print('*** Server closed connection')
break
# Display the line
print('Server:', line) #removed end since it kept causing errors
# Close the connection
sockFile.close()
sock.close()
**Here is the error code:**
ast login: Thu Mar 17 12:36:45 on ttys002
iamlostboy@paki-Macbook-Pro GroupChat5 % python Client5.py localhost 56000
Enter Client Name:JohnnyRocket
Traceback (most recent call last):
File "Client5.py", line 21, in <module>
clientName = input('Enter Client Name:')
File "<string>", line 1, in <module>
NameError: name 'JohnnyRocket' is not defined
iamlostboy@paki-Macbook-Pro GroupChat5 %
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
