I want to use read() with ioctl(), but want to control how much time read should wait, by using a timeout.
Any idea on how to do this?
so far what i know is:
//CLIENT.cpp
struct timeval tv={1,0};
setsockopt( mysocket, SOL_SOCKET, SO_RCVTIMEO, (char *) &tv, sizeof(tv));
connect(mysocket, &sock_dest, sizeof(struct sockaddr));
len = read(mysocket, buffer, 10);
I tried using a 5 sec delay on server, but it did not timeout…
ioctl()won’t do what you want. To use a timeout on reads you need to usepoll()or the older interfaceselect()(I’d usepoll()). The timeout set withSO_RCVTIMEOmay get reset every time new data is received. So, for your example it may wait for up to 10 seconds.poll()returns after the specified timeout telling you whether there is any data. Once that is the case you can just read whatever is there using non-blockingread().