My problem is that I have a thread that is in a recv () call. The remote host suddenly stops (no sockets) and the RQV () call continues to block. This is clearly not good because when I'm joining the thread to shut down the process (locally) this thread never goes out because it is waiting on the REV which will never come.
So my question is, what is this method, generally people consider the best way to deal with this issue? There are some additional things to note that should be known before answering:
-
I have no way to make sure that the remote host closes the socket before it closes .
-
This solution can not use external libraries (like Boosting), it should use standard libraries / attributes of C ++ / C (primarily C + + 0x not specific).
Thanks!
Assume that you want to continue using blocking sockets, you can use it:
Specify the timing of receiving SO_RCVTIMEO and SO_SNDTIMEO error, or the timeout is specified, the parameter is straight time. If an input or out-function blocks for this period of time, and the data has been sent or received, the return value data will be transferred to that function; If no data has been transferred and the time has expired, then -1 has been returned from the errno set to EAGAN or EWWABLOCK, as if the socket was specified for descending. If timeout is set to zero (default), then Operation Timeout will never occur
, before receiving:
Structure Timelay Timeout = {Timo_sec, timo_usec}; Int R = SetCop (S, SOL_COCK, SO_RCVT MEO, and Timeout, Psychof (Timeout)); Emphasis (R == 0); / * Or something more user-friendly * /
If you are ready to use the non-blocked I / O, you can use the
poll ()
Select () ,
Apollo () ,
kqueue () , or the appropriate event despatched mechanism for your system is non-blocked. Need to use / O to inform you that you call the system call on
recv () to inform you that there is no data in the input row of the socket sub Example to industry involves a bit more:
for (;;) {ssize_t bytes = Arkear (S, snow, Akarf (buf), MSG_DONTWAIT); If (bytes> 0) {/ * ... * / continu; } If (bytes <0) {if (errno == EWOULDBLOCK) {struct pollfd p = {s, POLLIN, 0}; Int r = survey (and p, 1, time_msec); If (R == 1) continues; If (R ==) {/*...handle timeout * / / * either releases or breaks, depending on policy * /}} / * ... handle errors / breaks; } / * Connection is closed / break; }
No comments:
Post a Comment