Socket Programming Question

Ray Van Dolson rayvd at nospam.firetail.org
Fri Apr 7 01:01:58 EDT 2000


I'm trying to write a simple chat server w/ Python sockets that supports 
multiple connections and am having a little trouble... here's my server code so 
far:

#!/usr/bin/python

from socket import *
from threading import *

serversocket = socket(AF_INET,SOCK_STREAM)
serversocket.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
serversocket.bind((gethostname(), 9001))
serversocket.listen(5)

def client_thread(clientsocket):
        clientsocket.send("Welcome to the chat server...\r\n")
        while 1:
                data = clientsocket.recv(1024)
                if data:
                        clientsocket.send(data)
                else:
                        pass

def main_server_thread():
        while 1:
                (clientsocket, address) = serversocket.accept()
                ct = Thread(target=client_thread,args=((clientsocket),))
                ct.run()

mainThread = Thread(target=main_server_thread,args=())
mainThread.start()
while 1:
        pass

However, this code only allows one connection at a time... the other can connect 
to the socket but is simply queued.  I would think the main_server_thread() 
therad would create a new thread for each connection?

What do I need to do to support multiple, simultaneous connections with this 
code?

Thanks,
Ray Van Dolson



More information about the Python-list mailing list