-1

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.

Tiger Hwang
  • 300
  • 1
  • 5
  • 16
  • What is a "tcpip port"? There is TCP and there is IP. Both are different networking layers. – too honest for this site Dec 13 '16 at 14:24
  • @Olaf that's overly pedantic - it's _very_ common to combine the two together and call it TCPIP – Alnitak Dec 13 '16 at 14:47
  • 1
    @Alnitak: "Millions of flies can't be wrong: eat sh**":-) Seriously: just because it is used wrong does not mean it should be. Confusing networking layers is one common reason for missconception and problems. (it would be overly pedantic, though to ask what "tcpip" is, as it typically is called "TCP/IP".). – too honest for this site Dec 13 '16 at 15:22

1 Answers1

1

The simplest way to unblock the connect is to close the socket. Unfortunately while waiting for the connect your thead is suspended so you would have to close the socket from another thread.

The alternative solution will be to use asynchronous io Windows does this with OVERLAPPED io. On linux one would set the socket as non-blocking. This will cause the connect to return immediatel while the connect happens in the background. One can then use select or poll to wait for the socket to be ready. If you add stdin to the select, a keypress will interrupt the select abd can be processed as required.

doron
  • 26,460
  • 11
  • 62
  • 99
  • I am trying this, but it will take a while. The socket is opened and stored inside a struct, I have to let the main thread to access the socket. – Tiger Hwang Dec 13 '16 at 15:40
  • The simplest way works the best. I just call closesocket from main thread, then it break from the blocked connect method. 17:00:08.371 connecting to 172.20.17.26:5001 17:00:08.549 cancel now 17:00:08.549 connect failed 10038 – Tiger Hwang Dec 13 '16 at 16:03