It's not really duplicated. When use none-block socket, you have to use select method to check if connection is created, in select method you can set a timeout value to terminate. Let's say if I want to break the select method before the preset timeout value, it's the same problem again. In my case the select timeout can be up to 5 minutes, in case the printer is busy doing something and has no time to reply.
Close the socket from another thread to cancel connect, or select, seems the best solution for my question.
I have program connect to a remote TCPIP port, it's a printer.
SOCKET socket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in SockAddr;
//... initialized host and port and so on
int iret = connect(socket, &SockAddr, sizeof(SockAddr));
if (iret != 0)
{
// get error code...
}
As far as the printer is on, it works fine. In case printer was turned off, connect will be fail and "host not reachable", that is also fine.
If the printer is off, connect method will be blocked for about 20 to 21 seconds then return failed. This 20 seconds should be calculated from some system configuration and it's possible but difficult to change. On other system this could be longer.
My question is that is it possible to break from the connect method, by another call from another thread? I want to cancel the connection in case I found the printer is not on, not to stop the application but continue to do some logging jobs.
If I am attach a debug to the process, pause then continue, it seems will break earlier.
I cannot use none-block socket here, because it's a very big project and everything else is designed with the block socket by someone else. I am not permitted to do big changes.