我正在C类型中使用winsock2套接字,我可以
closesocket()
很好,但是调用CloseHandle,总是会导致
ERROR_INVALID_HANDLE (6)
.我应该如何正确关闭它?当前,我的应用程序总是在64次socket()调用后崩溃。
# from MSDN:
# BOOL CloseHandle( HANDLE hObject);
closehandle = coredll.CloseHandle
closehandle.argtypes = [ w.LPVOID ]
SOCKET = c_ulong
socket = ws2.socket
socket.restype = SOCKET
self._clnt_socket = socket(AF_BT, SOCK_STREAM, BTHPROTO_RFCOMM)
...
connect( self._clnt_socket, _psa, sizeof(self._sa) )
...
send( self._clnt_socket, pbuff, szbuff, 0 ) # int send( SOCKET s, const char FAR* buf, int len, int flags);
SetLastError(0)
rt = closesocket( self._clnt_socket )
ec = GetLastError()
if ec != w.ERROR_SUCCESS :
print( u'failed to close socket, ec=%s, %s, rt=%s', (ec, FormatError( ec ), rt) )
raise Exception(u'BT_SOCKET.close.socket %s' % ec)
else:
print( u'close socket ok' )
#> close socket ok
# from MSDN:
# To close the connection to the target device, call the closesocket
# function to close the Bluetooth socket. Also, ensure that you release
# the socket by calling the CloseHandle function, as the following
# example code shows.
#
# closesocket(client_socket);
# CloseHandle((LPVOID)client_socket);
SetLastError(0)
rt = closehandle( w.LPVOID( self._clnt_socket ) )
ec = GetLastError()
if ec != w.ERROR_SUCCESS :
print( u'failed to close handle, ec=%s, %s, rt=%s ', (ec, FormatError( ec ), rt) )
# //Perform error handling.
raise Exception(u'BT_SOCKET.close.handle %s' % ec)
else:
print( u'close socket ok' )
#> failed to close handle, ec=6