了解INADDR_ANY进行套接字编程 [英] Understanding INADDR_ANY for socket programming

查看:72
本文介绍了了解INADDR_ANY进行套接字编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对某些套接字进行编程,因此,在服务器端,我使用了 htonl(INADDR_ANY).就我所知,在我看来,该功能会生成一个随机IP(我正确吗?).实际上,我想将套接字与 localhost 绑定.但是,如果我运行此

I am trying to program some sockets and so, on the server side, I use htonl(INADDR_ANY). To the extent I understood, it seems to me that this function generates a random IP (am I correct ?). In fact, I want to bind my socket with my localhost. But if I run this

printf("%d",htonl(INADDR_ANY));

我得到0作为返回值.有人可以带来一些解释吗?

I get 0 as a return value. Could someone bring some explanation ?

推荐答案

    INADDR_ANY
  1. bind()确实生成随机IP".它将套接字绑定到所有可用接口.

  1. bind() of INADDR_ANY does NOT "generate a random IP". It binds the socket to all available interfaces.

对于服务器,通常需要绑定到所有接口-不仅是"localhost".

For a server, you typically want to bind to all interfaces - not just "localhost".

如果只想将套接字绑定到本地主机,则语法为 my_sockaddress.sin_addr.s_addr = inet_addr("127.0.0.1"); ,然后调用 bind(my_socket,(SOCKADDR *)& my_sockaddr,...).

If you wish to bind your socket to localhost only, the syntax would be my_sockaddress.sin_addr.s_addr = inet_addr("127.0.0.1");, then call bind(my_socket, (SOCKADDR *) &my_sockaddr, ...).

INADDR_ANY 是一个常量,恰好等于零":

As it happens, INADDR_ANY is a constant that happens to equal "zero":

http://www.castaglia.org/proftpd/doc/devel-guide/src/include/inet.h.html

# define INADDR_ANY ((unsigned long int) 0x00000000)
...
# define INADDR_NONE    0xffffffff
...
# define INPORT_ANY 0
...

  • 如果您还不熟悉它,我敦促您查看Beej的套接字编程指南:

  • If you're not already familiar with it, I urge you to check out Beej's Guide to Sockets Programming:

    http://beej.us/guide/bgnet/

    由于人们仍在阅读此内容,因此请注意:

    Since people are still reading this, an additional note:

    man(7)ip :

    当进程想要接收新的传入数据包或连接时,它应该使用 bind(2)将套接字绑定到本地接口地址.a>.

    When a process wants to receive new incoming packets or connections, it should bind a socket to a local interface address using bind(2).

    在这种情况下,只能将一个IP套接字绑定到任何给定的本地(地址,端口)对.在绑定调用中指定INADDR_ANY时,套接字将绑定到所有本地接口.

    In this case, only one IP socket may be bound to any given local (address, port) pair. When INADDR_ANY is specified in the bind call, the socket will be bound to all local interfaces.

    在未绑定的对象上调用 listen(2)时插座,插座是自动绑定到具有本地地址集的随机空闲端口到INADDR_ANY.

    When listen(2) is called on an unbound socket, the socket is automatically bound to a random free port with the local address set to INADDR_ANY.

    在未绑定的对象上调用 connect(2)时插座,插座是自动绑定到随机空闲端口或可用共享端口本地地址设置为INADDR_ANY ...

    When connect(2) is called on an unbound socket, the socket is automatically bound to a random free port or to a usable shared port with the local address set to INADDR_ANY...

    有几个特殊地址:INADDR_LOOPBACK(127.0.0.1)始终通过环回设备引用本地主机;INADDR_ANY(0.0.0.0)表示任何要绑定的地址...

    There are several special addresses: INADDR_LOOPBACK (127.0.0.1) always refers to the local host via the loopback device; INADDR_ANY (0.0.0.0) means any address for binding...

    也:

    bind()—将名称绑定到套接字:

    如果(sin_addr.s_addr)字段设置为常量INADDR_ANY,则为在netinet/in.h中定义,调用方正在请求将套接字绑定到主机上的所有网络接口.随后,UDP数据包和所有接口的TCP连接(与绑定名称匹配)被路由到应用程序.当服务器时这很重要为多个网络提供服务.通过离开地址未指定,服务器可以接受所有UDP数据包和TCP连接对其端口进行的请求,而不考虑其上的网络接口请求到达了.

    If the (sin_addr.s_addr) field is set to the constant INADDR_ANY, as defined in netinet/in.h, the caller is requesting that the socket be bound to all network interfaces on the host. Subsequently, UDP packets and TCP connections from all interfaces (which match the bound name) are routed to the application. This becomes important when a server offers a service to multiple networks. By leaving the address unspecified, the server can accept all UDP packets and TCP connection requests made for its port, regardless of the network interface on which the requests arrived.

    这篇关于了解INADDR_ANY进行套接字编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆