Interrumpir un enchufe wsa de Windows que se conecta

I was just wondering if it is possible to interrupt call to windows socket "connect" function?

The problem is that my code requires that to be done in a different thread (so GUI thread keeps running). But when the programm is closed there my still be threads calling "connect" that are wating for a WSAETIMEDOUT exception.

¿Alguna idea?

Update/Hint: i cant call close() since i only have a valid handle when connect() returns. the latter one is not the case when using blocking sockets and having a tcp-connect to a firewalled location (for example) :/

preguntado el 10 de marzo de 12 a las 10:03

1 Respuestas

If the socket is in blocking mode, the only way to abort connect() call is to close the socket from a different thread context than the one that is calling connect(). connect() will return an error, and the thread can then exit itself normally.

If the socket is in non-blocking or overlapped mode, connect() will return immediately with a WSAEWOULDBLOCK error, and you then have to call select(), WSAAsyncSelect(FD_CONNECT) o el WSAEventSelect(FD_CONNECT) to detect when the connection has been established before continuing with your socket work. Since the calling thread is not blocked on connect(), it is free to periodically check for any termination/abort signals from the rest of your code, and if detected then close the socket (if needed) and exit itself normally.

If you write your socket code in non-blocking or overlapped mode, then you do not really need to use a thread. You can do your socket work within the main thread without blocking your UI, then you can just close the socket when needed. It takes a little more work to code that way, but it does work. Or you can continue using a thread. It will keep your socket code separate from your UI code and thus a bit more managable.

respondido 11 mar '12, 00:03

theres one big problem, i cant call close(<HANDLE>) since i dont hav any handle to operate on. i will only have this handle when the initial socket connect() call has terminated. so its a chicken-egg problem here - mschmoock

You close the handle that was returned by socket(). connect() takes an existing socket handle as input. It does not return a new socket handle. So just make sure you store the socket handle from socket() in a place that both the connecting thread and the closing thread can reach. - Rémy Lebeau

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.