Basic Server/Client socket pair not working

Michael Goettsche mail at tuxipuxi.org
Mon Aug 29 15:30:51 EDT 2005


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



More information about the Python-list mailing list