I am using a blocking socket to accept connection. I rarely get this error which makes it hard to debug. The accept returns with EAGAIN error. How can that be for a blocking socket?
If the socket has a receive timeout set (with the SO_RCVTIMEO socket option), then accept will return EAGAIN when the timeout expires.
This code will demonstrate it (and also let you investigate the effect of delivering a signal):
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>
#define TESTEXIT(s, f) if (s < 0) { perror(f); exit(1); }
void handler(int x)
{
return;
}
int main()
{
int s;
int r;
struct sockaddr_in sin;
socklen_t sin_len = sizeof sin;
struct timeval timeo = { .tv_sec = 5, .tv_usec = 0 };
signal(SIGUSR1, handler);
s = socket(PF_INET, SOCK_STREAM, 0);
TESTEXIT(s, "socket");
r = listen(s, 10);
TESTEXIT(r, "listen");
r = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof timeo);
TESTEXIT(r, "setsockopt");
r = accept(s, (struct sockaddr *)&sin, &sin_len);
TESTEXIT(r, "accept");
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With