简体   繁体   中英

Closing a socket created in a dll

I'm creating a modbus slave using the libmodbus.dll in python on Windows OS. I've used ctypes to load the dll and make use of its features. There is a sample code I'm mimicking here . One of the call's in the dll ends up opening a socket

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. It's from the library #include <unistd.h> . Is there a way to close the socket without having to obtain a dll with the close() functionality? If not, how would I obtain access to the close() command on a windows platform? From what I know unistd.h is for posix.

You can use the socket.close(fd) which takes the socket descriptor and closed it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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