c++ - 进一步检查 C 头文件

标签 c++ c visual-studio-2013 header-files msdn

我正在通过查看 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 DefinitionGo 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 *) &in;
   __snprintf (buffer, sizeof (buffer), "%d.%d.%d.%d",
               bytes[0], bytes[1], bytes[2], bytes[3]);

  return buffer;
}

取自inet_ntoa.cglibc 2.23

请记住,glibc 是开源的,所以如果您想探索一下并了解幕后发生的事情,请不要犹豫并下载它!

问候

关于c++ - 进一步检查 C 头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38458387/

相关文章:

c++ - 为什么 getchar_unlocked() 在 c/c++ 中更快?

c++ - wchar_t 数据是否需要字节序转换?

c++ - 我可以将使用 v120_xp 工具集构建的静态库链接到使用 VS2013 中的 v120 工具集构建的 EXE/DLL 吗?

visual-studio-2013 - Visual Studio 2013-调试->启动新实例

c++ - 创建一个基于所选选项卡选择主菜单栏的功能

c++ - 在 C++ 中初始化结构数组

c++ - 缺少包含不会在 RedHat 6 上产生编译错误

c - GNU C 库的实现在哪里?

矩阵窗口上的 Java 代码优化计算时间更长

c++ - 使用 binomial_heap 和 indirect_cmp