Windows NAmed Pipes在Linux中的替代方案 [英] Windows NAmed Pipes alternative in Linux
问题描述
我们正在将现有的Windows代码移植到Linux.我们正在使用ACE作为抽象层. 我们正在使用名为管道的窗口与多个客户端进行通信并执行重叠的操作.
We are porting existing windows code to Linux. We are using ACE as abstraction layer. We are using windows named pipes for communicating with multiple clients and to perform overlapped operations .
在Linux中这等效于什么.我已经检查了名为管道(FIFO)的Linux,但是它们似乎仅支持一个客户端和服务器,并且不支持重叠的IO.
What is the equivalent to this in linux. I have checked linux named pipes(FIFO), but they seem to support only one client and server and do not support overlapped IO.
关于此事,您能指导我吗?
Can you guide me regarding this.
推荐答案
Unix套接字.本质上,
Unix sockets. Essentially,
- 致电
socket(PF_UNIX, SOCK_STREAM, 0)
.这将返回文件描述符或错误时返回-1. - 使用类似
struct sockaddr_un addr; bzero(addr); addr.sun_len = sizeof(addr); addr.sun_family = PF_UNIX; strncpy(addr.sun_path, "/path/to/file", sizeof(addr.sun_path)-1);
的方法来创建套接字地址. - 致电
bind(fd, &addr, sizeof(addr))
. - 呼叫
listen(fd,backlog)
来监听连接.backlog
是可以存在的未被接受的连接数. - 使用accept()接受来自客户端的连接.这会返回新的FD或错误时为-1.
- 让客户端创建一个类似的套接字,并将connect()连接到该地址(我认为它们不必绑定).
- 完成后删除文件/path/to/file(或者将其保留在此处,以备日后重用).
- Call
socket(PF_UNIX, SOCK_STREAM, 0)
. This returns a file descriptor or -1 on error. - Use something like
struct sockaddr_un addr; bzero(addr); addr.sun_len = sizeof(addr); addr.sun_family = PF_UNIX; strncpy(addr.sun_path, "/path/to/file", sizeof(addr.sun_path)-1);
to create the socket address. - Call
bind(fd, &addr, sizeof(addr))
. - call
listen(fd,backlog)
to listen for connections.backlog
is the number of un-accept()ed connections that can exist. - Use accept() to accept connections from clients. This returns a new FD or -1 on error.
- Have clients create a similar socket and connect() to the address (I don't think they have to bind).
- Remove the file /path/to/file when done (or just leave it there if you're going to reuse it later).
我不确定Unix套接字是否支持SOCK_DGRAM(如果这样,可能类似于UDP).
I'm not sure if SOCK_DGRAM is supported for Unix sockets (if so, it's probably UDP-like).
请参见手册页以了解socket(2),bind(2),listen(2),accept(2),connect(2),unix(4),setsockopt(2).
See the man pages for socket(2), bind(2), listen(2), accept(2), connect(2), unix(4), setsockopt(2).
有关重叠的I/O",请参见select(2).您还可以使用fcntl(fd,F_SETFL,(int)(fcntl(fd,F_GETFL)|O_NONBLOCK))
启用非阻塞IO(请参阅fcntl(2)),这意味着read()和write()从不阻塞(这意味着write()可以返回short,因此您需要查看返回值) ).
For "overlapped I/O", see select(2). You can additionally enable non-blocking IO with fcntl(fd,F_SETFL,(int)(fcntl(fd,F_GETFL)|O_NONBLOCK))
(see fcntl(2)), this means read() and write() never block (which means write() can return short, so you need to look at the return value).
我不太确定Windows命名管道如何表示来自多个客户端的多个连接,但是在UNIX中,每个连接获得一个文件描述符(为侦听"套接字添加一个文件描述符).
I'm not quite sure how Windows named pipes represent multiple connections from multiple clients, but in UNIX, you get one file descriptor per connection (plus one for the "listening" socket).
这篇关于Windows NAmed Pipes在Linux中的替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!