AF_UNIX equivalent for Windows [duplicate]

限于喜欢 提交于 2019-12-21 07:48:27

问题


I want to know how to use something that behaves like Unix Domain Socket on Windows.

The behaviour is: A process will be a "server" and receive connections from other processes and it can keep and use connections from different processes, as a TCP socket does.

IP socket is not a solution, because it requires to choose a port, processes from other applications may need the chosen port, and the open port may be seen in the network.
I do not know if named pipes can receive and keep multiple clients, but I did not see how to distinguish different clients. And it does not provide a way to wait data from multiple connections with something like select.

An old question says about using named pipes, but it does not explain how to use named pipes to get the desired behaviour. AF_UNIX in windows
I did not see how to get the handle of a specific client.


回答1:


Windows has recently (Windows 10 Insider build 17063) implemented support for AF_UNIX, so you can use it in future windows builds.

However not all of it is implemented, the below features don't work.

  • AF_UNIX datagram (SOCK_DGRAM) or sequence packet (SOCK_SEQPACKET) socket type.
  • Ancillary data: Linux's unix socket implementation supports passing ancillary data such as passing file descriptors (SCM_RIGHTS) or credentials (‘SCM_CREDENTIALS`) over the socket. There is no support for ancillary data in the Windows unix socket implementation.
  • Autobind feature (see the section on ‘sockaddr_un’ for details).
  • socketpair: socketpair socket API is not supported in Winsock 2.0.

Source: https://blogs.msdn.microsoft.com/commandline/2017/12/19/af_unix-comes-to-windows/




回答2:


Probably not the answer you want to hear, but COM is one of several standard mechanisms to achieve inter-process communication in Windows. It has it's issues that annoy developers - but it works quite well for all the requirements you listed.

As for IP sockets, you mentioned the issue of "can be seen in the network". This is not the case if you just simply bind your server socket to the localhost address (127.0.0.1).

SOCKET s;
const DWORD LOCAL_HOST_IP = 0x7f000001; // 127.0.0.1
sockaddr_in addrLocal = {};

s = socket(AF_INET, SOCK_STREAM, 0);
addrLocal.sin_family = AF_INET;
addrLocal.sin_port = htons(YOUR_APPLICATION_PORT);
addrLocal.sin_addr.s_addr = htonl(LOCAL_HOST_IP);
s = SOCKET(AF_INET, SOCK_STREAM, 0);
bind(s, (sockadr*)&addrLocal, sizeof(addrLocal));



回答3:


I have found the answer.

The big difference is that the handle the waits a connection is the same that does communications to a client. I would have to create a new named pipe for the server to wait for the next client.

References:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365799%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365588%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365603%28v=vs.85%29.aspx



来源:https://stackoverflow.com/questions/9029174/af-unix-equivalent-for-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!