I am writing a code send the output of a terminal command over a socket in C. I have tried using select for asynchronous reading and avoid blocking the event-loop, but I wasn't successful.
How can I change this code to make the file stream IO non-blocking?
int maxfdpl;
fd_set rset;
char sendline[100], recvline[100], my_msg[100];
FILE *in;
char str[30]="ping 192.168.26.219";
if(!(in = popen(str, "r"))){
return EXIT_FAILURE;
}
FD_ZERO(&rset);
FD_SET(fileno(in), &rset);
maxfdpl =fileno(in) + 1;
select(maxfdpl, &rset, NULL, NULL, NULL);
while(1) {
if (FD_ISSET(fileno(in), &rset)) {
if (fgets(sendline, 100, in)) {
send_over_socket(sendline);
}
}
}
How can I remove the while loop (which is blocking the event-loop) and replace the code with a non-blocking IO operation?