Basic Server/Client socket pair not working

M

Michael Goettsche

Hi there,

I'm trying to write a simple server/client example. The client should be able
to send text to the server and the server should distribute the text to all
connected clients. However, it seems that only the first entered text is sent
and received. When I then get prompted for input again and press return,
nothing gets back to me. Any hints on what I have done would be very much
appreciated!

Here's my code:

############ SERVER ##########
import socket
import select

mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mySocket.bind(('', 11111))
mySocket.listen(1)

clientlist = []

while True:
connection, details = mySocket.accept()
print 'We have opened a connection with', details
clientlist.append(connection)
readable = select.select(clientlist, [], [])
msg = ''
for i in readable[0]:
while len(msg) < 1024:
chunk = i.recv(1024 - len(msg))
msg = msg + chunk

for i in clientlist:
totalsent = 0
while totalsent < 1024:
sent = i.send(msg)
totalsent = totalsent + sent

############## CLIENT ################
import socket
import select

socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.connect(("127.0.0.1", 11111))

while True:
text = raw_input("Du bist an der Reihe")
text = text + ((1024 - len(text)) * ".")
totalsent = 0
while totalsent < len(text):
sent = socket.send(text)
totalsent = totalsent + sent

msg = ''
while len(msg) < 1024:
chunk = socket.recv(1024 - len(msg))
msg = msg + chunk

print msg
 
N

Nicolas Couture

Michael said:
Hi there,

I'm trying to write a simple server/client example. The client should be able
to send text to the server and the server should distribute the text to all
connected clients. However, it seems that only the first entered text is sent
and received. When I then get prompted for input again and press return,
nothing gets back to me. Any hints on what I have done would be very much
appreciated!

Here's my code:

############ SERVER ##########
import socket
import select

mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mySocket.bind(('', 11111))
mySocket.listen(1)

clientlist = []

while True:
connection, details = mySocket.accept()
print 'We have opened a connection with', details
clientlist.append(connection)
readable = select.select(clientlist, [], [])
msg = ''
for i in readable[0]:

for i in readable:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top