socket closing problem

flupke flupke at nonexistingdomain.com
Fri Jul 9 10:16:02 CEST 2004


Thanks Gandalf & Albert,

both your sollutions seem to be working.
This is what i've tried but as to what is the most natural sollution, 
i'm not sure.

1) using select.select on the socket (Gandalf)

========================= snippet =======================
client _connection class:
	...
         in_socket = [self.client_socket]
         try:
             while not done:
                 print "ready to receive data"
                 try:
                     i, o, e = select.select(in_socket,[],[])
                     for x_socket in i:
                         data = x_socket.recv(BUF_SIZE)
                 except socket.error, msg:
                     print "Error receiving data"
                     break
	...

main class:
	...
     def OnFileExit(self,e):
         #self.s.close()
         #wait until the thread dies
         if ( self.s != None ):
             self.s.settimeout(0)
             self.connection.close()
             #self.s.close()
             time.sleep(5)
         self.Close(true)  # Close the frame.
	...
========================= snippet =======================

Now, the exit closes the socket which triggers an exception, as 
expected. This also triggers a "clean" closing of the connection on the 
server.

2) Albert said: "herefore, you cannot expect for the close() to have any 
effect at the recv()."
So in order to close, i made sure that part of closing of the client is 
to first send a "bye" command to the server. This makes the server close 
the socket which then also triggers the exception.

========================= snippet =======================
client _connection class:
	...
         try:
             while not done:
                 print "ready to receive data"
                 try:
                     data = self.client_socket.recv(BUF_SIZE)
                 except socket.error, msg:
                     print "Error receiving data"
                     break
	...
     def close(self):
         print "from client_connection close() "
         self.message("bye")
         done = 1
         self.client_socket.settimeout(0)
         self.client_socket.close()
========================= snippet =======================

Now, there are 2 ways to solve my problem but i'm not sure which one is 
the nicest when you would aim for the most "logical" sollution.
Any thoughts?

Thanks,
Benedict



More information about the Python-list mailing list