为什么不能仅将Linux服务绑定到环回? [英] Why can't a bind linux service to the loop-back only?
问题描述
我正在编写一个服务器应用程序,该应用程序将在临时端口上提供服务,而我仅希望在环回接口上对其进行访问.为此,我正在编写如下代码:
I am writing a server application that will provide a service on an ephemeral port that I only want accessible on the loopback interface. In order to do this, I am writing code like the following:
struct sockaddr_in bind_addr;
memset(&bind_addr,0,sizeof(bind_addr));
bind_addr.sin_family = AF_INET;
bind_addr.sin_port = 0;
bind_addr.sin_addr.s_addr = htonl(inet_addr("127.0.0.1"));
rcd = ::bind(
socket_handle,
reinterpret_cast<struct sockaddr *>(&bind_addr),
sizeof(bind_addr));
此对bind()的调用的返回值为-1,而errno的值为99(无法分配请求的地址).是因为inet_addr()已经按网络顺序返回其结果而失败,还是有其他原因?
The return value for this call to bind() is -1 and the value of errno is 99 (Cannot assign requested address). Is this failing because inet_addr() already returns its result in network order or is there some other reason?
推荐答案
失败是因为inet_addr()已经按网络顺序返回了结果
Is this failing because inet_addr() already returns its result in network order
是的
因此删除htonl调用.
So remove the htonl call.
这篇关于为什么不能仅将Linux服务绑定到环回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!