【发布时间】:2018-04-19 07:57:21
【问题描述】:
我在 ctypes 中使用 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
【问题讨论】:
标签: python windows-ce ctypes winsock2