0

I'm testing things out and i made a non blocking socket but the "fgets" ruined my plans. How can i prevent fgets for blocking out my while loop showing below? A code that solves this would be greatly appreciated.

fcntl(clientSocket, F_SETFL, O_NONBLOCK);

while(1){

     fgets(buffer,sizeof(buffer),stdin);
     send(clientSocket,buffer,sizeof(buffer),0);

     recv(clientSocket,buffer,sizeof(buffer),0);
     printf("%s",buffer);

}
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328

1 Answers1

-1

It's not recommended, but if you want you can just do:

if (fcntl(0, F_SETFL, O_NONBLOCK) == -1) {
   perror("fcntl");
   exit(EXIT_FAILURE);
}

And you have a non blocking stdin.

Hint: better use select or poll for waiting ready file descriptor.

Overflow 404
  • 482
  • 5
  • 20