3
votes

First of all I would like to say that this is another problem than this one: Similar but not the same

My code looks like this:

struct addrinfo hints, *res;
struct sockaddr* serveraddr;

memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;

int res2 = getaddrinfo(ip, port, &hints, &res);
printf("getaddrinfo() res: %d, %d\n", res2, errno);

serveraddr = res->ai_addr;

//create new socket

int soc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
printf("socket() res: %d, %d\n", socket, errno);

//set nonblocking mode
unsigned long on = 1;
res2 = ioctl(soc, FIONBIO, &on);
printf("ioctl() res: %d\n, %d", res2, errno);

res2 = connect(soc, serveraddr, sizeof(struct sockaddr));
printf("connect() res: %d, %d\n", res2, errno);

//check if socket is ready
fd_set wfds;
FD_ZERO(&wfds);
FD_SET(soc, &wfds);

struct timeval t;
t.tv_sec=15;
t.tv_usec=0;

res2 = select(soc + 1, 0, &wfds, 0, &t);
printf("select() res: %d, %d\n", res2, errno);`

I'm connecting to machine that exists (IP address exists, but there is no server listening on port I'm trying to connect).

Select always returns 1. Why? According to man it should timeout and return 0. After this when I try to write something to socket it returns -1/ECONNREFUSED.

Is this expected behaviour? If yes, how to check after select() is we are connected?

2
Expected? Yes. How to check? cr.yp.to/docs/connect.htmlpilcrow

2 Answers

8
votes

I don't know where in the man pages you see that it should time out.

If there is no firewall dropping the packets, the connection will be refused pretty fast (one packet from your host, one packet reply). So an "event" on the connecting socket will come in as soon as the reset is received. This will wake up select, with (at least) one active socket.

The first attempt to read from or write to that socket will return the underlying connect error.

2
votes

This is correct behavior, because a pending error causes a socket to become both readable and writable. As with any other system call, you need to check for error conditions explicitly. You could include the socket in the exception fdset (the fourth argument to select()), and pick up a notification directly, or you could check later for an error in a number of ways.

Be careful of writing to the socket, as a SIGPIPE could happen. In general, you should set a SIGPIPE handler and handle the error synchronously with EPIPE.