<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[[实践OK]socket网络字节序以及大端序小端序，htons，ntohs，htonl，ntohl。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Unix/LinuxC技术]]></category>
<pubDate>Fri, 29 Dec 2017 13:35:48 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	常见的网络字节转换函数有：<br/>htons()：host to network short，将short类型数据从主机字节序转换为网络字节序。<br/>ntohs()：network to host short，将short类型数据从网络字节序转换为主机字节序。<br/>htonl()：host to network long，将long类型数据从主机字节序转换为网络字节序。<br/>ntohl()：network to host long，将long类型数据从网络字节序转换为主机字节序。<br/><br/>通常，以s为后缀的函数中，s代表2个字节short，因此用于端口号转换；以l为后缀的函数中，l代表4个字节的long，因此用于IP地址转换。<br/>————————————————————————————————————————————————————————————————<br/>cat hto.c <br/><textarea name="code" class="php" rows="15" cols="100">
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
int main()&#123;
&nbsp;&nbsp;&nbsp;&nbsp;unsigned short host_port = 0x1234, net_port;
&nbsp;&nbsp;&nbsp;&nbsp;unsigned long host_addr = 0x12345678, net_addr;
&nbsp;&nbsp;&nbsp;&nbsp;net_port = htons(host_port);
&nbsp;&nbsp;&nbsp;&nbsp;net_addr = htonl(host_addr);
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Host ordered port: %#x&#92;n&quot;, host_port);
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Network ordered port: %#x&#92;n&quot;, net_port);
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Host ordered address: %#lx&#92;n&quot;, host_addr);
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Network ordered address: %#lx&#92;n&quot;, net_addr);
&nbsp;&nbsp;&nbsp;&nbsp;return 0;
&#125;
</textarea><br/><br/>make hto<br/>cc&nbsp;&nbsp;&nbsp;&nbsp; hto.c&nbsp;&nbsp; -o hto<br/><br/>#./hto<br/>Host ordered port: 0x1234<br/>Network ordered port: 0x3412<br/>Host ordered address: 0x12345678<br/>Network ordered address: 0x78563412<br/><br/><br/>cat inetaddr.c <br/><textarea name="code" class="php" rows="15" cols="100">
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;arpa/inet.h&gt;
int main()&#123;
&nbsp;&nbsp;&nbsp;&nbsp;char *addr1 = &quot;192.168.1.1&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;char *addr2 = &quot;192.168.1.11&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;unsigned long conv_addr = inet_addr(addr1);
&nbsp;&nbsp;&nbsp;&nbsp;if(conv_addr == INADDR_NONE)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts(&quot;Error occured!&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;else&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Network ordered integer addr: %#lx&#92;n&quot;, conv_addr);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;conv_addr = inet_addr(addr2);
&nbsp;&nbsp;&nbsp;&nbsp;if(conv_addr == INADDR_NONE)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts(&quot;Error occured!&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;else&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Network ordered integer addr: %#lx&#92;n&quot;, conv_addr);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;return 0;
&#125;
</textarea><br/><br/><br/>make inetaddr&nbsp;&nbsp;<br/>cc&nbsp;&nbsp;&nbsp;&nbsp; inetaddr.c&nbsp;&nbsp; -o inetaddr<br/><br/>./inetaddr <br/>Network ordered integer addr: 0x101a8c0<br/>Network ordered integer addr: 0xb01a8c0<br/><br/>From:http://c.biancheng.net/cpp/html/3047.html
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]socket网络字节序以及大端序小端序，htons，ntohs，htonl，ntohl。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>