I’ve been trying to code a simple chat server in Python, my code is as follows:
import socket
import select
port = 11222
serverSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1024)
serverSocket.bind(('',port))
serverSocket.listen(5)
sockets=[serverSocket]
print 'Server is started on port' , port,'\n'
def acceptConn():
newsock, addr = serverSocket.accept()
sockets.append(newsock)
newsock.send('You are now connected to the chat server\n')
msg = 'Client joined',addr.__str__(),
broadcast(msg, newsock)
def broadcast(msg, sourceSocket):
for s in sockets:
if (s != serverSocket and s != sourceSocket):
s.send(msg)
print msg,
while True:
(sread, swrite, sexec)=select.select(sockets,[],[])
for s in sread:
if s == serverSocket:
acceptConn()
else:
msg=s.recv(100)
if msg.rstrip() == "quit":
host,port=socket.getpeername()
msg = 'Client left' , (host,port)
broadcast(msg,s)
s.close()
sockets.remove(s)
del s
else:
host,port=s.getpeername()
msg = s.recv(1024)
broadcast(msg,s)
continue
After running the server and connecting via telnet, the server reads single character and skips the next one. Example if I type Hello in telnet, server reads H l o.
Any help please ?! 🙂
You call recv twice.
First:
Then, if that’s not “quit”, you read and broadcast another message:
So the original message is lost.
Because you’re using telnet as the client, you get one character at a time, so you see every other character. If you used, say, nc instead, you’d get different results—but still the same basic problem of every other read being thrown away.
There are a few other problems here:
Here’s a modified version of your sample that fixes most of the problems, except for requiring “quit” to be in a single message alone and relying on the clients to send newlines:
To fix the ‘quit’ problem, you’re going to have to keep a buffer for each client, and do something like this:
But you’ve still got the newline problem. Imagine that user1 logs in and types “abc\n” while user2 logs in and types “def\n”; you may get something like “abClient joined: 127.0.0.1:56881\ndec\nf\n”.
If you want a line-based protocol, you have to rewrite your code to do the echoing on a line-by-line instead of read-by-read basis.