proper way to close a socket?
I have a problem with CAsynsocket. I can connect to a remote socket. When I
want to close the connection, I just delete the socket object (which closes
the connection) and have no problems.
But if I've started to connect to a remote socket that doesn't exist (or
doesn't accept), and I delete the local socket object while waiting for the
connection, I sometimes get an assert in CAsyncSocket::DoCallBack(). See
below. Actually, it's not in the middle of an Accept call because I test
with an address and port that I know doesn't exist.
Any idea how I should be doing this so as not to crash my program?
Thanks,
Bill
void PASCAL CAsyncSocket::DoCallBack(WPARAM wParam, LPARAM lParam)
{
if (wParam == 0 && lParam == 0)
return;
// Has the socket be closed?
CAsyncSocket* pSocket = CAsyncSocket::LookupHandle((SOCKET)wParam, TRUE);
// If yes ignore message
if (pSocket != NULL)
return;
pSocket = CAsyncSocket::LookupHandle((SOCKET)wParam, FALSE);
if (pSocket == NULL)
{
// Must be in the middle of an Accept call
pSocket = CAsyncSocket::LookupHandle(INVALID_SOCKET, FALSE);
ASSERT(pSocket != NULL);
<-------------------------------------------------asserts here sometimes but
not always.
pSocket->m_hSocket = (SOCKET)wParam;
CAsyncSocket::DetachHandle(INVALID_SOCKET, FALSE);
CAsyncSocket::AttachHandle(pSocket->m_hSocket, pSocket, FALSE);
}