Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Xymostech provided the exact function I needed and after consulting the API for "select", which includes a small usage example, I modified the code there to fit what I needed and wrote a socket timer (for reads, those it's pretty simple to extend towards writes and such, as "select" has parameters for enabling this kind of check). Make sure you've included the following libraries as specified by the "select" API:</p> <pre><code>#include &lt;sys/types.h&gt; #include &lt;sys/socket.h&gt; #include &lt;sys/time.h&gt; #include &lt;stdio.h&gt; </code></pre> <p>The following is the waittoread(...) function I created from the API example and works pretty well. This works well in the domain of my specific problem, however, if one is looking for a more generalized timer (i.e. not just for timing socket read and writes, or file descriptors) please consult signal handling (somewhat in the spirit of the code I posted in my initial question).</p> <pre><code>#define S1READY 0x01 // necessary for the function's bitwise OR operation int waittoread(int s1, int timeout_value){ fd_set fds; // create set of sockets to be waited on struct timeval timeout; // the time-out value int rc; // # of sockets that are ready before timer expires int result; /* Set time limit. */ timeout.tv_sec = timeout_value; timeout.tv_usec = 0; /* Create a descriptor set containing the socket. */ FD_ZERO(&amp;fds); // MACRO to reset the socket storage set so new ones can be added FD_SET(s1, &amp;fds); // add the socket descriptor into the socket set to wait on rc = select(sizeof(fds)*4, &amp;fds, NULL, NULL, &amp;timeout); // build the socket-wait system // another way of calling select that would be a better approach: // rc = select(s1 + 1), &amp;fds, NULL, NULL, &amp;timeout); if (rc==-1) { perror("Error: Call to select failed."); return -1; } result = 0; if (rc &gt; 0){ if (FD_ISSET(s1, &amp;fds)) result |= S1READY; // if the result is non-zero, perform a BIT-wise OR to extract the true socket count # } return result; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload