Socket Programming Question

Paul Gresham gresham at mediavisual.com
Sat Apr 8 08:36:37 EDT 2000


Have a look at www.freshmeat.net search for ACHAT. Someone has written it
already ... I believe this is using sockets.


"Ray Van Dolson" <rayvd at nospam.firetail.org> wrote in message
news:aZdH4.95109$AT6.106753 at dfw-read.news.verio.net...
> 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