Setting socket timeout for receive function

Viewed 6259

I have a client server connection where the client is sending data to the server.

while (1) {

    bzero(buffer, 256);
    sleep(1);

    n = read(sock, buffer);
    if(n < 0) error("ERROR reading from socket");
    c = message[0];

    //do something

}//close while loop

The issue i only want to wait for a read to happen only for some seconds - in my code, if the client does not send anything, it gets stuck waiting for the server to read something.

How can I wait for a read to happen only some seconds please?

3 Answers
Related