Select with a socket and timeout

Select with a socket and timeout

Post by Alai » Fri, 23 Dec 2005 20:53:20


Hello,
I am using the select call before a non blocking recv. This is to wait for a
limited time period. My problem is when closing the connection (using
shutdown et close call to the socket), it is effective only after the
timeout. The call select does not return when closing the socket. So I am
searching a way to return the select when closing the socket ? My connection
is TCP Client and only receiving data (every x seconds).
Thank you for your help,
AR
 
 
 

Select with a socket and timeout

Post by Maxim Yego » Fri, 23 Dec 2005 21:19:43


Are you trying to call close() on a socket while sitting in select() on
the same socket in another thread?

 
 
 

Select with a socket and timeout

Post by Maxim Yego » Fri, 23 Dec 2005 23:25:30


If so there are two common solution:

1) Send a signal to the blocked thread. In this case select() returns
-1, errno == EINTR.
2) Add a control (unix, socketpair()) socket to the set of sockets used
in select(). When you'd like to terminate the thread just send anything
into the other end of the socket from another thread. Once the control
socket becomes ready for read the select will return and you can then
proceed as required.
 
 
 

Select with a socket and timeout

Post by Joe Seig » Sat, 24 Dec 2005 00:06:47


Are polling for error events, not just input or output events? I haven't
tried it but it might work for shutting down a socket.

If you are doing a close on a socket descriptor from one thread while it's
still being used by another thread then stop since it's extremely dangerous.
It's eqivalent to freeing the memory for a data object that's still being
used by another thread. When you close a file/socket descriptor, it's
immediately available for reuse by another thread that's creating descriptors.
So the other thread still using the old descriptor may do i/o on a completely
different socket or file depending on what the reused descriptor is actually
pointing to.


--
Joe Seigh

When you get lemons, you make lemonade.
When you get hardware, you make software.
 
 
 

Select with a socket and timeout

Post by Maxim Yego » Sat, 24 Dec 2005 00:24:01


[]


Yes, rather bad an idea.
 
 
 

Select with a socket and timeout

Post by Alai » Sat, 24 Dec 2005 01:26:55

Thank you for your answer and sorry for the previous message sent to you.
AR

"Maxim Yegorushkin" < XXXX@XXXXX.COM > a rit dans le message de
 
 
 

Select with a socket and timeout

Post by David Schw » Sat, 24 Dec 2005 04:31:10


When you call 'shutdown', that should cause 'select' to return a read
hit, since a read will not block.

DS