C 语言中带有 getaddrinfo() 的 Unix 套接字
有谁知道是否可以在 C (AF_UNIX) 中将 getaddrinfo 与 unix 套接字一起使用。我尝试了一些方法,但无法使其发挥作用。 这基本上就是我正在尝试的:
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNIX;
hints.ai_socktype = SOCK_STREAM;
if(getaddrinfo("What should I put here?", "What should I put here?", &hints, &res) != 0){
//do sth about
}
我的问题是如何填充节点和服务字段,以防可以将其与 unix 套接字一起使用。
提前致谢。
Does anyone know if it's possible to use getaddrinfo with unix sockets in C (AF_UNIX). I've tried a couple of things but I can't make it work.
This is basically what I'm trying:
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNIX;
hints.ai_socktype = SOCK_STREAM;
if(getaddrinfo("What should I put here?", "What should I put here?", &hints, &res) != 0){
//do sth about
}
My question is how to fill the node and service fields, in case that is possible to use it with unix sockets.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
getaddrinfo()
的某些实现支持AF_UNIX
套接字地址,但出于安全考虑,它们不再这样做。无论如何,您实际上并不需要一个函数来“查找”
AF_UNIX
地址 - 如果您知道套接字路径,那么您可以将其直接复制到sockaddr_un
中足够的尺寸。AF_UNIX
地址没有解析步骤 - 套接字名称是套接字地址。Some implementations of
getaddrinfo()
have supportedAF_UNIX
socket addresses, but they no longer do so due to security concerns.You don't really need a function to "look up" an
AF_UNIX
address anyway - if you know the socket path, then you can just copy it straight into asockaddr_un
of sufficient size. There's no resolution step forAF_UNIX
addresses - the socket name is the socket address.来自
man 3 getaddrinfo
:所以看起来你需要传入
hints
参数来告诉它你想要一个 unix 套接字。From
man 3 getaddrinfo
:So it looks like you need to pass in the
hints
parameter to tell it you want a unix socket.晚了很多,但只是为了记录,你尝试过这个 -
struct addrinfo 提示,*res;
parm1 ->;是要连接的主机名或地址。此处使用 NULL 的调用会放置本地主机的地址,在这种情况下您指定hints.ai_flags=AI_PASSIVE。
parm2 ->;是预期服务的名称(http、ftp 等)或套接字所用于的该服务的相应端口号
A lot late, but just for the records, did you try this -
struct addrinfo hints, *res;
parm1 -> is the host name or address to connect to. A call with NULL here puts the address of local host, in which case you specify hints.ai_flags=AI_PASSIVE.
parm2 -> is the name of the intended service (http, ftp etc) or the corresponding port number for that service the socket is used for