I've a small agent that supposed to connect to server from time to time
first stage it opens a socket
:
const char* hostname // e.g. "127.0.0.1"
hostaddr = inet_addr(hostname);
/* Ultra basic "connect to port 22 on localhost"
* Your code is responsible for creating the socket establishing the
* connection
*/
sock = socket(AF_INET, SOCK_STREAM, 0);
sin.sin_family = AF_INET;
sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n");
return -1;
}
when ip-address
is not valid it seems that connect hangs for very long time
before failing
looking at the man page provide some data on the subject but not how to overpass it
My question is why and how can I add timeout for it (or any other solution)