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

查看:58
本文介绍了理解 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 ?

推荐答案

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

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

对于服务器,您通常希望绑定到所有接口——而不仅仅是本地主机".

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).

    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...

    还有:

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

    如果 (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天全站免登陆