版权声明:本博客文章,大多是本人整理编写,或在网络中收集,转载请注明出处! https://blog.csdn.net/tennysonsky/article/details/45045549 </div>
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-cd6c485e8b.css">
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-cd6c485e8b.css">
<div class="htmledit_views" id="content_views">
<h1><a name="t0"></a><span style="font-family:SimSun;font-size:18px;">网络字节序</span></h1><h2><a name="t1"></a><strong><span style="font-family:SimSun;font-size:14px;">故事的起源</span></strong></h2><span style="font-family:SimSun;font-size:14px;">“endian”这个词出自《格列佛游记》。小人国的内战就源于吃鸡蛋时是究竟从大头(Big-Endian)敲开还是从小头(Little-Endian)敲开,由此曾发生过六次叛乱,其中一个皇帝送了命,另一个丢了王位。<br></span><p></p><p><span style="font-family:SimSun;font-size:14px;"><br></span></p><p><span style="font-family:SimSun;font-size:14px;">我们一般将“endian”翻译成“字节序”,将 Big-Endian 和 Little-Endian 称作“大端格式”和“小端格式”。<br></span></p><p><span style="font-family:SimSun;font-size:14px;"><br></span></p><h2><a name="t2"></a><strong><span style="font-family:SimSun;font-size:14px;">字节序</span></strong></h2><p><span style="font-family:SimSun;font-size:14px;"><span>字节序</span>是指多字节数据的存储顺序,在设计计算机系统的时候,有两种处理内存中数据的方法:大端格式、小端格式。</span></p><p><span style="font-family:SimSun;font-size:14px;"><br></span></p><p><span style="font-family:SimSun;font-size:14px;">小端格式(Little-Endian):将低位字节数据存储在低地址。<br>大端格式(Big-Endian):将高位字节数据存储在低地址。</span><br></p><p style="text-align:center;"><span style="font-size:24px;"><img src="https://img-blog.csdn.net/20150603164653467" alt=""><br></span></p><p><span style="font-size:14px;"><br></span></p><p><span style="font-size:14px;">举个简单的例子,对于整形 0x12345678,它在大端格式和小端格式的系统中,分别如下图所示的方式存放:</span><br></p><p style="text-align:center;"><span style="font-size:24px;"><img src="https://img-blog.csdn.net/20150414180347022" alt=""><br></span></p><p><span style="font-size:14px;">下面例子为确定主机的字节序:</span></p><p><span style="font-size:24px;"></span></p><pre><code class="language-cpp hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><stdio.h></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">char</span> *argv[])</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">{</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> a = <span class="hljs-number">0x12345678</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> *p = (<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> *)&a; <span class="hljs-comment">//只取一个字节</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">if</span>(<span class="hljs-number">0x12</span> == *p){</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Big-Endian
"</span>);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> }<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(<span class="hljs-number">0x78</span> == *p){</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Little-Endian
"</span>);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> }</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">}</div></div></li></ol></code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><br><p></p><span style="font-size:14px;">网络上的数据流是字节流,对于一个多字节数值,在进行网络传输的时候,先传递哪个字节?也就是说,当接收端收到第一个字节的时候,它是将这个字节作为高位还是低位来处理呢?<br> <br>网络字节序定义:收到的第一个字节被当作高位看待,这就要求发送端发送的第一个字节应当是高位。而在发送端发送数据时,发送的第一个字节是该数字在内存中起始地址对应的字节。可见多字节数值在发送前,在内存中数值应该以大端法存放。 <br><br>所以,<span style="color:rgb(255,0,0);"><strong>网络协议指定了通讯字节序:大端</strong></span>。只有在多字节数据处理时才需要考虑字节序,运行在同一台计算机上的进程相互通信时,一般不用考虑字节序,异构计算机之间通讯,需要转换自己的字节序为网络字节序。<br></span><br><h2><a name="t3"></a><strong><span style="font-size:18px;">字节序转换函数介绍</span></strong></h2><p><span style="font-size:14px;">以下接口所需头文件:#include <arpa/inet.h></span><br></p><p><span style="font-size:24px;"><span style="color:#ff0000;"><br></span></span></p><p><span><span style="font-size:14px;"><span style="color:rgb(255,0,0);"><strong>uint32_t htonl(uint32_t hostint32);</strong></span><br></span></span></p><p><span><span style="font-size:14px;">功能:</span></span></p><blockquote style="border:none;"><p><span><span style="font-size:14px;">将 32 位主机字节序数据转换成网络字节序数据</span></span></p></blockquote><p><span><span style="font-size:14px;">参数:</span></span></p><blockquote style="border:none;"><p><span><span style="font-size:14px;"><span style="color:#ff0000;">hostint32</span>:需要转换的 32 位主机字节序数据,uint32_t 为 32 为无符号整型</span></span></p></blockquote><p><span><span style="font-size:14px;">返回值:</span></span></p><blockquote style="border:none;"><p><span><span style="font-size:14px;">成功:返回网络字节序的值</span></span></p></blockquote><p><span><span style="font-size:14px;"><br></span></span></p><p><span><span style="font-size:14px;"><span style="color:#ff0000;"><strong>uint16_t htons(uint16_t hostint16);</strong></span><br></span></span></p><p><span><span style="font-size:14px;">功能:</span></span></p><blockquote style="border:none;"><p><span><span style="font-size:14px;">将 16 位主机字节序数据转换成网络字节序数据</span></span></p></blockquote><p><span><span style="font-size:14px;">参数:</span></span></p><blockquote style="border:none;"><p><span><span style="font-size:14px;"><span style="color:#ff0000;">hostint16</span>:需要转换的 16 位主机字节序数据,uint16_t,unsigned short int</span></span></p></blockquote><p><span><span style="font-size:14px;">返回值:</span></span></p><blockquote style="border:none;"><p><span><span style="font-size:14px;">成功:返回网络字节序的值</span></span></p></blockquote><p><span><span style="font-size:14px;"><br></span></span></p><p><span><span style="font-size:14px;">测试示例:</span></span></p><p><span style="font-size:24px;"></span></p><pre><code class="language-cpp hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><stdio.h></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><arpa/inet.h></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">char</span> *argv[])</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">{</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">int</span> a = <span class="hljs-number">0x01020304</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">short</span> <span class="hljs-keyword">int</span> b = <span class="hljs-number">0x0102</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"htonl(0x%08x) = 0x%08x
"</span>, a, htonl(a));</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"htons(0x%04x) = 0x%04x
"</span>, b, htons(b));</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">}</div></div></li></ol></code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><br><p></p><span style="font-size:14px;">运行结果如下:</span><p></p><p><span style="font-size:24px;"><img src="https://img-blog.csdn.net/20150414192338539" alt=""><br><br></span></p><p><span><span style="font-size:14px;"><span style="color:rgb(255,0,0);"><strong>uint32_t ntohl(uint32_t netint32);</strong></span><br></span></span></p><p><span><span style="font-size:14px;">功能:</span></span></p><blockquote style="border:none;"><p><span><span style="font-size:14px;">将 32 位网络字节序数据转换成主机字节序数据</span></span></p></blockquote><p><span><span style="font-size:14px;">参数:</span></span></p><blockquote style="border:none;"><p><span><span style="font-size:14px;"><span style="color:#ff0000;">netint32</span>:待转换的 32 位网络字节序数据,uint32_t,unsigned int</span></span></p></blockquote><p><span><span style="font-size:14px;">返回值:</span></span></p><blockquote style="border:none;"><p><span><span style="font-size:14px;">成功:返回主机字节序的值</span></span></p></blockquote><p><span><span style="font-size:14px;"><br></span></span></p><p><span><span style="font-size:14px;"><span style="color:#ff0000;"><strong>uint16_t ntohs(uint16_t netint16);</strong></span><br></span></span></p><p><span><span style="font-size:14px;">功能:</span></span></p><blockquote style="border:none;"><p><span><span style="font-size:14px;">将 16 位网络字节序数据转换成主机字节序数据</span></span></p></blockquote><p><span><span style="font-size:14px;">参数:</span></span></p><blockquote style="border:none;"><p><span><span style="font-size:14px;"><span style="color:#ff0000;">netint16</span>:待转换的 16 位网络字节序数据,uint16_t,unsigned short int</span></span></p></blockquote><p><span><span style="font-size:14px;">返回值:</span></span></p><p><span><span style="font-size:14px;">成功:返回主机字节序的值</span></span><br></p><p><span style="font-size:24px;"><br></span></p><h1><a name="t4"></a><span style="font-size:18px;">地址转换函数</span></h1><div><span><span style="font-size:14px;">以下接口所需头文件:#include <arpa/inet.h><br></span></span></div><div><span><span style="font-size:14px;"><br></span></span></div><p><span><span style="font-size:14px;"><span style="color:#ff0000;"><strong>int inet_pton(int family, const char *strptr, void *addrptr);</strong></span><br></span></span></p><p><span><span style="font-size:14px;">功能:</span></span></p><blockquote style="border:none;"><p><span><span style="font-size:14px;">将点分十进制数串转换成 32 位无符号整数</span></span></p></blockquote><p><span><span style="font-size:14px;">参数:</span></span></p><blockquote style="border:none;"><p><span><span style="font-size:14px;"><span style="color:#ff0000;">family</span>:协议族( AF_INET、AF_INET6、PF_PACKET 等 ),常用 AF_INET</span></span></p><p><span><span style="font-size:14px;"><span style="color:#ff0000;">strptr</span>:点分十进制数串</span></span></p><p><span><span style="font-size:14px;"><span style="color:#ff0000;">addrptr</span>:32 位无符号整数的地址</span></span></p></blockquote><p><span><span style="font-size:14px;">返回值:</span></span></p><blockquote style="border:none;"><p><span><span style="font-size:14px;">成功返回 1 、 失败返回其它</span></span></p><p><span><span style="font-size:14px;"><br></span></span></p></blockquote><p><span><span style="font-size:14px;">测试示例:</span></span></p><p><span style="font-size:24px;"></span></p><pre><code class="language-cpp hljs"><ol class="hljs-ln" style="731px"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><stdio.h></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><arpa/inet.h></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">{</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">char</span> ip_str[]=<span class="hljs-string">"172.20.223.75"</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> ip_uint = <span class="hljs-number">0</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> *ip_p = <span class="hljs-literal">NULL</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> inet_pton(AF_INET,ip_str,&ip_uint);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"in_uint = %d
"</span>,ip_uint);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> ip_p = (<span class="hljs-keyword">char</span> *)&ip_uint;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"in_uint = %d,%d,%d,%d
"</span>,*ip_p,*(ip_p+<span class="hljs-number">1</span>),*(ip_p+<span class="hljs-number">2</span>),*(ip_p+<span class="hljs-number">3</span>));</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">}</div></div></li></ol></code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><br><p></p><span style="font-family:SimSun;font-size:14px;">运行结果如下:</span><p><img src="https://img-blog.csdn.net/20150414193439497" alt="" style="font-size:24px;"></p><p><span style="font-size:24px;"><br></span></p><p><strong><span style="color:rgb(255,0,0);"><span style="font-size:14px;">const char *inet_ntop( int family, </span></span><span style="font-size:14px;color:rgb(255,0,0);">const void *addrptr,</span><span style="font-size:14px;color:rgb(255,0,0);"></span><span style="font-size:14px;color:rgb(255,0,0);"> </span><span style="color:rgb(255,0,0);font-size:14px;">char *strptr, </span><span style="font-size:14px;color:rgb(255,0,0);">size_t len );</span></strong></p><p><span style="font-size:14px;">功能:</span></p><blockquote style="border:none;"><p><span><span style="font-size:14px;">将 32 位无符号整数转换成点分十进制数串</span></span></p></blockquote><p><span><span style="font-size:14px;">参数:</span></span></p><blockquote style="border:none;"><p><span><span style="font-size:14px;"><span style="color:#ff0000;">family</span>:协议族( AF_INET、AF_INET6、PF_PACKET 等 ),常用 AF_INET</span></span></p><p><span><span style="font-size:14px;"><span style="color:#ff0000;">addrptr</span>:32 位无符号整数</span></span></p><p><span><span style="font-size:14px;"><span style="color:#ff0000;">strptr</span>:点分十进制数串</span></span></p><p><span><span style="font-size:14px;"><span style="color:#ff0000;">len</span>:strptr 缓存区长度</span></span></p></blockquote><blockquote style="border:none;"><blockquote style="border:none;"><span><span style="font-size:14px;">len 的宏定义</span></span></blockquote><blockquote style="border:none;"><span><span style="font-size:14px;">#define INET_ADDRSTRLEN 16 // for ipv4</span></span></blockquote><blockquote style="border:none;"><p><span><span style="font-size:14px;">#define INET6_ADDRSTRLEN 46 // for ipv6</span></span></p></blockquote></blockquote><p><span><span style="font-size:14px;">返回值:</span></span></p><blockquote style="border:none;"><p><span><span style="font-size:14px;">成功:则返回字符串的首地址</span></span></p><p><span><span style="font-size:14px;">失败:返回 NULL</span></span></p><p><span><span style="font-size:14px;"><br></span></span></p></blockquote><p><span><span style="font-size:14px;">测试示例:</span></span></p><p><span style="font-size:24px;"></span></p><pre><code class="language-cpp hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><stdio.h></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><arpa/inet.h></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">{</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> ip[] = {<span class="hljs-number">172</span>,<span class="hljs-number">20</span>,<span class="hljs-number">223</span>,<span class="hljs-number">75</span>};</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">char</span> ip_str[<span class="hljs-number">16</span>] = <span class="hljs-string">"NULL"</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> inet_ntop(AF_INET,(<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> *)ip,ip_str,<span class="hljs-number">16</span>);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"ip_str = %s
"</span>,ip_str);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">}</div></div></li></ol></code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><br><p></p><span style="font-size:14px;">运行结果如下:</span><p></p><p><img src="https://img-blog.csdn.net/20150414194040438" alt=""><br></p><p><br></p><p><strong><span style="font-size:18px;"><a href="http://download.csdn.net/detail/tennysonsky/8594685" rel="nofollow">测试示例代码下载请点此处。</a></span></strong></p><p><strong><img src="https://img-blog.csdn.net/20180210174507305" alt=""><br></strong></p><p><strong><br></strong></p> </div>
</div>
</article>