[Rd] Question on non-blocking socket

Ben Engbers ben@engber@ @end|ng |rom gm@||@com
Fri Feb 17 15:00:34 CET 2023


Hi Tomas,

Apparently, inserting some kind of socketSelect() is essential when 
using non-blocking sockets and a client/erve architecture. That is at 
least one thing that I have learned ;-).

In C++, between sending and requesting, I inserted a call to this function:
bool wait(int s) {
   fd_set read_set;
   struct timeval timeout {};
   memset(&timeout, 0, sizeof(timeout));
   bool done{};
   while (!done ) {
     FD_ZERO(&read_set);
     FD_SET(s, &read_set);
     int rc = select(s + 1, &read_set, NULL, NULL, &timeout);
     done = (rc == 1) && FD_ISSET(s, &read_set);
   };
   return done;
};

Inserting this call was essential in solving my problem.

Ben

Op 15-02-2023 om 17:17 schreef Tomas Kalibera:
> In the example you are waiting only for a single byte. But if the 
> response may be longer, one needs to take into account in the client 
> that not all bytes of the response may be available right away. One 
> would keep receiving the data in a loop, as they become available (e.g. 
> socketSelect() would tell), keep appending them to a buffer, and keep 
> looking for when they are complete.
> 
> Tomas
>> Ben



More information about the R-devel mailing list