Tino Dai wrote:
> Hi there,
>
> I am wondering if somebody to could answer a question about
> sockets. I have a socket that
> is listening, and a client program connects to it. The client program
> transfers a name over, and then disconnects from the socket. Now, how
> that is done is using a socket.close() call to shut down the entire
> socket. My question is: Is there a way to have the socket close the
> connection, yet stay open for the next client that comes along and
> connects to the server? I have my already written code below for further
> documentation. Thanks!
>
> while 1:
> s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
> s.bind((self.ipAddr,self.port))
> s.listen(5)
> print "The port is: ", self.port
> client,addr=s.accept()
> while 1:
>try:
>if addr[0] == self.remoteIpAddr:
>client.send("Connected to the server\n")
>msg=client.recv(1024)
>msg=msg.strip()
>if msg in 'exit':
> s.close() #Is there a different
> way to write this?
I think you want to close the client socket - client.close() - rather
than the master socket.
You might be interested in the SocketServer library which helps to write
simple socket servers such as this. One advantage of using SocketServer
is it makes it trivial to convert your server to a threaded or forked
server. Here are some examples:
http://www.amk.ca/python/simple/fingerd.py.html
http://examples.oreilly.com/pythonian/ see example 19-5
Kent
> time.sleep(30)
> print "exiting"
> break
>if len(msg) > 0:
>
>
>
>
> ___
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor