无法使用 ERROR_INVALID_HANDLE 关闭套接字句柄 (6)
Failed to close socket handle with ERROR_INVALID_HANDLE (6)
我在 ctypes 中使用 winsock2 套接字,我可以 closesocket()
很好,但是调用 CloseHandle,总是导致 ERROR_INVALID_HANDLE (6)
。我应该如何正确关闭它?目前我的应用程序总是在 socket() 调用 64 次后崩溃。
# 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
HANDLEs 和 SOCKETs 是不同类型的 objects,因此它们不兼容(也适用于他们的 Python 包装器)。
[MS.Docs]: CloseHandle function 声明如下:
Do not use the CloseHandle function to close a socket. Instead, use the closesocket function, which releases all resources associated with the socket including the handle to the socket object. For more information, see Socket Closure.
@EDIT0:
以上适用于"normal"Win。 [MS.Docs]: CloseHandle (Windows CE 5.0) 没有指定那个段落,还提到它可以用在 Sockets.
[MS.Docs]: socket (Windows Sockets) (Windows CE 5.0) 状态:
When a session has been completed, a closesocket call must be performed.
[MS.Docs]: Creating a Connection to a Remote Device Using Winsock (Windows CE 5.0) 确实指定调用 CloseHandle 但仅在步骤 #5 用于客户端套接字(既不在步骤 #4 中,也不在服务器套接字中),这让我认为这是一个错误(WinCE 页面充满错误 - 至少是拼写错误)
作为附带问题:您为什么使用 ctypes 而不是 [Python 3]: socket - Low-level networking interface,后者是 WinSock 的包装器?这就像搬起石头砸自己的脚。如果 BT 套接字与其他套接字(例如网络)一样工作,您唯一需要做的就是定义一些常量(例如 BTHPROTO_RFCOMM).
我在 ctypes 中使用 winsock2 套接字,我可以 closesocket()
很好,但是调用 CloseHandle,总是导致 ERROR_INVALID_HANDLE (6)
。我应该如何正确关闭它?目前我的应用程序总是在 socket() 调用 64 次后崩溃。
# 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
HANDLEs 和 SOCKETs 是不同类型的 objects,因此它们不兼容(也适用于他们的 Python 包装器)。
[MS.Docs]: CloseHandle function 声明如下:
Do not use the CloseHandle function to close a socket. Instead, use the closesocket function, which releases all resources associated with the socket including the handle to the socket object. For more information, see Socket Closure.
@EDIT0:
以上适用于"normal"Win。 [MS.Docs]: CloseHandle (Windows CE 5.0) 没有指定那个段落,还提到它可以用在 Sockets.
[MS.Docs]: socket (Windows Sockets) (Windows CE 5.0) 状态:
When a session has been completed, a closesocket call must be performed.
[MS.Docs]: Creating a Connection to a Remote Device Using Winsock (Windows CE 5.0) 确实指定调用 CloseHandle 但仅在步骤 #5 用于客户端套接字(既不在步骤 #4 中,也不在服务器套接字中),这让我认为这是一个错误(WinCE 页面充满错误 - 至少是拼写错误)
作为附带问题:您为什么使用 ctypes 而不是 [Python 3]: socket - Low-level networking interface,后者是 WinSock 的包装器?这就像搬起石头砸自己的脚。如果 BT 套接字与其他套接字(例如网络)一样工作,您唯一需要做的就是定义一些常量(例如 BTHPROTO_RFCOMM).