I'm trying to set a timeout on an accept socket function without success. I would like the accept function blocks until the end of the timeout delay. Is it possible without setting the accept function non-blocking ? I have tried out many possibilities without success.
Thanks for your answers.
Below is my code :
struct timeval tv;
fd_set readfds;
tv.tv_sec = 1;
tv.tv_usec = 0;
int s, s_remote;
struct sockaddr_un remote;
struct sockaddr_un local;
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
FD_ZERO(&readfds);
FD_SET(s, &readfds);
if (select(s+1, &readfds, NULL, NULL, &tv) > 0) {
printf("Waiting for a connection...\n");
memset(&local, 0, sizeof(local));
local.sun_family = AF_UNIX;
strcpy(local.sun_path, SOCK_PATH);
unlink(local.sun_path);
if (bind(s, (struct sockaddr *)&local, sizeof(local)) == -1) {
perror("UnixSocketClient :: error bind.\n");
close(s);
return -1;
}
if (listen(s, 5) == -1) {
perror("UnixSocketClient :: error listen.\n");
close(s);
return -1;
}
socklen_t remote_len = sizeof(remote);
printf("Accept :\n\r");
if ((s_remote = accept(s, (struct sockaddr *)&remote, &remote_len)) == -1) {
perror("UnixSocket :: error accept.\n");
return -1;
}
printf("Client accepted\n\r");
}