简体   繁体   English

关闭在 dll 中创建的套接字

[英]Closing a socket created in a dll

I'm creating a modbus slave using the libmodbus.dll in python on Windows OS.我正在 Windows 操作系统上的 python 中使用 libmodbus.dll 创建一个 modbus 从站。 I've used ctypes to load the dll and make use of its features.我使用 ctypes 加载 dll 并利用其功能。 There is a sample code I'm mimicking here .我在这里模仿了一个示例代码。 One of the call's in the dll ends up opening a socket dll 中的一个调用最终打开了一个套接字

s = modbus_tcp_listen(ctx, 1);

At the end of the example it has a line where it close's the socket like so在示例的末尾,它有一条线像这样关闭套接字

close(s);

close() is not part of the libmodbus.dll. close()不是 libmodbus.dll 的一部分。 It's from the library #include <unistd.h> .它来自库#include <unistd.h> Is there a way to close the socket without having to obtain a dll with the close() functionality?有没有办法关闭套接字而不必获得具有close()功能的 dll ? If not, how would I obtain access to the close() command on a windows platform?如果没有,我将如何访问 windows 平台上的close()命令? From what I know unistd.h is for posix.据我所知unistd.h适用于 posix。

You can use the socket.close(fd) which takes the socket descriptor and closed it.您可以使用socket.close(fd)获取套接字描述符并关闭它。

import socket
...
server_socket = mbpi.modbus_tcp_listen(self._ctx, 1)
...
socket.close(server_socket.value) # Need to call attribute value since its a ctypes.c_int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM