SOCKET : How to gracfully stop application when recv() function is blocking
-
Hi , I am working on an application which involves socket communication. Current in my read () function 'recv()' method is blocking, i.e. does not allow execution until it gets some data. Here at this point I want to gracfully stop application . Is there any API for this ?
-
Hi , I am working on an application which involves socket communication. Current in my read () function 'recv()' method is blocking, i.e. does not allow execution until it gets some data. Here at this point I want to gracfully stop application . Is there any API for this ?
You have two obvious choices (and maybe a few more esoteric ones). 1. Google "non-blocking socket" and see how you can get to read what data is available, when you want to get it. 2. Fire up another thread which can block on the
recv()
call, but doesn't block your whole app. Another thread can then take control and shut down or whatever you need to do. Cheers, PeterSoftware rusts. Simon Stephenson, ca 1994.
-
You have two obvious choices (and maybe a few more esoteric ones). 1. Google "non-blocking socket" and see how you can get to read what data is available, when you want to get it. 2. Fire up another thread which can block on the
recv()
call, but doesn't block your whole app. Another thread can then take control and shut down or whatever you need to do. Cheers, PeterSoftware rusts. Simon Stephenson, ca 1994.
I remember way back when me and my buddy were at the university and had a class about networking. The class got an assignment to make a simple chat program with 2 participants using sockets. At that point we never heard of non-blocking sockets before (not sure about threads). So most people made chat programs in which the two sides could talk alternately, A sending something, then B sending, then A again, then B and so on. We on the other hand, created a "ping-pong" mechanism, a kind of "token" packet would be sent here and there between the two clients constantly, if one had something to say, it would send its content along with the token to the other client, if it had nothing to say then it would just send the token alone back. This way the clients didn't seem to block waiting for the other side to say something. Ah, the good old naive young days... :)
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > //TODO: Implement signature here<
-
Hi , I am working on an application which involves socket communication. Current in my read () function 'recv()' method is blocking, i.e. does not allow execution until it gets some data. Here at this point I want to gracfully stop application . Is there any API for this ?
-
recv() is acting on Socket S. Keep S in a list (or some other available source). On shutdown spin up a thread. Grab S. Close S. Then recv() will return. Modify the code that uses that to behave correctly.