Okay basically I need to write a simple chat program. So far I have the following for the server and client code:
Code:
# server
import socket
import thread
HOST = 'localhost' # Symbolic name meaning the local host
PORT = 150 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
while True:
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if data[:4] == "exit": print 'Parted by', addr
else: conn.send(data),
if not data: break
print `data`
conn.close()
and...
Code:
# client
import socket
getHOST = (raw_input('Enter the Host: '))
print getHOST
HOST = getHOST
PORT = 150 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
NICK = raw_input('Welcome, Enter your name: ')
while True:
MSG = raw_input('>> ')
MSG = NICK + ': ' + MSG
if MSG[5:] == "exit": break
s.send(MSG)
data = s.recv(1024)
print data
s.close()
The main issue is this:
I have no idea to allow it to accept connections from multiple clients. I need it to basically keep checking for a new connection. After that is done, I can already tell I'm going to have an issue with being able to receive messages from other users while I am sitting at the prompt to send a message- since that will be holding up the program.
Anyone have some experience with this?