我正在通过查看 Microsoft Visual Basic 2013 中有效数据包嗅探器的代码来学习 C 语言网络。
下面的代码创建了一个指向hostent 结构 的指针,获取localhost 主机名,并将其加载到名为hostent 结构 中em>本地。
struct hostent *local;
gethostname(hostname, sizeof(hostname))
local = gethostbyname(hostname);
下一部分使地址能够以点分十进制表示法打印。
for (i = 0; local->h_addr_list[i] != 0; ++i)
{
memcpy(&addr, local->h_addr_list[i], sizeof(struct in_addr));
printf(" Interface Number : %d Address : %s\n",i,inet_ntoa(addr));
}
现在,我想了解这一切是如何工作的以及更多。 . .
假设我想了解 inet_ntoa(),我右键单击并选择 Go To Definition 或 Go To Declaration,它会将我转到 WinSock2.h 这表明:
inet_ntoa(
__in struct in_addr in
);
这似乎是在向我展示参数,而不是函数的工作原理或返回值。这意味着我必须引用 MSDN 才能了解每次发生的情况。
我的问题是:读取正在发生的事情的代码在哪里,这样我就不必使用文档了?
例如inet_ntoa
函数内容在哪里?
最佳答案
给你:
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
/* The interface of this function is completely stupid, it requires a
static buffer. We relax this a bit in that we allow one buffer for
each thread. */
static __thread char buffer[18];
char *inet_ntoa (struct in_addr in)
{
unsigned char *bytes = (unsigned char *) ∈
__snprintf (buffer, sizeof (buffer), "%d.%d.%d.%d",
bytes[0], bytes[1], bytes[2], bytes[3]);
return buffer;
}
取自inet_ntoa.c,glibc 2.23
请记住,glibc 是开源的,所以如果您想探索一下并了解幕后发生的事情,请不要犹豫并下载它!
问候
关于c++ - 进一步检查 C 头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38458387/