2010-10-28 08:36:03     socket problem ?

Document created by Aaronwu Employee on Aug 23, 2013
Version 1Show Document
  • View in full screen mode

2010-10-28 08:36:03     socket problem ?

Filip Vanalme (BELGIUM)

Message: 95361   

 

BF561

Kernel 2009R1

 

I'm running a server application on my board. I use a listening socket in blocked mode. When there's no data, the recv() call blocks. A client can make a connection without any problem and exchanging data works fine. Obviously, when I close the socket from within the server, the thread remains blocked in the recv(). I would expect the recv() to return with a value 0 or -1 if the socket closes. Am I missing something ?

 

(I noticed this with the following case : the client, while having a connection with the server, reboots. It makes a new connection to the server. The server detects that it still has a connection for that device and closes the previous socket to continue with the new one. However, as the recv() remains blocked on that previous socket, the communication does not work anymore. )

TranslateQuoteReplyEditDelete

 

 

2010-10-28 16:09:51     Re: socket problem ?

Mike Frysinger (UNITED STATES)

Message: 95370   

 

i dont quite follow the example

 

a server creates a socket, binds it to an address/port, calls listen() on it, and then waits for a client by calling accept().  you do this in one thread (call it "the main server thread").

 

when a client connects, accept() returns a new socket.  this has no relationship to the original socket created which the server is listening on.  another thread takes this new socket and calls recv() on it to wait for data.  while the main server thread goes back to listening for new clients.  or not ... it doesnt matter much to this example i dont think.

 

at some point in the future, the main server thread calls close() on the first socket.  but the second socket returned by accept() is not affected and thus the other thread continues to wait for data.

 

if my description reflects your situation, then this behavior is correct.

 

i dont know how you've architectured your threads, but one option might be for the main server thread to call pthread_cancel on all the worker threads.

QuoteReplyEditDelete

 

 

2010-10-29 03:06:10     Re: socket problem ?

Filip Vanalme (BELGIUM)

Message: 95380   

 

Hi Mike,

 

I try to clarify a little more :

 

Indeed, there's a listener thread. It accepts an incoming connection and returns a new socket, say SOCK1. Then, a thread, say T1, continues with that socket and calls a blocking recv(). The main thread goes back listening for incoming connections.

 

At some point, there's a new incoming connection from the same remote device (because that device has rebooted and makes its connection again). Because it's a connection from the same device, we have to close SOCK1 and accept the new connection, giving us SOCK2. Now, when T1 is blocking on the recv() for SOCK1 and SOCK1 is closed, I notice that T1 stays blocked in recv(). recv() does not return with i.e. -1.

 

Meanwhile, I did some other tests. Obviously, there's also a shutdown() function to "close" a socket. As an experiment, I substituted the close() function by the shutdown(). When using shutdown(), the blocking recv() returns with -1. However, I'm not sure as to when using close() and shutdown(). I feel like using shutdown() is not the right way to go. On the other hand, it's strange that a close() does not make the blocking recv() to return.

TranslateQuoteReplyEditDelete

 

 

2010-10-29 03:28:25     Re: socket problem ?

Filip Vanalme (BELGIUM)

Message: 95394   

 

Hi Mike,

 

I try to clarify a little more :

 

Indeed, there's a listener thread. It accepts an incoming connection and returns a new socket, say SOCK1. Then, a thread, say T1, continues with that socket and calls a blocking recv(). The main thread goes back listening for incoming connections.

 

At some point, there's a new incoming connection from the same remote device (because that device has rebooted and makes its connection again). Because it's a connection from the same device, we close SOCK1 and accept the new connection, giving us SOCK2. Now, when T1 is blocking on the recv() for SOCK1 and SOCK1 is closed, I notice that T1 stays blocked in recv(). recv() does not return with i.e. -1.

 

Meanwhile, I did some other tests. Obviously, there's also a shutdown() function to "close" a socket. As an experiment, I substituted the close() function by the shutdown(). When using shutdown(), the blocking recv() returns with -1. However, I'm not sure as to when using close() and shutdown(). I feel like using shutdown() is not the right way to go. On the other hand, it's strange that a close() does not make the blocking recv() to return.

 

---

 

Closing the socket seems to be successful. The close() function does not return any error.

TranslateQuoteReplyEditDelete

 

 

2010-10-29 08:16:18     Re: socket problem ?

Filip Vanalme (BELGIUM)

Message: 95397   

 

Elsewhere on the Internet, I found that this could indeed be a problem :

 

Thread A blocks in recv() for socket1 (because there's no data available)

Thread B closes socket1

Thread A never comes out of its recv call

 

Can anyone confirm that this is indeed normal behaviour for sockets ? And how to tackle this ?

Attachments

    Outcomes