socket closing problem

Albert Hofkamp hat at se-126.se.wtb.tue.nl
Thu Jul 8 03:54:26 EDT 2004


On Thu, 08 Jul 2004 07:10:30 GMT, flupke <flupke at nonexistingdomain.com> wrote:

> So when the user pushes the exit button, i try to call the close 
> function of the client_connection class:
>======================= snippet =======================
>      def OnFileExit(self,e):
>          self.connection.close()
>          time.sleep(5)
>          self.Close(true)  # Close the frame.
>======================= snippet =======================
> 
> I thought that this would trigger the 
> "data=self.client_socket.recv(BUF_SIZE)"
> from the client_connection class in such a way that it would produce an
> exception or return empty data so i could close the thread gracefuly.
> Apparently this doesn't happen.

sockets are full-duplex, which means that there are two streams of data.
One stream runs from your send-side to the receive-side of the server,
and one stream runs in the other direction (from the send-side of the
server to the receive-side of your connection).
Other than the fact that both streams are connected to the same
processes, both streams are independant.

That means that actions you perform at the send-side are independant of
actions you perform at the receive-side.
Therefore, you cannot expect for the close() to have any effect at the
recv().

(unless the server co-operates of course, and closes its send-side as
reaction to you closing its receive-side. Even then, you still have to
be prepared to receive arbitrary amounts of data before you get EOF,
because you don't know how much data is in the connection between the
server and you).


So the answer to your problem is not to rely on the server reacting to
your close(), and organize you application such that you close both the
send-side and the receive-side (instead of waiting for the EOF).


Albert
-- 
Unlike popular belief, the .doc format is not an open publically available format.



More information about the Python-list mailing list