简体   繁体   中英

non blocking socket select returns 1 after connect

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM