Bonus question #2!
I need connect to timeout. I'm trying to convert what I already have to use non-blocking sockets and select(), but I'm failing. It claims to be connected, but it never actually logs on or receives data. Am I using select() correctly?
Code:
FD_ZERO(&sockets);
FD_SET(s, &sockets);
timeout.tv_sec = 0;
timeout.tv_usec = 100;
printf("Connecting...\n");
connect(s, (struct sockaddr*)&sin, sizeof(sin));
while (select(0, NULL, &sockets, NULL, &timeout) == SOCKET_ERROR) {
printf("[%d] ", WSAGetLastError());
printf("Retrying connection...\n");
connect(s, (struct sockaddr*)&sin, sizeof(sin));
FD_ZERO(&sockets);
FD_SET(s, &sockets);
}
Edit1:
Code:
FD_ZERO(&sockets);
FD_SET(s, &sockets);
timeout.tv_sec = 0;
timeout.tv_usec = 900;
printf("Connecting...\n");
connect(s, (struct sockaddr*)&sin, sizeof(sin));
ret = select(s+1, NULL, &sockets, NULL, &timeout);
while (ret == 0 || ret == SOCKET_ERROR) {
printf("[%d] ", WSAGetLastError());
printf("Retrying connection...\n");
connect(s, (struct sockaddr*)&sin, sizeof(sin));
FD_ZERO(&sockets);
FD_SET(s, &sockets);
ret = select(s+1, NULL, &sockets, NULL, &timeout);
}
And it works. However, if I set tv_usec to 999 and under, I get [10035] Retrying connection, [10037] Retrying connection, [10037] Retrying connection, [10037] Retrying connection, Connected!. Why is this? Do I have to close the socket and create a new, every time I retry the connection?
Edit2: Durrrr. Microseconds.
Do I have to close the socket and create a new, every time I retry the connection?