C语言 in_addr_t inet_ntoa 等类型的地址是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6530578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:02:37  来源:igfitidea点击:

what are addresses of type in_addr_t inet_ntoa etc

csocketsnetwork-programmingin-addr

提问by Registered User

Ok before posting this question I have checked threads here in_addr_t to string

好的,在发布这个问题之前,我已经检查了in_addr_t 中的线程 到字符串

my question is different.

我的问题是不同的。

I understand that inet_ntoa function is of following type

我了解 inet_ntoa 函数属于以下类型

char * inet_ntoa (struct in_addr in);

What I want to know is what is in_addr_t

我想知道的是什么 in_addr_t

what will be the representation when it is used in a socket program.

在套接字程序中使用时的表示是什么。

I checked the structure definition in <netinet/in.h>

我检查了结构定义 <netinet/in.h>

   typedef uint32_t in_addr_t;

   struct in_addr {
       in_addr_t s_addr;
   };

is of above type. I do not want to print in_addr_t to char * as inet_ntoa does. I want to see what is in_addr_t.So how can it be done or what exactly is in_addr_t and why is it used again and again in socket programming. If I am not mistaken it is defined as

属于上述类型。我不想像 inet_ntoa 那样将 in_addr_t 打印到 char * 。我想看看 in_addr_t 是什么。所以怎么做,或者 in_addr_t 到底是什么,为什么在套接字编程中一次又一次地使用它。如果我没记错的话,它被定义为

typedef uint32_t in_addr_t;

typedef uint32_t in_addr_t;

So if in_addr_tis unsigned 32 bitint I want to see what is that and not the char *which prints 192.168.1.1 kind of output on stdout.

所以如果in_addr_tunsigned 32 bitint 我想看看那是什么,而不是char *在标准输出上打印 192.168.1.1 类型的输出。

回答by cnicutar

As we all know (we do, right?) an IPv4address is represented using an unsigned 32 bits integer. Us puny humans aren't so good with large numbers (or small ones) so we use dot decimal notation. (Did you know you're at 1076000524right now ? Neither did I).

众所周知(我们知道,对吗?)IPv4地址使用无符号 32 位整数表示。我们这些弱小的人类不太擅长处理大数字(或小数字),所以我们使用点十进制表示法。(你知道你现在在1076000524吗?我也不知道)。

However, since you dug through netinetand found out what is clearly specified by the standard

但是,由于您深入挖掘netinet并发现了标准明确规定的内容

The <netinet/in.h>header shall define the in_addrstructure, which shall include at least the following member:

in_addr_t s_addr

<netinet/in.h>报头应定义in_addr结构,其中应至少包括以下部件包括:

in_addr_t s_addr

Which is

这是

in_addr_tEquivalent to the type uint32_tas described in <inttypes.h>

in_addr_t等同于uint32_t描述的类型<inttypes.h>

you can print it.

你可以打印出来。

printf("My unreadable addres is %u\n", in.s_addr);

Quick, what does 134744072stand for ?

快,代表什么134744072