﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-LIULIANG</title><link>http://www.cppblog.com/LIULIANG/</link><description /><language>zh-cn</language><lastBuildDate>Fri, 17 Apr 2026 15:04:03 GMT</lastBuildDate><pubDate>Fri, 17 Apr 2026 15:04:03 GMT</pubDate><ttl>60</ttl><item><title>Ubuntu下进行安装视频录制</title><link>http://www.cppblog.com/LIULIANG/archive/2013/10/10/203648.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Thu, 10 Oct 2013 14:20:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2013/10/10/203648.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/203648.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2013/10/10/203648.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/203648.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/203648.html</trackback:ping><description><![CDATA[<p>因为工作需要，所以到网上搜索在Ubuntu系统下的屏幕录制软件。在搜索中遇到RecordMyDesktop这个软件，亲自安装试用了一下操作简单易用。声音和画面可以同步录制。在Ubuntu系统下需要炫自己屏幕作品的人可以下载试用一下。</p>
<p>文件名：recordMyDesktop</p>
<p>参考文献出处：<a href="http://sosyi.iteye.com/blog/1130992">&nbsp;<wbr> http://sosyi.iteye.com/blog/1130992</a></p>
<p>目前为止我只在Gnome桌面环境下使用，所以就gnome环境下的安装方法简要说明。按装方法很简单，在终端输入一下命令就可以安装。</p>
<p>命令行：sudo apt-get install gtk-recordmydesktop</p>
<p>依照上面的文献，KDE环境的用户输入以下命令</p>
<p>命令行：sudo apt-get install qt-recordmydesktop</p>
<p>安装完成后可以在 Applications -&gt; Video &amp; Sound 下面找到。<br />使用recordMyDesktop录制的视频是ogv格式的视频，将其转换为avi格式的，就要使用到mencoder软件，<br />安装mencoder软件：<br />sudo apt-get install mencoder<br />转换：<br />mencoder out.ogv -o out.avi -oac mp3lame -ovc lavc<br />如果不需要声音的话，可以这样<br />mencoder out.ogv -o out.avi -nosound -ovc lavc<br /><br />来自：<br /><a href="http://blog.sina.com.cn/s/blog_4a504d380100yees.html">http://blog.sina.com.cn/s/blog_4a504d380100yees.html</a><br /><a href="http://www.cnblogs.com/linuxcat/archive/2012/10/04/2711389.html">http://www.cnblogs.com/linuxcat/archive/2012/10/04/2711389.html</a></p><img src ="http://www.cppblog.com/LIULIANG/aggbug/203648.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2013-10-10 22:20 <a href="http://www.cppblog.com/LIULIANG/archive/2013/10/10/203648.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Struts2文件上传的大小限制问题  转载</title><link>http://www.cppblog.com/LIULIANG/archive/2013/07/06/201564.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Sat, 06 Jul 2013 08:07:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2013/07/06/201564.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/201564.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2013/07/06/201564.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/201564.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/201564.html</trackback:ping><description><![CDATA[<div class="postbody"><strong style="color: red">问题：上传大文件大小限制</strong><strong><br /><br /></strong></div>
<div class="postbody">解决方案：修改struts.xml文件中的参数如下
<div style="font-size: 13px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; border-bottom: #cccccc 1px solid; word-break: break-all; padding-bottom: 4px; padding-top: 4px; padding-left: 4px; border-left: #cccccc 1px solid; padding-right: 5px; width: 98%; background-color: #eeeeee"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #0000ff">&lt;</span><span style="color: #800000">constant&nbsp;</span><span style="color: #ff0000">name</span><span style="color: #0000ff">="struts.multipart.maxSize"</span><span style="color: #ff0000">&nbsp;value</span><span style="color: #0000ff">="55000000"</span><span style="color: #0000ff">/&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">&lt;</span><span style="color: #800000">action&nbsp;</span><span style="color: #ff0000">name</span><span style="color: #0000ff">="UploadFile"</span><span style="color: #ff0000">&nbsp;class</span><span style="color: #0000ff">="com.buptisc.srpms.action.UploadFileAction"</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">result&nbsp;</span><span style="color: #ff0000">name</span><span style="color: #0000ff">="UploadFileResult"</span><span style="color: #0000ff">&gt;</span><span style="color: #000000">/pages/ShowOtherFiles.jsp</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">result</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">result&nbsp;</span><span style="color: #ff0000">name</span><span style="color: #0000ff">="JGBsuccess"</span><span style="color: #0000ff">&gt;</span><span style="color: #000000">/pages/JGBdetail.jsp</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">result</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">interceptor-ref&nbsp;</span><span style="color: #ff0000">name</span><span style="color: #0000ff">="fileUpload"</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">param&nbsp;</span><span style="color: #ff0000">name</span><span style="color: #0000ff">="savePath"</span><span style="color: #0000ff">&gt;</span><span style="color: #000000">/data</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">param</span><span style="color: #0000ff">&gt;</span><span style="color: #000000">&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">param&nbsp;</span><span style="color: #ff0000">name</span><span style="color: #0000ff">="maximumSize"</span><span style="color: #0000ff">&gt;</span><span style="color: #000000">52428800</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">param</span><span style="color: #0000ff">&gt;</span><span style="color: #000000">&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">interceptor-ref</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">interceptor-ref&nbsp;</span><span style="color: #ff0000">name</span><span style="color: #0000ff">="defaultStack"</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">interceptor-ref</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">action</span><span style="color: #0000ff">&gt;</span></div></div>
<p>struts.xml文件中的大小与实际文件大小的关系：<span style="color: rgb(255,0,0)">1048576（Bytes） = 1024*1024 = 1M实际文件大小.<br /></span><span style="color: rgb(255,0,0)">struts.multipart.maxSize掌控整个项目所上传文件的最大的Size<br /></span>struts.multipart.maxSize和fileUpload拦截器的maximumSize属性分工不同，总结如下：<br />1.struts.multipart.maxSize掌控整个项目所上传文件的最大的Size。超过了这个size，后台报错，程序处理不了如此大的文件。fielderror里面会有如下的提示：<br />the request was rejected because its size (16272982) exceeds the configured maximum (9000000）<br />2.fileUpload拦截器的maximumSize属性必须小于struts.multipart.maxSize的值。<br />struts.multipart.maxSize默认2M，当maximumSize大于2M时,必须设置struts.multipart.maxSize的值大于maximumSize。<br />3.当上传的文件大于struts.multipart.maxSize时，系统报错<br />当上传的文件在struts.multipart.maxSize和maximumSize之间时，系统提示：<br />File too large: file "MSF的概念.ppt" "upload__5133e516_129ce85285f__7ffa_00000005.tmp" 6007104 <br />当上传的文件小于maximumSize，上传成功。<br /><br />转自：<a href="http://www.cnblogs.com/forlina/archive/2011/9/8.html">http://www.cnblogs.com/forlina/archive/2011/9/8.html</a></p><img src ="http://www.cppblog.com/LIULIANG/aggbug/201564.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2013-07-06 16:07 <a href="http://www.cppblog.com/LIULIANG/archive/2013/07/06/201564.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>mysql中TIMESTAMP设置默认值的灵活运用 </title><link>http://www.cppblog.com/LIULIANG/archive/2013/05/03/199951.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Fri, 03 May 2013 14:20:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2013/05/03/199951.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/199951.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2013/05/03/199951.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/199951.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/199951.html</trackback:ping><description><![CDATA[<a href="http://blog.chinaunix.net/uid-20577907-id-2213871.html">mysql中TIMESTAMP设置默认值的灵活运用</a> <em>2011-08-18 12:12:51</em> 
<div class="Blog_con2">
<div class="Blog_con3">
<p>分类： <span>Mysql/postgreSQL</span></p>
<p>&nbsp;</p></div>
<div class="Blog_wz1">
<div class="post-body">
<p>默认值：</p></div>
<div class="post-body">CURRENT_TIMESTAMP ：当我更新这条记录的时候，这条记录的这个字段<span style="color: rgb(255,0,0)">不会改变</span>。</div>
<p class="post-body">CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ：当我更新这条记录的时候，这条记录的这个字段<span style="color: rgb(255,0,0)">将会改变</span>。即时间变为了更新时候的时间。（注意一个UPDATE设置一个列为它已经有的值，这将不引起TIMESTAMP列被更新，因为如果你设置一个列为它当前的值，MySQL为了效率而忽略更改。）</p>
<p class="post-body">如果有多个TIMESTAMP列，只有第一个自动更新。</p>
<p class="post-body"><br />#1创建一个有两个timestamp字段的表dj1。<br />root@localhost:test &gt;create table dj1 (a char(1), b timestamp ,c timestamp); <br />Query OK, 0 rows affected (0.01 sec)<br /><br />#2插入两行数据，仅赋值于列A<br />root@localhost:test &gt;insert into dj1 values (1,null,null);<br />Query OK, 1 row affected (0.00 sec)<br /><br />root@localhost:test &gt;insert into dj1 values (2,null,null); <br />Query OK, 1 row affected (0.00 sec)<br /><br />#3查询插入数据，b,c两列都使用current timestamp作为默认值。<br />root@localhost:test &gt;select * from dj1;<br />+------+---------------------+---------------------+<br />| a | b | c |<br />+------+---------------------+---------------------+<br />| 1 | 2009-09-09 13:48:40 | 2009-09-09 13:48:40 | <br />| 2 | 2009-09-09 13:48:44 | 2009-09-09 13:48:44 | <br />+------+---------------------+---------------------+<br />2 rows in set (0.00 sec)<br /><br />#4更新一行数据，发现b列timestamp被自动更新，而c列保持不变。<br />root@localhost:test &gt;update dj1 set a=9 where a=1; <br />Query OK, 1 row affected (0.00 sec)<br />Rows matched: 1 Changed: 1 Warnings: 0<br /><br />root@localhost:test &gt;select * from dj1;<br />+------+---------------------+---------------------+<br />| a | b | c |<br />+------+---------------------+---------------------+<br />| 9 | 2009-09-09 13:49:08 | 2009-09-09 13:48:40 | <br />| 2 | 2009-09-09 13:48:44 | 2009-09-09 13:48:44 | <br />+------+---------------------+---------------------+<br />2 rows in set (0.00 sec)<br /><br />#5再更新一列，仍然如#4<br />root@localhost:test &gt;update dj1 set a=8 where a=2; <br />Query OK, 1 row affected (0.00 sec)<br />Rows matched: 1 Changed: 1 Warnings: 0<br /><br />root@localhost:test &gt;select * from dj1;<br />+------+---------------------+---------------------+<br />| a | b | c |<br />+------+---------------------+---------------------+<br />| 9 | 2009-09-09 13:49:08 | 2009-09-09 13:48:40 | <br />| 8 | 2009-09-09 13:49:36 | 2009-09-09 13:48:44 | <br />+------+---------------------+---------------------+<br />2 rows in set (0.00 sec)<br /><br />#6在b列上创建唯一索引<br />root@localhost:test &gt;create unique index dj1_idx_u1 on dj1(b);<br />Query OK, 2 rows affected (0.01 sec)<br />Records: 2 Duplicates: 0 Warnings: 0<br /><br />#7更新所有行a列，报唯一性冲突。<br />root@localhost:test &gt;update dj1 set a=1;<br />ERROR 1062 (23000): Duplicate entry '2009-09-09 13:54:45' for key 'dj1_idx_u1'<br /><br />#8查看表定义，可以看到b列有个属性ON UPDATE CURRENT_TIMESTAMP，导致更新数据时，即便未涉及到该列，该列数据也被自动更新。<br />另一方面，c列默认值是'0000-00-00 00:00:00'，实际插入已经被自动赋值为current_timestamp。<br />root@localhost:test &gt;show create table dj1\G<br />*************************** 1. row ***************************<br />Table: dj1<br />Create Table: CREATE TABLE `dj1` (<br />`a` char(1) COLLATE utf8_bin DEFAULT NULL,<br />`b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,<br />`c` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',<br />UNIQUE KEY `dj1_idx_u1` (`b`)<br />) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin<br />1 row in set (0.00 sec)<br /><br />#9创建表dj2，列b不带自动更新属性。<br />root@localhost:test &gt;CREATE TABLE `dj2` (<br />-&gt; `a` char(1) COLLATE utf8_bin DEFAULT NULL,<br />-&gt; `b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ,<br />-&gt; `c` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',<br />-&gt; UNIQUE KEY `dj1_idx_u1` (`b`)<br />-&gt; ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;<br />Query OK, 0 rows affected (0.01 sec)<br /><br />#10插入dj2测试数据<br />root@localhost:test &gt;insert into dj2 values (1,null,null); <br />Query OK, 1 row affected (0.00 sec)<br /><br />root@localhost:test &gt;insert into dj2 values (2,null,null); <br />Query OK, 1 row affected (0.00 sec)<br /><br />#11查看dj2数据<br />root@localhost:test &gt;select * from dj2;<br />+------+---------------------+---------------------+<br />| a | b | c |<br />+------+---------------------+---------------------+<br />| 1 | 2009-09-09 14:02:55 | 2009-09-09 14:02:55 | <br />| 2 | 2009-09-09 14:03:00 | 2009-09-09 14:03:00 | <br />+------+---------------------+---------------------+<br />2 rows in set (0.00 sec)<br /><br />#12dj2上创建唯一索引<br />root@localhost:test &gt;create unique index dj2_idx_u1 on dj2(b);<br />Query OK, 2 rows affected (0.02 sec)<br />Records: 2 Duplicates: 0 Warnings: 0<br /><br />#更新数据成功<br />root@localhost:test &gt;update dj2 set a=9;<br />Query OK, 2 rows affected (0.00 sec)<br />Rows matched: 2 Changed: 2 Warnings: 0<br /><br />root@localhost:test &gt;select * from dj2;<br />+------+---------------------+---------------------+<br />| a | b | c |<br />+------+---------------------+---------------------+<br />| 9 | 2009-09-09 14:02:55 | 2009-09-09 14:02:55 | <br />| 9 | 2009-09-09 14:03:00 | 2009-09-09 14:03:00 | <br />+------+---------------------+---------------------+<br />2 rows in set (0.00 sec)<br /><br />#13创建表dj3，b列默认值为CURRENT_TIMESTAMP，c列默认值为CURRENT_TIMESTAMP带自动更新属性，报错，不允许行为。<br />root@localhost:test &gt;CREATE TABLE `dj3` (<br />-&gt; `a` char(1) COLLATE utf8_bin DEFAULT NULL,<br />-&gt; `b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ,<br />-&gt; `c` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,<br />-&gt; UNIQUE KEY `dj1_idx_u1` (`b`)<br />-&gt; ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;<br />ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause<br /><br />#14创建表dj4，b列默认值为CURRENT_TIMESTAMP，c列默认值为'0000-00-00 00:00:00'带自动更新属性，报错，不允许行为。<br />root@localhost:test &gt;CREATE TABLE `dj4` (<br />-&gt; `a` char(1) COLLATE utf8_bin DEFAULT NULL,<br />-&gt; `b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ,<br />-&gt; `c` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,<br />-&gt; UNIQUE KEY `dj1_idx_u1` (`b`)<br />-&gt; ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;<br />ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause<br /><br />#15创建表dj5，b列默认值为CURRENT_TIMESTAMP带自动更新属性，c列默认值为CURRENT_TIMESTAMP，报错，不允许行为。<br />root@localhost:test &gt;CREATE TABLE `dj5` (<br />-&gt; `a` char(1) COLLATE utf8_bin DEFAULT NULL,<br />-&gt; `b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,<br />-&gt; `c` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ,<br />-&gt; UNIQUE KEY `dj1_idx_u1` (`b`)<br />-&gt; ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;<br />ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause<br /><br /><font color="#ff0000">conclusion：<br />1.MySQL默认表的第一个timestamp字段为NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP属性，必须显式定义改变这种行为。<br />2.MySQL只允许一个timestamp字段拥有[DEFAULT CURRENT_TIMESTAMP |ON UPDATE CURRENT_TIMESTAMP]属性。 我的理解为要么都是DEFAULT CURRENT_TIMESTAMP 要么都是DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP</font></p>
<p class="post-body"><font color="#ff0000">3.修改字段属性值</font></p>
<div class="post-body"><font color="#ff0000">show create table tbl_ledgerrecord;</font></div>
<div class="post-body"><font color="#ff0000">alter table tbl_ledgerrecord change intoStorageDate&nbsp; intoStorageDate timestamp DEFAULT CURRENT_TIMESTAMP;</font></div></div></div><br /><br />转自:<a href="http://blog.chinaunix.net/uid-20577907-id-2213871.html">http://blog.chinaunix.net/uid-20577907-id-2213871.html</a> <img src ="http://www.cppblog.com/LIULIANG/aggbug/199951.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2013-05-03 22:20 <a href="http://www.cppblog.com/LIULIANG/archive/2013/05/03/199951.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>浅谈Radius协议 </title><link>http://www.cppblog.com/LIULIANG/archive/2013/01/06/197021.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Sun, 06 Jan 2013 08:52:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2013/01/06/197021.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/197021.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2013/01/06/197021.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/197021.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/197021.html</trackback:ping><description><![CDATA[<p style="text-indent: 21pt"><strong><span style="font-family: 黑体; font-size: 12pt">一．RADIUS协议原理</span></strong></p>
<p style="text-indent: 21pt"><strong></strong>&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="font-family: 'Times New Roman'; font-size: 10.5pt">RADIUS</span><span style="font-family: 宋体; font-size: 10.5pt">（</span><span style="font-family: 'Times New Roman'; font-size: 10.5pt">Remote Authentication Dial In User Service</span><span style="font-family: 宋体; font-size: 10.5pt">）</span> <span style="font-family: 宋体; font-size: 10.5pt">用户远程拨入认证服务，<span style="font-family: 宋体">它主要针对的远程登录类型有：</span><span style="font-family: Times New Roman">SLIP</span><span style="font-family: 宋体">、<span style="font-family: Times New Roman">PPP</span></span><span style="font-family: 宋体">、<span style="font-family: Times New Roman">telnet</span></span><span style="font-family: 宋体">和<span style="font-family: Times New Roman">rlogin</span></span><span style="font-family: 宋体">等。</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">协议应用范围很广，包括普通电话、上网业务计费，对</span><span style="font-family: Times New Roman">VPN</span><span style="font-family: 宋体">的支持可以使不同的拨入服务器的用户具有不同权限。</span></span></p>
<p>&nbsp;</p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">典型应用环境如下：</span></p>
<p style="text-indent: 21pt">&nbsp;</p>
<p style="text-indent: 21pt"><span style="font-family: 宋体"><img alt="radius典型应用" src="http://p.blog.csdn.net/images/p_blog_csdn_net/cjx1986/EntryImages/20090831/clip_image002.jpg" width="465" height="230" /></span></p>
<p style="text-indent: 21pt">&nbsp;</p>
<p style="text-indent: 21pt">&nbsp;</p>
<p>
<p style="text-indent: 21pt">&nbsp;</p>
<p style="text-indent: 21pt">&nbsp;</p>
<p style="text-indent: 20.6pt"><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">数据包分为</span><span style="font-family: DY168+ZEFA21-171"><span style="font-family: Times New Roman">5</span></span><span style="font-family: 宋体">个部分：</span></p>
<p style="text-align: left; text-indent: -36pt; margin: 0cm 0cm 0pt 57pt" align="left"><span style="font-family: Times New Roman"><span style="font-family: DY168+ZEFA21-171">（1）<span style="font: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-family: DY168+ZEFA21-171">Code:1</span></span><span style="font-family: 宋体">个字节，用于区分</span><span style="font-family: DY168+ZEFA21-171"><span style="font-family: Times New Roman">RADIUS</span></span><span style="font-family: 宋体">包的类型：常用类型有：</span></p>
<p align="left"><span style="font-family: 宋体">接入请求（</span><span style="font-family: DY168+ZEFA21-171"><span style="font-family: Times New Roman">Access-Request</span></span><span style="font-family: 宋体">），</span><span style="font-family: DY168+ZEFA21-171"><span style="font-family: Times New Roman">Code=1</span></span><span style="font-family: 宋体">；接入允许（</span><span style="font-family: DY168+ZEFA21-171"><span style="font-family: Times New Roman">Access-Accept</span></span><span style="font-family: 宋体">），</span><span style="font-family: DY168+ZEFA21-171"><span style="font-family: Times New Roman">Code=2</span></span><span style="font-family: 宋体">；接入拒绝（</span><span style="font-family: DY168+ZEFA21-171"><span style="font-family: Times New Roman">Access-Reject</span></span><span style="font-family: 宋体">），</span><span style="font-family: DY168+ZEFA21-171"><span style="font-family: Times New Roman">Code=3</span></span><span style="font-family: 宋体">；计费请求（</span><span style="font-family: DY168+ZEFA21-171"><span style="font-family: Times New Roman">Accounting-Request</span></span><span style="font-family: 宋体">），</span><span style="font-family: DY168+ZEFA21-171"><span style="font-family: Times New Roman">Code=4</span></span><span style="font-family: 宋体">等。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">（</span><span style="font-family: Times New Roman">2</span><span style="font-family: 宋体">）</span><span style="font-family: Times New Roman">Identifier:</span><span style="font-family: 宋体">一个字节，用于请求和应答包的匹配。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">（</span><span style="font-family: Times New Roman">3</span><span style="font-family: 宋体">）</span><span style="font-family: Times New Roman">Length:</span><span style="font-family: 宋体">两个字节，表示</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">数据区（包括</span><span style="font-family: Times New Roman">Code, Identifier, Length, Authenticator, Attributes</span><span style="font-family: 宋体">）的长度，单位是字节，最小为</span><span style="font-family: Times New Roman">20</span><span style="font-family: 宋体">，最大为</span><span style="font-family: Times New Roman">4096</span><span style="font-family: 宋体">。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">（</span><span style="font-family: Times New Roman">4</span><span style="font-family: 宋体">）</span><span style="font-family: Times New Roman">Authenticator:16</span><span style="font-family: 宋体">个字节，用于验证服务器端的应答，另外还用于用户口令的加密。</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">服务器和</span><span style="font-family: Times New Roman">NAS</span><span style="font-family: 宋体">的共享密钥(</span><span style="font-family: Times New Roman">Shared Secret)</span><span style="font-family: 宋体">与请求认证码(</span><span style="font-family: Times New Roman">Request Authenticator)</span><span style="font-family: 宋体">和应答认证码</span><span style="font-family: Times New Roman">(Response Authenticator)</span><span style="font-family: 宋体">，共同支持发、收报文的完整性和认证。另外，用户密码不能在</span><span style="font-family: Times New Roman">NAS</span><span style="font-family: 宋体">和</span><span style="font-family: Times New Roman">RADIUS </span><span style="font-family: 宋体">服务器之间用明文传输，而一般使用共享密钥(</span><span style="font-family: Times New Roman">Shared Secret)</span><span style="font-family: 宋体">和认证码(</span><span style="font-family: Times New Roman">Authenticator)</span><span style="font-family: 宋体">通过</span><span style="font-family: Times New Roman">MD5</span><span style="font-family: 宋体">加密算法进行加密隐藏。</span></p>
<p style="text-indent: 20.6pt"><span style="font-family: 宋体">（</span><span style="font-family: Times New Roman">5</span><span style="font-family: 宋体">）</span><span style="font-family: Times New Roman">Attributes:</span><span style="font-family: 宋体">不定长度，最小可为</span><span style="font-family: Times New Roman">0</span><span style="font-family: 宋体">个字节，描述</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">协议的属性，如用户名、口令、</span><span style="font-family: Times New Roman">IP</span><span style="font-family: 宋体">地址等信息都是存放在本数据段。</span></p>
<p style="text-indent: 20.6pt"><span style="font-family: 宋体">各个属性的详细编码信息，以及数据格式，限于篇幅，这里不作具体介绍，感兴趣的，可以参看</span><span style="font-family: Times New Roman">RFC</span><span style="font-family: 宋体">文档，或与我交流。</span></p>
<p style="text-indent: 20.6pt">&nbsp;</p>
<p style="text-indent: 20.6pt"><strong><span style="font-family: 黑体; font-size: 12pt">二．RADIUS协议实现</span></strong></p>
<p style="text-indent: 20.6pt"><strong></strong>&nbsp;</p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">目前，开源软件包</span><span style="font-family: Times New Roman">freeRadius , tinyRadius</span><span style="font-family: 宋体">，可以下载到其实现的源码。</span><span style="font-family: Times New Roman">freeRadius</span><span style="font-family: 宋体">是目前功能最强大的开源</span><span style="font-family: Times New Roman"> RADIUS </span><span style="font-family: 宋体">服务器软件，采用</span><span style="font-family: Times New Roman">C</span><span style="font-family: 宋体">语言实现，采用了多进程，进程池的处理方法，拥有很好的吞吐处理能力，同时，提供了连接各种数据库的应用接口，方便用户根据自己的需要进行适当的扩展。值得一提的是，它采用模块化处理，用户可以定做适合自己的认证计费处理模块。</span></p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman">freeRadius</span><span style="font-family: 宋体">的功能强大，也造成了它模块的庞大，不易维护，对安装环境有一些要求。</span><span style="font-family: Times New Roman">tinyRadius</span><span style="font-family: 宋体">采用</span><span style="font-family: Times New Roman">Java</span><span style="font-family: 宋体">开发，短小精悍，能接收各种标准协议中的数据包，可以快速的完成对</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">数据包的封装与解包，我们可以自己的需要进行某些处理，具有很大的自由度，唯一的缺陷是单线程，没有数据库接口。</span></p>
<p style="text-indent: 21pt">&nbsp;</p>
<p style="text-indent: 21pt"><strong><span style="font-family: 黑体; font-size: 12pt">三．RADIUS协议对安全的考虑</span></strong></p>
<p style="text-indent: 21pt"><strong></strong>&nbsp;</p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">采用</span><span style="font-family: Times New Roman">UDP</span><span style="font-family: 宋体">协议基于以下几点原因：</span></p>
<p style="text-indent: -18pt; margin: 0cm 0cm 0pt 39pt"><span style="font-family: Times New Roman"><span>1．&nbsp;</span>NAS</span><span style="font-family: 宋体">和</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">服务器大多在同一个局域网中，使用</span><span style="font-family: Times New Roman">UDP</span><span style="font-family: 宋体">更加快捷方便。</span></p>
<p style="text-indent: -18pt; margin: 0cm 0cm 0pt 39pt"><span style="font-family: Times New Roman">2．&nbsp;</span><span style="font-family: 宋体">简化了服务端的实现。</span></p>
<p style="margin: 0cm 0cm 0pt 21pt"><span style="font-family: 宋体">事实证明，采用</span><span style="font-family: Times New Roman">UDP</span><span style="font-family: 宋体">协议可行，</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">有自己的机制，来解决</span><span style="font-family: Times New Roman">UDP</span><span style="font-family: 宋体">丢包特点。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">如果</span><span style="font-family: Times New Roman">NAS</span><span style="font-family: 宋体">向某个</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">服务器提交请求没有收到返回信息，那么可以要求备份</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">服务器重传。由于有多个备份</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">服务器，因此</span><span style="font-family: Times New Roman">NAS</span><span style="font-family: 宋体">进行重传的时候，可以采用轮询的方法。如果备份</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">服务器的密钥和以前</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">服务器的密钥不同，则需要重新进行认证。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">下面重点从</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">协议来谈下它在身份认证中如何确保安全认证的。</span></p>
<p style="text-indent: -18pt; margin: 0cm 0cm 0pt 39pt"><span style="font-family: Times New Roman"><span>1．&nbsp;</span><span>Authenticator</span></span><span style="font-family: 宋体">：鉴别码，分为请求鉴别码，回应鉴别码。</span><span style="font-family: Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">在<span style="font-family: Times New Roman">&#8220;Access-Request&#8221;</span></span><span style="font-family: 宋体">数据包中，<span style="font-family: Times New Roman">Authenticator</span></span><span style="font-family: 宋体">是一个<span style="font-family: Times New Roman">16</span></span><span style="font-family: 宋体">字节的随机数，称为<span style="font-family: Times New Roman">&#8220;Request Authenticator&#8221;</span></span><span style="font-family: 宋体">。</span> <span style="font-family: 宋体">在机密的整个生存周期中（如</span><span style="font-family: Times New Roman">RADIUAS</span><span style="font-family: 宋体">服务器和客户端共享的机密），这个值应该是不可预测的，并且是唯一的，因为具有相同密码的重复请求值，使黑客有机会用已截取的响应回复用户</span><span style="font-family: 宋体">。</span><span style="font-family: 宋体">因为同一机密可以被用在不同地理区域中的服务器的验证中，所以请求认证域应该具有全球和临时唯一性。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">为防止数据包中数据被截获被篡改，回应鉴别码采用如下方式生成：</span></p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman">ResponseAuth = MD5(Code+ID+Length+RequestAuth+ Attributes+Secret)</span><span style="font-family: 宋体">；</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">回应鉴别码是对整个数据包进行</span><span style="font-family: Times New Roman">MD5</span><span style="font-family: 宋体">演算产生的</span><span style="font-family: Times New Roman">16</span><span style="font-family: 宋体">字节索引，防止伪造服务器的回应。</span></p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman">2</span><span style="font-family: 宋体">．加密方式。</span><span style="font-family: Times New Roman">PAP</span><span style="font-family: 宋体">，</span><span style="font-family: Times New Roman">CAHP</span><span style="font-family: 宋体">，</span><span style="font-family: Times New Roman">EAP</span><span style="font-family: 宋体">以及</span><span style="font-family: Times New Roman">Unix</span><span style="font-family: 宋体">登录认证三种加密认证方式。最常用的是前两种，下面介绍下：</span></p>
<p style="text-indent: 21pt"><strong><span style="font-family: Times New Roman">PAP</span></strong><strong><span style="font-family: 宋体">加密</span></strong><span style="font-family: 宋体">，采用此加密方法时，密码存放在</span><span style="font-family: Times New Roman">User-Password</span><span style="font-family: 宋体">属性中。</span></p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman">User-Password</span><span style="font-family: 宋体">加密方法：</span></p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman">1.</span><span style="font-family: 宋体">在密码的末尾用</span><span style="font-family: Times New Roman">nulls</span><span style="font-family: 宋体">代替填补形成多个十六个字节的二进制数</span><span style="font-family: Times New Roman">;</span></p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman">2.</span><span style="font-family: 宋体">把密码按</span><span style="font-family: Times New Roman">16</span><span style="font-family: 宋体">个字节为一组划分为</span><span style="font-family: Times New Roman">p1</span><span style="font-family: 宋体">、</span><span style="font-family: Times New Roman">p2</span><span style="font-family: 宋体">等等</span><span style="font-family: Times New Roman">; </span></p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman">&nbsp;b1=MD5(Secret + Authenticator)&nbsp;c(1) = p1 </span><span style="font-family: 宋体">异或</span><span style="font-family: Times New Roman"> b1</span></p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman">&nbsp;b2 = MD5(S + c(1))<span>&nbsp;&nbsp;&nbsp;&nbsp; c(2) = p2 </span></span><span style="font-family: 宋体">异或</span><span style="font-family: Times New Roman">b2 </span></p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .</span></span></p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .</span></span></p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .</span></span></p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman">&nbsp;bi = MD5(S + c(i-1))<span>&nbsp;&nbsp; c (i) = pi </span></span><span style="font-family: 宋体">异或</span><span style="font-family: Times New Roman"> bi</span></p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman">c(1)+c(2)+...+c (i) </span></p>
<p style="text-indent: 21pt"><span>&nbsp;</span><span style="font-family: 宋体">在接收时，这个过程被反过来，由于采用异或方式贯穿在每</span><span style="font-family: Times New Roman">16</span><span style="font-family: 宋体">个字节之间，同样的算法再异或一次，然后配合</span><span style="font-family: Times New Roman">MD5</span><span style="font-family: 宋体">演算，从而生成原始的密码，尽管这种加密方式是可逆的，黑客截获到密文后，能通过一定的手段来破解出密码，但如果共享密钥未知的情况下，很难破解，也就只能采用蛮力破解方法。使用共享密钥应采用合适的长度，来防止破解，不应过短。</span></p>
<p style="text-indent: 21pt">&nbsp;</p>
<p style="text-align: left; text-indent: 21pt" align="left"><span style="font-family: Times New Roman">PAP</span><span style="font-family: 宋体">加密方法使密码以密文的方式在网络中进行传输，使黑客仍有有机可乘的机会，但</span><span style="font-family: Times New Roman">CHAP</span><span style="font-family: 宋体">加密方法阻止了密码的传输。</span></p>
<p style="text-indent: 21pt"><strong><span style="font-family: Times New Roman">CHAP</span></strong><strong><span style="font-family: 宋体">加密</span></strong><span style="font-family: 宋体">，采用此加密方法时，密码存放在</span><span style="font-family: Times New Roman">Chap-Password</span><span style="font-family: 宋体">属性中。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">这种加密方法的原则是不是密码在网络中进行传输，而只是传输一个索引值，从而增加了安全性，但这样做的代价是，</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">服务端必须要知道用户的密码，从而再现密码索引值来和发来的认证请求中的密码索引值比对。</span></p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman">Chap-Password</span><span style="font-family: 宋体">加密方法：</span></p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman">Md5(chapId+password+chapChallenge);</span></p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman">chapId :</span><span style="font-family: 宋体">可以是随机产生的一字节码；</span></p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman">chapChallenge&nbsp;:NAS</span><span style="font-family: 宋体">生成一个随机挑战字（</span><span style="font-family: Times New Roman">16</span><span style="font-family: 宋体">个字节比较合适）</span></p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;</span></span><span style="font-family: 宋体">该字段有时缺失，读取</span><span style="font-family: Times New Roman">Authenticator</span><span style="font-family: 宋体">域作为挑战字。</span></p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman">3</span><span style="font-family: 宋体">．共享密钥（</span><span style="font-family: Times New Roman">Secret</span><span style="font-family: 宋体">）：共享密钥在密码加密以及数据包的鉴别码部分都参与了运算，即使</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">数据包被截获，但不知道共享密钥，很难破解用户的密码伪造数据。共享密钥应采用合适的长度，不应过短，</span><span style="font-family: 宋体">宽大密码范围能有效提供对穷举搜寻攻击的防卫，</span><span style="font-family: 宋体">随着密钥长度的增加，其破解花费的时间将大大增加。</span></p>
<p style="text-indent: 21pt">4．为防止非法用户的重放攻击，造成服务器瘫痪。如果在一个很短的时间片段里，一个请求有相同的客户源IP地址、源UDP端口号和标识符，RADIUS服务器会认为这是上一个重复的请求，将直接丢弃，不做任何处理。</p>
<p style="text-indent: 21pt">&nbsp;</p>
<p style="text-indent: 21pt">&nbsp;</p>
<p style="text-indent: 21pt"><strong><span style="font-family: 黑体; font-size: 12pt">四．总结</span></strong></p>
<p style="text-indent: 21pt"><strong></strong>&nbsp;</p>
<p style="text-indent: 21pt">Radius协议本身比较易掌握，在应用中，可以结合其自身的密码方式，实现满足企业需要的更高强度的强认证，比如结合各种令牌卡，手机短信等等。对协议进行开发，离不开各种抓包工具，像sniffer,tcpdump,ethereal等抓包工具的使用。</p>
<p>&nbsp;</p>
<p style="text-indent: 20.6pt"><span style="font-family: 宋体">外部用户要访问某局域网络中计算机设备，其访问方式有多种，比如采用</span><span style="font-family: Times New Roman">VPN</span><span style="font-family: 宋体">拨号，</span><span style="font-family: Times New Roman">Telnet</span><span style="font-family: 宋体">等等。如用户</span><span style="font-family: Times New Roman">Telnet</span><span style="font-family: 宋体">登陆时，产生用户名和密码信息，而</span><span style="font-family: Times New Roman">NAS</span><span style="font-family: 宋体">服务器</span><span style="font-family: Times New Roman">AAA</span><span style="font-family: 宋体">配置中指定了采用</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">作为认证服务器，则</span><span style="font-family: 宋体">将其封装成</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">请求数据包发送到</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">服务器进行身份认证，</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">服务器通过</span><span style="font-family: Times New Roman">NAS</span><span style="font-family: 宋体">和用户进行交流，以提示用户认证通过与否，以及是否需要</span><span style="font-family: Times New Roman">Challenge</span><span style="font-family: 宋体">身份认证。</span></p>
<p style="text-indent: 20.6pt">&nbsp;</p>
<p style="text-indent: 20.6pt"><span style="font-family: 宋体">其工作原理为：用户接入</span><span style="font-family: Times New Roman">NAS </span><span style="font-family: 宋体">（</span><span style="font-family: Times New Roman">Net Access Server</span><span style="font-family: 宋体">），</span><span style="font-family: Times New Roman">NAS</span><span style="font-family: 宋体">一般为路由器等设备，</span><span style="font-family: Times New Roman">NAS</span><span style="font-family: 宋体">向</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">服务器使用</span><span style="font-family: Times New Roman">Access-Request</span><span style="font-family: 宋体">数据包提交用户信息，包括用户名、密码等相关信息，其中用户密码是经过</span><span style="font-family: Times New Roman">MD5</span><span style="font-family: 宋体">加密的，双方使用共享密钥，这个密钥不经过网络传播；</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">服务器对用户名和密码的合法性进行检验，必要时可以提出一个</span><span style="font-family: Times New Roman">Challenge</span><span style="font-family: 宋体">，要求进一步对用户认证，也可以对</span><span style="font-family: Times New Roman">NAS</span><span style="font-family: 宋体">进行类似的认证；如果合法，给</span><span style="font-family: Times New Roman">NAS</span><span style="font-family: 宋体">返回</span><span style="font-family: Times New Roman">Access-Accept</span><span style="font-family: 宋体">数据包，允许用户进行下一步工作，否则返回</span><span style="font-family: Times New Roman">Access-Reject</span><span style="font-family: 宋体">数据包，拒绝用户访问；如果允许访问，</span><span style="font-family: Times New Roman">NAS</span><span style="font-family: 宋体">向</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">服务器提出计费请求</span><span style="font-family: Times New Roman">Account-Require</span><span style="font-family: 宋体">，</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">服务器响应</span><span style="font-family: Times New Roman">Account-Accept</span><span style="font-family: 宋体">，对用户的计费开始，同时用户可以进行自己的相关操作。</span></p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">还支持代理和漫游功能。简单地说，代理就是一台服务器，可以作为其他</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">服务器的代理，负责转发</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">认证和计费数据包。所谓漫游功能，就是代理的一个具体实现，这样可以让用户通过本来和其无关的</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">服务器进行认证。</span></p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">能够实现其功能依赖于它自身的数据包结构。</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">采用的是</span><span style="font-family: Times New Roman">UDP</span><span style="font-family: 宋体">传输协议，认证和计费监听端口一般分别为：</span><span style="font-family: Times New Roman">1812</span><span style="font-family: 宋体">，</span><span style="font-family: Times New Roman">1813</span><span style="font-family: 宋体">。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">以太网上的</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">封装后的包结构如下：</span></p>
<p>&nbsp;</p>
<p style="text-indent: 21pt"></p>
<table style="border-bottom: medium none; border-left: medium none; margin: auto auto auto 23.4pt; border-collapse: collapse; border-top: medium none; border-right: medium none" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; padding-bottom: 0cm; background-color: transparent; padding-left: 5.4pt; width: 81pt; padding-right: 5.4pt; border-top: windowtext 1pt solid; border-right: windowtext 1pt solid; padding-top: 0cm" valign="top" width="108">
<p style="text-indent: 10.5pt"><span style="font-size: small"><span style="font-family: 宋体">以太帧头</span></span></p></td>
<td style="border-bottom: windowtext 1pt solid; border-left: #ece9d8; padding-bottom: 0cm; background-color: transparent; padding-left: 5.4pt; width: 63pt; padding-right: 5.4pt; border-top: windowtext 1pt solid; border-right: windowtext 1pt solid; padding-top: 0cm" valign="top" width="84">
<p style="text-indent: 10.5pt"><span style="font-size: small"><span style="font-family: Times New Roman">IP</span><span style="font-family: 宋体">包头</span></span></p></td>
<td style="border-bottom: windowtext 1pt solid; border-left: #ece9d8; padding-bottom: 0cm; background-color: transparent; padding-left: 5.4pt; width: 72pt; padding-right: 5.4pt; border-top: windowtext 1pt solid; border-right: windowtext 1pt solid; padding-top: 0cm" valign="top" width="96">
<p><span style="font-size: small"><span style="font-family: Times New Roman">UDP</span><span style="font-family: 宋体">包头</span></span></p></td>
<td style="border-bottom: windowtext 1pt solid; border-left: #ece9d8; padding-bottom: 0cm; background-color: transparent; padding-left: 5.4pt; width: 90pt; padding-right: 5.4pt; border-top: windowtext 1pt solid; border-right: windowtext 1pt solid; padding-top: 0cm" valign="top" width="120">
<p><span style="font-size: small"><span style="color: red"><span style="font-family: Times New Roman">RADIUS</span></span><span style="font-family: 宋体; color: red">数据包</span></span></p></td>
<td style="border-bottom: windowtext 1pt solid; border-left: #ece9d8; padding-bottom: 0cm; background-color: transparent; padding-left: 5.4pt; width: 72pt; padding-right: 5.4pt; border-top: windowtext 1pt solid; border-right: windowtext 1pt solid; padding-top: 0cm" valign="top" width="96">
<p><span style="font-size: small"><span style="font-family: 宋体">以太网</span><span style="font-family: Times New Roman">FCS</span></span></p></td></tr></tbody></table>
<p>&nbsp;</p>
<p style="text-indent: 21pt">&nbsp;</p>
<p style="text-indent: 21pt">&nbsp;</p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">在这里我们关心的是红色</span><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">数据包部分。</span></p>
<p style="text-indent: 21pt"><span style="font-family: Times New Roman">RADIUS</span><span style="font-family: 宋体">数据包的格式如下：</span></p>
<p style="text-indent: 21pt">&nbsp;</p>
<p style="text-indent: 21pt"><img alt="radius数据包格式" src="http://p.blog.csdn.net/images/p_blog_csdn_net/cjx1986/EntryImages/20090831/2.gif" width="512" height="172" /><br /><br /><br />转自：<a href="http://blog.csdn.net/cjx1986/article/details/4503524#comments">http://blog.csdn.net/cjx1986/article/details/4503524#comments</a></p><img src ="http://www.cppblog.com/LIULIANG/aggbug/197021.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2013-01-06 16:52 <a href="http://www.cppblog.com/LIULIANG/archive/2013/01/06/197021.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux查看linux版本，内核版本，系统位数，gcc版本</title><link>http://www.cppblog.com/LIULIANG/archive/2012/11/23/195590.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Fri, 23 Nov 2012 03:05:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2012/11/23/195590.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/195590.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2012/11/23/195590.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/195590.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/195590.html</trackback:ping><description><![CDATA[<div><h2>linux查看linux版本，内核版本，系统位数，gcc版本</h2>     <p>1. 查看linux版本</p><p>&nbsp; sunny@ubuntu:~$cat /etc/issue<br />Ubuntu 11.04 \n \l</p><p>&nbsp;</p><p>2. 查看内核版本</p><p>1) sunny@ubuntu:~$ cat /proc/version<br />Linux  version 2.6.38-13-generic (buildd@rothera) (gcc version 4.5.2  (Ubuntu/Linaro 4.5.2-8ubuntu4) ) #57-Ubuntu SMP Mon Mar 5 18:10:14 UTC  2012</p><p>2)&nbsp; uname命令</p><p>sunny@ubuntu:~$ uname -a<br />Linux ubuntu 2.6.38-13-generic #57-Ubuntu SMP Mon Mar 5 18:10:14 UTC 2012 i686 i686 i386 GNU/Linux</p><p>&nbsp;</p><p>[ -------------------------------------------------------------------------------<br />&nbsp; -a, --all&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print all information, in the following order,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 查看全部信息<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except omit -p and -i if unknown:<br />&nbsp; -s, --kernel-name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print the kernel name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 查看内核名字<br />&nbsp; -n, --nodename&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print the network node hostname<br />&nbsp; -r, --kernel-release&nbsp;&nbsp;&nbsp;&nbsp; print the kernel release&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 查看内核发行版本<br />&nbsp; -v, --kernel-version&nbsp;&nbsp;&nbsp;&nbsp; print the kernel version&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 查看内核版本<br />&nbsp; -m, --machine&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print the machine hardware name<br />&nbsp; -p, --processor&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print the processor type or "unknown"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp; -i, --hardware-platform&nbsp; print the hardware platform or "unknown"<br />&nbsp; -o, --operating-system&nbsp;&nbsp; print the operating system&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 查看操作系统<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --help&nbsp;&nbsp;&nbsp;&nbsp; display this help and exit<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --version&nbsp; output version information and exit</p>--------------------------------------------------------------------------------------]<p>&nbsp;</p>3.查看系统位数<p>1) sunny@ubuntu:~$ getconf WORD_BIT<br />32</p><p>2)sunny@ubuntu:~$ file /bin/bash<br />/bin/bash:  ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically  linked (uses shared libs), for GNU/Linux 2.6.15, stripped</p><p>&nbsp;</p><p>4. 查看gcc版本</p>sunny@ubuntu:~$ gcc --version<br />gcc (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2</div><br />转自：<div>http://hi.baidu.com/sunnnnnnnnny/item/b20fb816164a445ef0090ebd</div><img src ="http://www.cppblog.com/LIULIANG/aggbug/195590.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2012-11-23 11:05 <a href="http://www.cppblog.com/LIULIANG/archive/2012/11/23/195590.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Ubuntu    /etc/network/interface </title><link>http://www.cppblog.com/LIULIANG/archive/2012/11/22/195569.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Thu, 22 Nov 2012 10:58:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2012/11/22/195569.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/195569.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2012/11/22/195569.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/195569.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/195569.html</trackback:ping><description><![CDATA[<div>######################################################################<br /># /etc/network/interfaces -- configuration file for ifup(8), ifdown(8)<br />#<br /># A "#" character in the very first column makes the rest of the line<br /># be ignored. Blank lines are ignored. Lines may be indented freely.<br /># A "\" character at the very end of the line indicates the next line<br /># should be treated as a continuation of the current one.<br />#<br /># The "pre-up", "up", "down" and "post-down" options are valid for all<br /># interfaces, and may be specified multiple times. All other options<br /># may only be specified once.<br />#<br /># See the interfaces(5) manpage for information on what options are<br /># available.<br />######################################################################<br /><br /># We always want the loopback interface.<br />#<br /># auto lo<br /># iface lo inet loopback<br /><br /># An example ethernet card setup: (broadcast and gateway are optional)<br />#<br /># auto eth0<br /># iface eth0 inet static<br />#&nbsp;&nbsp;&nbsp;&nbsp; address 192.168.0.42<br />#&nbsp;&nbsp;&nbsp;&nbsp; network 192.168.0.0<br />#&nbsp;&nbsp;&nbsp;&nbsp; netmask 255.255.255.0<br />#&nbsp;&nbsp;&nbsp;&nbsp; broadcast 192.168.0.255<br />#&nbsp;&nbsp;&nbsp;&nbsp; gateway 192.168.0.1<br /><br /># A more complicated ethernet setup, with a less common netmask, and a downright<br /># weird broadcast address: (the "up" lines are executed verbatim when the<br /># interface is brought up, the "down" lines when it's brought down)<br />#<br /># auto eth0<br /># iface eth0 inet static<br />#&nbsp;&nbsp;&nbsp;&nbsp; address 192.168.1.42<br />#&nbsp;&nbsp;&nbsp;&nbsp; network 192.168.1.0<br />#&nbsp;&nbsp;&nbsp;&nbsp; netmask 255.255.255.128<br />#&nbsp;&nbsp;&nbsp;&nbsp; broadcast 192.168.1.0<br />#&nbsp;&nbsp;&nbsp;&nbsp; up route add -net 192.168.1.128 netmask 255.255.255.128 gw 192.168.1.2<br />#&nbsp;&nbsp;&nbsp;&nbsp; up route add default gw 192.168.1.200<br />#&nbsp;&nbsp;&nbsp;&nbsp; down route del default gw 192.168.1.200<br />#&nbsp;&nbsp;&nbsp;&nbsp; down route del -net 192.168.1.128 netmask 255.255.255.128 gw 192.168.1.2<br /><br /># A more complicated ethernet setup with a single ethernet card with<br /># two interfaces.<br /># Note: This happens to work since ifconfig handles it that way, not because<br /># ifup/down handles the ':' any differently.<br /># Warning: There is a known bug if you do this, since the state will not<br /># be properly defined if you try to 'ifdown eth0' when both interfaces<br /># are up. The ifconfig program will not remove eth0 but it will be<br /># removed from the interfaces state so you will see it up until you execute:<br /># 'ifdown eth0:1 ; ifup eth0; ifdown eth0'<br /># BTW, this is "bug" #193679 (it's not really a bug, it's more of a<br /># limitation)<br />#<br /># auto eth0 eth0:1<br /># iface eth0 inet static<br />#&nbsp;&nbsp;&nbsp;&nbsp; address 192.168.0.100<br />#&nbsp;&nbsp;&nbsp;&nbsp; network 192.168.0.0<br />#&nbsp;&nbsp;&nbsp;&nbsp; netmask 255.255.255.0<br />#&nbsp;&nbsp;&nbsp;&nbsp; broadcast 192.168.0.255<br />#&nbsp;&nbsp;&nbsp;&nbsp; gateway 192.168.0.1<br /># iface eth0:1 inet static<br />#&nbsp;&nbsp;&nbsp;&nbsp; address 192.168.0.200<br />#&nbsp;&nbsp;&nbsp;&nbsp; network 192.168.0.0<br />#&nbsp;&nbsp;&nbsp;&nbsp; netmask 255.255.255.0<br /><br /># "pre-up" and "post-down" commands are also available. In addition, the<br /># exit status of these commands are checked, and if any fail, configuration<br /># (or deconfiguration) is aborted. So:<br />#<br /># auto eth0<br /># iface eth0 inet dhcp<br />#&nbsp;&nbsp;&nbsp;&nbsp; pre-up [ -f /etc/network/local-network-ok ]<br />#<br /># will allow you to only have eth0 brought up when the file<br /># /etc/network/local-network-ok exists.<br /><br /># Two ethernet interfaces, one connected to a trusted LAN, the other to<br /># the untrusted Internet. If their MAC addresses get swapped (because an<br /># updated kernel uses a different order when probing for network cards,<br /># say), then they don't get brought up at all.<br />#<br /># auto eth0 eth1<br /># iface eth0 inet static<br />#&nbsp;&nbsp;&nbsp;&nbsp; address 192.168.42.1<br />#&nbsp;&nbsp;&nbsp;&nbsp; netmask 255.255.255.0<br />#&nbsp;&nbsp;&nbsp;&nbsp; pre-up /path/to/check-mac-address.sh eth0 11:22:33:44:55:66<br />#&nbsp;&nbsp;&nbsp;&nbsp; pre-up /usr/local/sbin/enable-masq<br /># iface eth1 inet dhcp<br />#&nbsp;&nbsp;&nbsp;&nbsp; pre-up /path/to/check-mac-address.sh eth1 AA:BB:CC:DD:EE:FF<br />#&nbsp;&nbsp;&nbsp;&nbsp; pre-up /usr/local/sbin/firewall<br /><br /># Two ethernet interfaces, one connected to a trusted LAN, the other to<br /># the untrusted Internet, identified by MAC address rather than interface<br /># name:<br />#<br /># auto eth0 eth1<br /># mapping eth0 eth1<br />#&nbsp;&nbsp;&nbsp;&nbsp; script /path/to/get-mac-address.sh<br />#&nbsp;&nbsp;&nbsp;&nbsp; map 11:22:33:44:55:66 lan<br />#&nbsp;&nbsp;&nbsp;&nbsp; map AA:BB:CC:DD:EE:FF internet<br /># iface lan inet static<br />#&nbsp;&nbsp;&nbsp;&nbsp; address 192.168.42.1<br />#&nbsp;&nbsp;&nbsp;&nbsp; netmask 255.255.255.0<br />#&nbsp;&nbsp;&nbsp;&nbsp; pre-up /usr/local/sbin/enable-masq $IFACE<br /># iface internet inet dhcp<br />#&nbsp;&nbsp;&nbsp;&nbsp; pre-up /usr/local/sbin/firewall $IFACE<br /><br /># A PCMCIA interface for a laptop that is used in different locations:<br /># (note the lack of an "auto" line for any of these)<br />#<br /># mapping eth0<br />#&nbsp;&nbsp;&nbsp; script /path/to/pcmcia-compat.sh<br />#&nbsp;&nbsp;&nbsp; map home,*,*,*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; home<br />#&nbsp;&nbsp;&nbsp; map work,*,*,00:11:22:33:44:55&nbsp; work-wireless<br />#&nbsp;&nbsp;&nbsp; map work,*,*,01:12:23:34:45:50&nbsp; work-static<br />#<br /># iface home inet dhcp<br /># iface work-wireless bootp<br /># iface work-static static<br />#&nbsp;&nbsp;&nbsp;&nbsp; address 10.15.43.23<br />#&nbsp;&nbsp;&nbsp;&nbsp; netmask 255.255.255.0<br />#&nbsp;&nbsp;&nbsp;&nbsp; gateway 10.15.43.1<br />#<br /># Note, this won't work unless you specifically change the file<br /># /etc/pcmcia/network to look more like:<br />#<br />#&nbsp;&nbsp;&nbsp;&nbsp; if [ -r ./shared ] ; then . ./shared ; else . /etc/pcmcia/shared ; fi<br />#&nbsp;&nbsp;&nbsp;&nbsp; get_info $DEVICE<br />#&nbsp;&nbsp;&nbsp;&nbsp; case "$ACTION" in<br />#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'start')<br />#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /sbin/ifup $DEVICE<br />#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;;<br />#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'stop')<br />#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /sbin/ifdown $DEVICE<br />#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;;<br />#&nbsp;&nbsp;&nbsp;&nbsp; esac<br />#&nbsp;&nbsp;&nbsp;&nbsp; exit 0<br /><br /># An alternate way of doing the same thing: (in this case identifying<br /># where the laptop is is done by configuring the interface as various<br /># options, and seeing if a computer that is known to be on each particular<br /># network will respond to pings. The various numbers here need to be chosen<br /># with a great deal of care.)<br />#<br /># (note the lack of an "auto" line for any of these)<br />#<br /># mapping eth0<br />#&nbsp;&nbsp;&nbsp; script /path/to/pcmcia-compat.sh<br />#&nbsp;&nbsp;&nbsp; map home,*,*,*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; home<br />#&nbsp;&nbsp;&nbsp; map work,*,*,00:11:22:33:44:55&nbsp; work-wireless<br />#&nbsp;&nbsp;&nbsp; map work,*,*,01:12:23:34:45:50&nbsp; work-static<br />#<br /># iface home inet dhcp<br /># iface work-wireless bootp<br /># iface work-static static<br />#&nbsp;&nbsp;&nbsp;&nbsp; address 10.15.43.23<br />#&nbsp;&nbsp;&nbsp;&nbsp; netmask 255.255.255.0<br />#&nbsp;&nbsp;&nbsp;&nbsp; gateway 10.15.43.1<br />#<br /># Note, this won't work unless you specifically change the file<br /># /etc/pcmcia/network to look more like:<br />#<br />#&nbsp;&nbsp;&nbsp;&nbsp; if [ -r ./shared ] ; then . ./shared ; else . /etc/pcmcia/shared ; fi<br />#&nbsp;&nbsp;&nbsp;&nbsp; get_info $DEVICE<br />#&nbsp;&nbsp;&nbsp;&nbsp; case "$ACTION" in<br />#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'start')<br />#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /sbin/ifup $DEVICE<br />#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;;<br />#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'stop')<br />#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /sbin/ifdown $DEVICE<br />#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;;<br />#&nbsp;&nbsp;&nbsp;&nbsp; esac<br />#&nbsp;&nbsp;&nbsp;&nbsp; exit 0<br /><br /># An alternate way of doing the same thing: (in this case identifying<br /># where the laptop is is done by configuring the interface as various<br /># options, and seeing if a computer that is known to be on each particular<br /># network will respond to pings. The various numbers here need to be chosen<br /># with a great deal of care.)<br />#<br /># iface eth0 inet manual<br />#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; up ifconfig $IFACE 0.0.0.0 up<br />#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; up ip link set $IFACE promisc on<br />#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; down ip link set $IFACE promisc off<br />#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; down ifconfig $IFACE down<br /><br /># Set up an interface which will not be allocated an IP address by<br /># ifupdown but will be configured through external programs. This<br /># can be useful to setup interfaces configured through other programs,<br /># like, for example, PPPOE scripts.<br />#<br /># auto eth0<br /># iface eth0 inet manual<br />#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; up ifconfig $IFACE 0.0.0.0 up<br />#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; up /usr/local/bin/myconfigscript<br />#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; down ifconfig $IFACE down</div><br />转自：<div>http://blog.chinaunix.net/uid-93067-id-90196.html</div><img src ="http://www.cppblog.com/LIULIANG/aggbug/195569.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2012-11-22 18:58 <a href="http://www.cppblog.com/LIULIANG/archive/2012/11/22/195569.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>网卡工作原理</title><link>http://www.cppblog.com/LIULIANG/archive/2012/11/22/195561.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Thu, 22 Nov 2012 10:01:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2012/11/22/195561.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/195561.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2012/11/22/195561.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/195561.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/195561.html</trackback:ping><description><![CDATA[<div style="layout-grid:  15.6pt none">
<p align="center"><strong><span style="font-family: 黑体; font-size: 15pt">网卡工作原理</span></strong></p>
<p style="text-indent: 21pt">Linux<span style="font-family: 宋体">操作系统的功能可以概括为进程管理、内存管理、文件系统管理、设备管理、网络等几部分。所有的系统操作最终都可以映射到对物理设备的操作。除去对</span>CPU<span style="font-family: 宋体">、内存以及其他少数几个物理实体的操作之外，系统对其他设备的所有操作都通过专门的称为驱动程序的代码完成。系统中存在的每种外设在内核中都必须有对应的设备驱动程序对其进行处理。所以分析网卡的工作原理即是分析网卡的驱动程序。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">网络是独立的一个模块。为了屏蔽网络环境中物理网络设备的多样性，</span>Linux<span style="font-family: 宋体">对所有的设备进行抽象并定义了一个统一的概念，称之为接口。所有对网络硬件的访问都是通过接口进行的，接口提供了一个对所有类型的硬件一致化的操作集合来处理基本数据发送和接收。一个网络接口被看作是一个发送和接收数据包的实体。对于每个网络接口，都用一个</span>net_device<span style="font-family: 宋体">的数据结构来表示。</span>net_device<span style="font-family: 宋体">中有很多提供系统访问和协议层调用的设备方法，包括提供设备初始化和往系统注册用的</span>init<span style="font-family: 宋体">函数，打开和关闭网络设备的</span>open<span style="font-family: 宋体">和</span>stop<span style="font-family: 宋体">函数，处理数据包发送的函数</span>hard_start_xmit<span style="font-family: 宋体">，以及中断处理函数。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">所有被发送和接收的包都用数据结构</span>sk_buff<span style="font-family: 宋体">表示。要发送数据时，网络系统将分局系统路由表选择相应的网络接口进行数据传输；当接收数据包时，通过驱动程序登记的中断服务程序进行数据的接口处理。</span></p>
<p style="text-indent: 21pt">Linux<span style="font-family: 宋体">网络驱动程序崇尚倒下分为四层：协议接口层、网络设备接口层、设备驱动功能层、网络设备和网络媒介层。如下图所示：</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体"><span style="font-size: 10.5pt" times="" new=""  ar-sa?="" 1.0pt;?? roman?;??><a href="http://blog.chinaunix.net/attachment/201102/27/23208702_1298783439GKfa.jpg" target="_blank"><img border="0" src="http://blog.chinaunix.net/attachment/201102/27/23208702_1298783439GKfa.jpg"  ;="" load="imgResize(this, 650);"  .  alt="" /></a> </span></span></p>
<p align="center"></p>
<p><strong><span style="font-family: 黑体; font-size: 14pt">网卡初始化</span></strong></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">网络设备初始化主要工作时检测设备的存在、初始化描述设备的</span>net_device<span style="font-family: 宋体">结构及在系统中登记该设备。在系统初始化完成以后，系统检测到的网络设备将保存在链表</span>dev_base<span style="font-family: 宋体">中，其中每个链表单元</span>net_device<span style="font-family: 宋体">对应一个存在的物理网络设备。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">初始化过程首先检测网络物理设备是否存在，这是通过检测物理设备的硬件特征来完成；然后对设备进行资源配置，这些完成之后就要构造设备的</span>net_device<span style="font-family: 宋体">数据结构，用检测到值对</span>net_device<span style="font-family: 宋体">中的变量初始化；最后</span>Linux<span style="font-family: 宋体">内核中注册该设备并申请内存空间。</span></p>
<p style="text-indent: 21.1pt"><strong>&nbsp;</strong></p>
<p><strong><span style="font-family: 黑体; font-size: 14pt">网卡的打开与关闭</span></strong></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">为了使用网络设备，需要打开网卡，打开和关闭的一个接口是由</span>shell<span style="font-family: 宋体">命令</span>ifconfig<span style="font-family: 宋体">调用的，而</span>ifconfig<span style="font-family: 宋体">则要调用一个通用的设备打开函数</span>dev_open<span style="font-family: 宋体">（</span>net/core/dev.c<span style="font-family: 宋体">），相应的还有一个</span>dev_close<span style="font-family: 宋体">函数，这两个函数提供独立于设备的操作接口的打开和关闭功能。一般打开函数执行的操作包括注册中断函数，分配并初始化网卡所需要的接收与发送缓冲区，启动硬件检查网络连接线状态等。</span></p>
<p style="text-indent: 21pt">&nbsp;</p>
<p><strong><span style="font-family: 黑体; font-size: 14pt">数据包的发送与接收</span></strong></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">数据包的发送和接收是实现</span>Linux<span style="font-family: 宋体">网络驱动程序中两个最关键的过程。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">当物理网络设备接收到数据是，系统通过两种途径解决这个问题。一种方法是轮询方式，另一种方式是中断法师。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">在轮询方式中，系统每隔一定的时间间隔就去检查一次物理设备，若设备有数据到达，就调用读取数据的程序。</span>Linux<span style="font-family: 宋体">中通过定时器实现，但是此法有一个明显的缺点：不管设备是否有数据，系统总是要固定的消耗</span>CPU<span style="font-family: 宋体">资源去查看设备，并且可能对一些紧急数据处理予以延迟。从资源的利用率以及工作的效率上看都不是最优的。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">中断方式利用硬件体系结构的中断机制实现设备和系统的应答对话，即当物理设备需要</span>CPU<span style="font-family: 宋体">处理数据时，就向</span>CPU<span style="font-family: 宋体">发送一个终端信号，系统则在收到信号后调用相应的中断服务程序响应对设备中断的处理。因此，基本在所有的网络设备驱动程序中都是用中断方式的。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">每一个网卡上都有一块</span>FIFO<span style="font-family: 宋体">存储器，对于</span>NIC<span style="font-family: 宋体">（</span>Network Interface Controller<span style="font-family: 宋体">），</span>FIFO<span style="font-family: 宋体">存储器是用来通过系统总线传送数据到系统存储器之前，缓存从</span>LAN<span style="font-family: 宋体">上接收到的数据。对与快速以太网还有一个直接内存存取（</span>DMA<span style="font-family: 宋体">：</span>Directly Memory Access<span style="font-family: 宋体">）控制器，用于提供对系统存储器的可靠访问。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">驱动为网卡分配一个环形缓冲区，在一段连续的物理内存中实现。</span></p>
<p style="text-indent: -18pt; margin-left: 39pt"><span>1、&nbsp;</span><span style="font-family: 宋体">数据接收</span></p>
<p style="text-indent: -36pt; margin-left: 57pt"><span>（1）<span style="font: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-family: 宋体">接收来自</span>MAC<span style="font-family: 宋体">的数据包，先暂存于片内</span>FIFO<span style="font-family: 宋体">接收队列；</span></p>
<p style="text-indent: -36pt; margin-left: 57pt"><span>（2）<span style="font: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-family: 宋体">当接收器达到早期接收上线时就移至环形缓冲区；</span></p>
<p style="text-indent: -36pt; margin-left: 57pt"><span>（3）<span style="font: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-family: 宋体">待整个数据包全部从</span>FIFO<span style="font-family: 宋体">移至缓存后，将接收状态寄存器和包长度写入接收的数据包头部，并更新</span>CBA<span style="font-family: 宋体">（</span>Current Buffer Address<span style="font-family: 宋体">）寄存器的值；</span></p>
<p style="text-indent: -36pt; margin-left: 57pt"><span>（4）<span style="font: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span>CMD<span style="font-family: 宋体">（</span>Command<span style="font-family: 宋体">）寄存器中的</span>BufferEmpty<span style="font-family: 宋体">位和</span>ISR<span style="font-family: 宋体">（中断状态寄存器）寄存器的</span>ROK<span style="font-family: 宋体">位置</span>1<span style="font-family: 宋体">，并发出</span>ROK<span style="font-family: 宋体">的中断；</span></p>
<p style="text-indent: -36pt; margin-left: 57pt"><span>（5）<span style="font: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span>ISR<span style="font-family: 宋体">中断调用完成后，清除</span>ISR<span style="font-family: 宋体">（</span>ROK<span style="font-family: 宋体">）并更新</span>CAPR<span style="font-family: 宋体">（</span>Current Address of Packet Read<span style="font-family: 宋体">，指向接收缓存的已读取包的地址），完成本次接收。</span></p>
<p style="text-indent: -18pt; margin-left: 39pt"><span>2、&nbsp;</span><span style="font-family: 宋体">数据发送</span></p>
<p style="text-indent: -36pt; margin-left: 57pt"><span>（1）<span style="font: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-family: 宋体">将待传送的数据写入主存中一段连续的缓存空间，由</span>OS<span style="font-family: 宋体">配合驱动程序完成；</span></p>
<p style="text-indent: -36pt; margin-left: 57pt"><span>（2）<span style="font: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-family: 宋体">找到一个可用的描述器，并写入内容，包括该数据包的开始物理地址和传输状态字（包的大小、可传送下限、</span>OWN<span style="font-family: 宋体">位）；</span></p>
<p style="text-indent: -36pt; margin-left: 57pt"><span>（3）<span style="font: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span>OWN<span style="font-family: 宋体">位有效，将数据从缓存移至片内</span>FIFO<span style="font-family: 宋体">队列；</span></p>
<p style="text-indent: -36pt; margin-left: 57pt"><span>（4）<span style="font: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-family: 宋体">当</span>FIFO<span style="font-family: 宋体">队列中的数据达到早期传送下限，</span>NIC<span style="font-family: 宋体">的传送单元就会启动，将数据顺序输出至线路；</span></p>
<p style="text-indent: -36pt; margin-left: 57pt"><span>（5）<span style="font: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-family: 宋体">当整个数据包都已经传至</span>FIFO<span style="font-family: 宋体">，</span>OWN<span style="font-family: 宋体">位置</span>1<span style="font-family: 宋体">；</span></p>
<p style="text-indent: -36pt; margin-left: 57pt"><span>（6）<span style="font: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-family: 宋体">当整个数据包都已经传至线路上，</span> TOK<span style="font-family: 宋体">寄存器置</span>1<span style="font-family: 宋体">；</span></p>
<p style="text-indent: -36pt; margin-left: 57pt"><span>（7）<span style="font: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-family: 宋体">当</span>TOK<span style="font-family: 宋体">（</span>IMR<span style="font-family: 宋体">）和</span>TOK<span style="font-family: 宋体">（</span>ISR<span style="font-family: 宋体">）多置</span>1<span style="font-family: 宋体">，就发出</span>TOK<span style="font-family: 宋体">中断；</span></p>
<p style="text-indent: -36pt; margin-left: 57pt"><span>（8）<span style="font: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span>TOK<span style="font-family: 宋体">中断调用完成以后，清除</span>TSD<span style="font-family: 宋体">状态字，完成本次传送。</span></p>
<p>&nbsp;</p>
<p><span style="font-family: 宋体">可以看出，网卡需要发送</span>/<span style="font-family: 宋体">接收数据，都必须以中断的方式告诉系统，系统处理中断后做出相应操作。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">网卡存在一定大小的</span>FIFO<span style="font-family: 宋体">存储器，同时还有缓冲区，缓冲区是由系统以及驱动共同分配一段连续的物理内存，所有的发送</span>/<span style="font-family: 宋体">接收的数据，都必须通过</span>FIFO<span style="font-family: 宋体">已经缓冲区，只有一包数据都发送成功后，才能继续发送下一包数据。系统维护缓冲区，只有当缓冲区有空间时才会接受上层来的数据，而网卡处理数据的速率远高于接收数据的最大速率，因此在网卡上不会存在堵塞情况。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">对编程而言，在应用层调用传输层函数</span>send/sendto<span style="font-family: 宋体">，使用套接字传送数据，屏蔽了底层的所有实现。此时，</span>send/sendto<span style="font-family: 宋体">函数是没有阻塞的，只要调用，必然有返回值，成功返回发送数据的长度，失败则返回负值（失败的主要原因是网络连接的问题），因此可能存在数据丢失的现象，需要写程序的时候保证数据的传输成功。但是只有</span>send/sendto<span style="font-family: 宋体">函数返回后，程序才会执行下一次发送，因此编程时没必要考虑数据会在传输层上出现阻塞。<br /><br /><font face="Verdana">转自：<a href="http://blog.chinaunix.net/uid-25839577-id-3035405.html">http://blog.chinaunix.net/uid-25839577-id-3035405.html</a></font></span></p></div><img src ="http://www.cppblog.com/LIULIANG/aggbug/195561.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2012-11-22 18:01 <a href="http://www.cppblog.com/LIULIANG/archive/2012/11/22/195561.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ASCII，Unicode和UTF-8</title><link>http://www.cppblog.com/LIULIANG/archive/2012/11/18/195326.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Sun, 18 Nov 2012 13:00:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2012/11/18/195326.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/195326.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2012/11/18/195326.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/195326.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/195326.html</trackback:ping><description><![CDATA[<p><span style="font-family: Arial; color: #333333" color="#333333" face="Arial"><span style="color: #ff0000; font-size: 18pt">UTF-8是Unicode的一种实现方式，也就是它的字节结构有特殊要求，所以我们说一个汉字的范围是0X4E00到0x9FA5，是指unicode值，至于放在utf-8的编码里去就是由三个字节来组织，所以可以看出unicode是给出一个字符的范围，定义了这个字是码值是多少，至于具体的实现方式可以有多种多样来实现。</span><br /><br /><br /><br /></span></p>
<p><span style="line-height: 26px; font-family: Arial; color: #333333" class="Apple-style-span">UTF-8是一种变长字节编码方式。对于某一个字符的UTF-8编码，如果只有一个字节则其最高二进制位为0；如果是多字节，其第一个字节从最高位开始，连续的二进制位值为1的个数决定了其编码的位数，其余各字节均以10开头。UTF-8最多可用到6个字节。&nbsp;<br />如表：&nbsp;<br />1字节 0xxxxxxx&nbsp;<br />2字节 110xxxxx 10xxxxxx&nbsp;<br />3字节 1110xxxx 10xxxxxx 10xxxxxx&nbsp;<br />4字节 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx&nbsp;<br />5字节 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx&nbsp;<br />6字节 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx&nbsp;<br />因此UTF-8中可以用来表示字符编码的实际位数最多有31位，即上表中x所表示的位。除去那些控制位（每字节开头的10等），这些x表示的位与UNICODE编码是一一对应的，位高低顺序也相同。&nbsp;<br />实际将UNICODE转换为UTF-8编码时应先去除高位0，然后根据所剩编码的位数决定所需最小的UTF-8编码位数。&nbsp;<br />因此那些基本ASCII字符集中的字符（UNICODE兼容ASCII）只需要一个字节的UTF-8编码（7个二进制位）便可以表示。&nbsp;<br /><br />对于上面的问题，代码中给出的两个字节是&nbsp;<br />十六进制：C0 B1&nbsp;<br />二进制：11000000 10110001&nbsp;<br />对比两个字节编码的表示方式：&nbsp;<br />110xxxxx 10xxxxxx&nbsp;<br />提取出对应的UNICODE编码：&nbsp;<br />00000 110001&nbsp;<br />可以看出此编码并非&#8220;标准&#8221;的UTF-8编码，因为其第一个字节的&#8220;有效编码&#8221;全为0，去除高位0后的编码仅有6位。由前面所述，此字符仅用一个字节的UTF-8编码表示就够了。&nbsp;<br />JAVA在把字符还原为UTF-8编码时，是按照&#8220;标准&#8221;的方式处理的，因此我们得到的是仅有1个字节的编码。&nbsp;<br /><br />大家可以试试运行这段代码：&nbsp;<br /></span></p>
<div class="cnblogs_Highlighter">
<div>
<div id="highlighter_751042" class="syntaxhighlighter nogutter  java ie">
<div class="toolbar"><span><a class="toolbar_item command_help help" href="http://www.cnblogs.com/chenwenbiao/archive/2011/08/11/2134503.html#">?</a></span></div>
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">TestUTF8 {&nbsp; </code></div>
<div class="line number2 index1 alt1"><code class="java keyword">public</code> <code class="java keyword">static</code> <code class="java keyword">void</code> <code class="java plain">main(String[] args) </code><code class="java keyword">throws</code> <code class="java plain">Exception {&nbsp; </code></div>
<div class="line number3 index2 alt2"><code class="java keyword">byte</code><code class="java plain">[][] bytes = {&nbsp; </code></div>
<div class="line number4 index3 alt1"><code class="java comments">// 00110001&nbsp; </code></div>
<div class="line number5 index4 alt2"><code class="java plain">{(</code><code class="java keyword">byte</code><code class="java plain">)</code><code class="java value">0x31</code><code class="java plain">},&nbsp; </code></div>
<div class="line number6 index5 alt1"><code class="java comments">// 11000000 10110001&nbsp; </code></div>
<div class="line number7 index6 alt2"><code class="java plain">{(</code><code class="java keyword">byte</code><code class="java plain">)</code><code class="java value">0xC0</code><code class="java plain">,(</code><code class="java keyword">byte</code><code class="java plain">)</code><code class="java value">0xB1</code><code class="java plain">},&nbsp; </code></div>
<div class="line number8 index7 alt1"><code class="java comments">// 11100000 10000000 10110001&nbsp; </code></div>
<div class="line number9 index8 alt2"><code class="java plain">{(</code><code class="java keyword">byte</code><code class="java plain">)</code><code class="java value">0xE0</code><code class="java plain">,(</code><code class="java keyword">byte</code><code class="java plain">)</code><code class="java value">0x80</code><code class="java plain">,(</code><code class="java keyword">byte</code><code class="java plain">)</code><code class="java value">0xB1</code><code class="java plain">},&nbsp; </code></div>
<div class="line number10 index9 alt1"><code class="java comments">// 11110000 10000000 10000000 10110001&nbsp; </code></div>
<div class="line number11 index10 alt2"><code class="java plain">{(</code><code class="java keyword">byte</code><code class="java plain">)</code><code class="java value">0xF0</code><code class="java plain">,(</code><code class="java keyword">byte</code><code class="java plain">)</code><code class="java value">0x80</code><code class="java plain">,(</code><code class="java keyword">byte</code><code class="java plain">)</code><code class="java value">0x80</code><code class="java plain">,(</code><code class="java keyword">byte</code><code class="java plain">)</code><code class="java value">0xB1</code><code class="java plain">},&nbsp; </code></div>
<div class="line number12 index11 alt1"><code class="java comments">// 11111000 10000000 10000000 10000000 10110001&nbsp; </code></div>
<div class="line number13 index12 alt2"><code class="java plain">{(</code><code class="java keyword">byte</code><code class="java plain">)</code><code class="java value">0xF8</code><code class="java plain">,(</code><code class="java keyword">byte</code><code class="java plain">)</code><code class="java value">0x80</code><code class="java plain">,(</code><code class="java keyword">byte</code><code class="java plain">)</code><code class="java value">0x80</code><code class="java plain">,(</code><code class="java keyword">byte</code><code class="java plain">)</code><code class="java value">0x80</code><code class="java plain">,(</code><code class="java keyword">byte</code><code class="java plain">)</code><code class="java value">0xB1</code><code class="java plain">},&nbsp; </code></div>
<div class="line number14 index13 alt1"><code class="java comments">// 11111100 10000000 10000000 10000000 10000000 10110001&nbsp; </code></div>
<div class="line number15 index14 alt2"><code class="java plain">{(</code><code class="java keyword">byte</code><code class="java plain">)</code><code class="java value">0xFC</code><code class="java plain">,(</code><code class="java keyword">byte</code><code class="java plain">)</code><code class="java value">0x80</code><code class="java plain">,(</code><code class="java keyword">byte</code><code class="java plain">)</code><code class="java value">0x80</code><code class="java plain">,(</code><code class="java keyword">byte</code><code class="java plain">)</code><code class="java value">0x80</code><code class="java plain">,(</code><code class="java keyword">byte</code><code class="java plain">)</code><code class="java value">0x80</code><code class="java plain">,(</code><code class="java keyword">byte</code><code class="java plain">)</code><code class="java value">0xB1</code><code class="java plain">},&nbsp; </code></div>
<div class="line number16 index15 alt1"><code class="java plain">};&nbsp; </code></div>
<div class="line number17 index16 alt2"><code class="java keyword">for</code> <code class="java plain">(</code><code class="java keyword">int</code> <code class="java plain">i = </code><code class="java value">0</code><code class="java plain">; i &lt; </code><code class="java value">6</code><code class="java plain">; i++) {&nbsp; </code></div>
<div class="line number18 index17 alt1"><code class="java plain">String str = </code><code class="java keyword">new</code> <code class="java plain">String(bytes[i], </code><code class="java string">"UTF-8"</code><code class="java plain">);&nbsp; </code></div>
<div class="line number19 index18 alt2"><code class="java plain">System.out.println(</code><code class="java string">"原数组长度："</code> <code class="java plain">+ bytes[i].length +&nbsp; </code></div>
<div class="line number20 index19 alt1"><code class="java string">"/t转换为字符串："</code> <code class="java plain">+ str +&nbsp; </code></div>
<div class="line number21 index20 alt2"><code class="java string">"/t转回后数组长度："</code> <code class="java plain">+ str.getBytes(</code><code class="java string">"UTF-8"</code><code class="java plain">).length);&nbsp; </code></div>
<div class="line number22 index21 alt1"><code class="java plain">}&nbsp; </code></div>
<div class="line number23 index22 alt2"><code class="java plain">}&nbsp; </code></div>
<div class="line number24 index23 alt1"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div></div>
<p>　　<span style="font-family: Arial; color: #333333" class="Apple-style-span">运行结果为：&nbsp;</span></p>
<p><span style="line-height: 26px; font-family: Arial; color: #333333" class="Apple-style-span">原数组长度：1 转换为字符串：1 转回后数组长度：1&nbsp;<br />原数组长度：2 转换为字符串：1 转回后数组长度：1&nbsp;<br />原数组长度：3 转换为字符串：1 转回后数组长度：1&nbsp;<br />原数组长度：4 转换为字符串：1 转回后数组长度：1&nbsp;<br />原数组长度：5 转换为字符串：1 转回后数组长度：1&nbsp;<br />原数组长度：6 转换为字符串：1 转回后数组长度：1&nbsp;</span></p>
<p><span style="line-height: 26px; font-family: Arial; color: #333333" class="Apple-style-span"><br /></span></p>
<p><span style="font-family: Arial; color: #333333" color="#333333" face="Arial"><br /><br /><br />另转：</span></p>
<p><span style="font-family: Arial; color: #333333" color="#333333" face="Arial"><span style="line-height: 21px; background-color: #f5f5d5; font-family: Georgia, serif; color: #111111; font-size: 12px; word-spacing: 2px" class="Apple-style-span"></span></span></p>
<h1 style="border-bottom: #d3d3d3 1px solid; text-align: left; padding-bottom: 0.2em; line-height: 72px; border-right-style: none; text-transform: capitalize; list-style-type: none; font-style: normal; margin: 0.4em 0px 0.2em; padding-left: 0px; padding-right: 0px; border-top-style: none; letter-spacing: -0.03em; color: #000000; font-size: 2.88em; border-left-style: none; font-weight: 900; text-decoration: none; padding-top: 0px" id="page-title" class="asset-name entry-title">字符编码笔记：ASCII，Unicode和UTF-8</h1>
<p><span style="font-family: Arial; color: #333333" color="#333333" face="Arial"><span style="line-height: 21px; background-color: #f5f5d5; font-family: Georgia, serif; color: #111111; font-size: 12px; word-spacing: 2px" class="Apple-style-span"></span></span><span style="line-height: 21px; background-color: #f5f5d5; font-family: Georgia, serif; color: #111111; font-size: 12px; word-spacing: 2px" class="Apple-style-span"></span></p>
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; border-right-style: none; list-style-type: none; font-style: normal; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; border-left-style: none; text-decoration: none; padding-top: 0px" class="asset-body">
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">今天中午，我突然想搞清楚Unicode和UTF-8之间的关系，于是就开始在网上查资料。</p></div>
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; border-right-style: none; list-style-type: none; font-style: normal; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; border-left-style: none; text-decoration: none; padding-top: 0px" id="more" class="asset-more">
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">结果，这个问题比我想象的复杂，从午饭后一直看到晚上9点，才算初步搞清楚。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">下面就是我的笔记，主要用来整理自己的思路。但是，我尽量试图写得通俗易懂，希望能对其他朋友有用。毕竟，字符编码是计算机技术的基石，想要熟练使用计算机，就必须懂得一点字符编码的知识。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px"><strong style="font-weight: 800">1. ASCII码</strong></p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">我们知道，在计算机内部，所有的信息最终都表示为一个二进制的字符串。每一个二进制位（bit）有0和1两种状态，因此八个二进制位就可以组合出256种状态，这被称为一个字节（byte）。也就是说，一个字节一共可以用来表示256种不同的状态，每一个状态对应一个符号，就是256个符号，从0000000到11111111。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">上个世纪60年代，美国制定了一套字符编码，对英语字符与二进制位之间的关系，做了统一规定。这被称为ASCII码，一直沿用至今。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">ASCII码一共规定了128个字符的编码，比如空格&#8220;SPACE&#8221;是32（二进制00100000），大写的字母A是65（二进制01000001）。这128个符号（包括32个不能打印出来的控制符号），只占用了一个字节的后面7位，最前面的1位统一规定为0。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px"><strong style="font-weight: 800">2、非ASCII编码</strong></p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">英语用128个符号编码就够了，但是用来表示其他语言，128个符号是不够的。比如，在法语中，字母上方有注音符号，它就无法用ASCII码表示。于是，一些欧洲国家就决定，利用字节中闲置的最高位编入新的符号。比如，法语中的&#233;的编码为130（二进制10000010）。这样一来，这些欧洲国家使用的编码体系，可以表示最多256个符号。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">但是，这里又出现了新的问题。不同的国家有不同的字母，因此，哪怕它们都使用256个符号的编码方式，代表的字母却不一样。比如，130在法语编码中代表了&#233;，在希伯来语编码中却代表了字母Gimel (ג)，在俄语编码中又会代表另一个符号。但是不管怎样，所有这些编码方式中，0&#8212;127表示的符号是一样的，不一样的只是128&#8212;255的这一段。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">至于亚洲国家的文字，使用的符号就更多了，汉字就多达10万左右。一个字节只能表示256种符号，肯定是不够的，就必须使用多个字节表达一个符号。比如，简体中文常见的编码方式是GB2312，使用两个字节表示一个汉字，所以理论上最多可以表示256x256=65536个符号。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">中文编码的问题需要专文讨论，这篇笔记不涉及。这里只指出，虽然都是用多个字节表示一个符号，但是GB类的汉字编码与后文的Unicode和UTF-8是毫无关系的。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px"><strong style="font-weight: 800">3.Unicode</strong></p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">正如上一节所说，世界上存在着多种编码方式，同一个二进制数字可以被解释成不同的符号。因此，要想打开一个文本文件，就必须知道它的编码方式，否则用错误的编码方式解读，就会出现乱码。为什么电子邮件常常出现乱码？就是因为发信人和收信人使用的编码方式不一样。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">可以想象，如果有一种编码，将世界上所有的符号都纳入其中。每一个符号都给予一个独一无二的编码，那么乱码问题就会消失。这就是Unicode，就像它的名字都表示的，这是一种所有符号的编码。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">Unicode当然是一个很大的集合，现在的规模可以容纳100多万个符号。每个符号的编码都不一样，比如，U+0639表示阿拉伯字母Ain，U+0041表示英语的大写字母A，U+4E25表示汉字&#8220;严&#8221;。具体的符号对应表，可以查询<a style="border-bottom-style: none; text-align: left; padding-bottom: 0px; border-right-style: none; list-style-type: none; font-style: normal; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #223472; border-left-style: none; text-decoration: underline; padding-top: 0px" href="http://www.unicode.org/" target="_blank">unicode.org</a>，或者专门的<a style="border-bottom-style: none; text-align: left; padding-bottom: 0px; border-right-style: none; list-style-type: none; font-style: normal; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #223472; border-left-style: none; text-decoration: underline; padding-top: 0px" href="http://www.chi2ko.com/tool/CJK.htm" target="_blank">汉字对应表</a>。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px"><strong style="font-weight: 800">4. Unicode的问题</strong></p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">需要注意的是，Unicode只是一个符号集，它只规定了符号的二进制代码，却没有规定这个二进制代码应该如何存储。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">比如，汉字&#8220;严&#8221;的unicode是十六进制数4E25，转换成二进制数足足有15位（100111000100101），也就是说这个符号的表示至少需要2个字节。表示其他更大的符号，可能需要3个字节或者4个字节，甚至更多。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">这里就有两个严重的问题，第一个问题是，如何才能区别unicode和ascii？计算机怎么知道三个字节表示一个符号，而不是分别表示三个符号呢？第二个问题是，我们已经知道，英文字母只用一个字节表示就够了，如果unicode统一规定，每个符号用三个或四个字节表示，那么每个英文字母前都必然有二到三个字节是0，这对于存储来说是极大的浪费，文本文件的大小会因此大出二三倍，这是无法接受的。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">它们造成的结果是：1）出现了unicode的多种存储方式，也就是说有许多种不同的二进制格式，可以用来表示unicode。2）unicode在很长一段时间内无法推广，直到互联网的出现。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px"><strong style="font-weight: 800">5.UTF-8</strong></p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">互联网的普及，强烈要求出现一种统一的编码方式。UTF-8就是在互联网上使用最广的一种unicode的实现方式。其他实现方式还包括UTF-16和UTF-32，不过在互联网上基本不用。<span style="color: #ff0000"><strong style="font-weight: 800">重复一遍，这里的关系是，UTF-8是Unicode的实现方式之一。</strong></span></p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">UTF-8最大的一个特点，就是它是一种变长的编码方式。它可以使用1~4个字节表示一个符号，根据不同的符号而变化字节长度。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">UTF-8的编码规则很简单，只有二条：</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">1）对于单字节的符号，字节的第一位设为0，后面7位为这个符号的unicode码。因此对于英语字母，UTF-8编码和ASCII码是相同的。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">2）对于n字节的符号（n&gt;1），第一个字节的前n位都设为1，第n+1位设为0，后面字节的前两位一律设为10。剩下的没有提及的二进制位，全部为这个符号的unicode码。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">下表总结了编码规则，字母x表示可用编码的位。</p>
<blockquote style="border-bottom-style: none; text-align: left; padding-bottom: 1em; border-right-style: none; background-color: #e0dfcc; list-style-type: none; font-style: normal; margin: 2em; padding-left: 1em; padding-right: 1em; border-top-style: none; color: #111111; border-left-style: none; text-decoration: none; padding-top: 1em; border-top-left-radius: 20px 20px; border-top-right-radius: 20px 20px; border-bottom-right-radius: 20px 20px; border-bottom-left-radius: 20px 20px">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; border-right-style: none; list-style-type: none; font-style: normal; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; border-left-style: none; text-decoration: none; padding-top: 0px">
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">Unicode符号范围 | UTF-8编码方式<br />(十六进制) | （二进制）<br />--------------------+---------------------------------------------<br />0000 0000-0000 007F | 0xxxxxxx<br />0000 0080-0000 07FF | 110xxxxx 10xxxxxx<br />0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx<br />0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx</p></div></blockquote>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">下面，还是以汉字&#8220;严&#8221;为例，演示如何实现UTF-8编码。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">已知&#8220;严&#8221;的unicode是4E25（100111000100101），根据上表，可以发现4E25处在第三行的范围内（0000 0800-0000 FFFF），因此&#8220;严&#8221;的UTF-8编码需要三个字节，即格式是&#8220;1110xxxx 10xxxxxx 10xxxxxx&#8221;。然后，从&#8220;严&#8221;的最后一个二进制位开始，依次从后向前填入格式中的x，多出的位补0。这样就得到了，&#8220;严&#8221;的UTF-8编码是&#8220;11100100 10111000 10100101&#8221;，转换成十六进制就是E4B8A5。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px"><strong style="font-weight: 800">6. Unicode与UTF-8之间的转换</strong></p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">通过上一节的例子，可以看到&#8220;严&#8221;的Unicode码是4E25，UTF-8编码是E4B8A5，两者是不一样的。它们之间的转换可以通过程序实现。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">在Windows平台下，有一个最简单的转化方法，就是使用内置的记事本小程序Notepad.exe。打开文件后，点击&#8220;文件&#8221;菜单中的&#8220;另存为&#8221;命令，会跳出一个对话框，在最底部有一个&#8220;编码&#8221;的下拉条。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px"><a style="border-bottom-style: none; text-align: left; padding-bottom: 0px; border-right-style: none; list-style-type: none; font-style: normal; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #223472; border-left-style: none; text-decoration: underline; padding-top: 0px" href="http://www.ruanyifeng.com/blog/2007/10/bg2007102801.jpg" target="_blank"><img style="border-bottom-style: solid; text-align: left; border-left: 1px solid; padding-bottom: 0px; list-style-type: none; font-style: normal; margin: 0px; padding-left: 0px; padding-right: 0px; color: #111111; border-top: 1px solid; border-right: 1px solid; text-decoration: none; padding-top: 0px" alt="bg2007102801.jpg" src="http://www.ruanyifeng.com/blog/2007/10/bg2007102801-thumb.jpg" width="500" height="227" /></a></p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">里面有四个选项：ANSI，Unicode，Unicode big endian 和 UTF-8。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">1）ANSI是默认的编码方式。对于英文文件是ASCII编码，对于简体中文文件是GB2312编码（只针对Windows简体中文版，如果是繁体中文版会采用Big5码）。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">2）Unicode编码指的是UCS-2编码方式，即直接用两个字节存入字符的Unicode码。这个选项用的little endian格式。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">3）Unicode big endian编码与上一个选项相对应。我在下一节会解释little endian和big endian的涵义。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">4）UTF-8编码，也就是上一节谈到的编码方法。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">选择完&#8221;编码方式&#8220;后，点击&#8221;保存&#8220;按钮，文件的编码方式就立刻转换好了。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px"><strong style="font-weight: 800">7. Little endian和Big endian</strong></p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">上一节已经提到，Unicode码可以采用UCS-2格式直接存储。以汉字&#8221;严&#8220;为例，Unicode码是4E25，需要用两个字节存储，一个字节是4E，另一个字节是25。存储的时候，4E在前，25在后，就是Big endian方式；25在前，4E在后，就是Little endian方式。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">这两个古怪的名称来自英国作家斯威夫特的《格列佛游记》。在该书中，小人国里爆发了内战，战争起因是人们争论，吃鸡蛋时究竟是从大头(Big-Endian)敲开还是从小头(Little-Endian)敲开。为了这件事情，前后爆发了六次战争，一个皇帝送了命，另一个皇帝丢了王位。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">因此，第一个字节在前，就是&#8221;大头方式&#8220;（Big endian），第二个字节在前就是&#8221;小头方式&#8220;（Little endian）。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">那么很自然的，就会出现一个问题：计算机怎么知道某一个文件到底采用哪一种方式编码？</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">Unicode规范中定义，每一个文件的最前面分别加入一个表示编码顺序的字符，这个字符的名字叫做&#8221;零宽度非换行空格&#8220;（ZERO WIDTH NO-BREAK SPACE），用FEFF表示。这正好是两个字节，而且FF比FE大1。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">如果一个文本文件的头两个字节是FE FF，就表示该文件采用大头方式；如果头两个字节是FF FE，就表示该文件采用小头方式。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px"><strong style="font-weight: 800">8. 实例</strong></p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">下面，举一个实例。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">打开&#8221;记事本&#8220;程序Notepad.exe，新建一个文本文件，内容就是一个&#8221;严&#8220;字，依次采用ANSI，Unicode，Unicode big endian 和 UTF-8编码方式保存。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">然后，用文本编辑软件<a style="border-bottom-style: none; text-align: left; padding-bottom: 0px; border-right-style: none; list-style-type: none; font-style: normal; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #223472; border-left-style: none; text-decoration: underline; padding-top: 0px" href="http://www.google.cn/search?aq=t&amp;oq=UltraEdit&amp;complete=1&amp;hl=zh-CN&amp;newwindow=1&amp;rlz=1B3GGGL_zh-CNCN216CN216&amp;q=ultraedit+%E4%B8%8B%E8%BD%BD&amp;btnG=Google+%E6%90%9C%E7%B4%A2&amp;meta=" target="_blank">UltraEdit中</a>的&#8221;十六进制功能&#8220;，观察该文件的内部编码方式。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">1）ANSI：文件的编码就是两个字节&#8220;D1 CF&#8221;，这正是&#8220;严&#8221;的GB2312编码，这也暗示GB2312是采用大头方式存储的。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">2）Unicode：编码是四个字节&#8220;FF FE 25 4E&#8221;，其中&#8220;FF FE&#8221;表明是小头方式存储，真正的编码是4E25。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">3）Unicode big endian：编码是四个字节&#8220;FE FF 4E 25&#8221;，其中&#8220;FE FF&#8221;表明是大头方式存储。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">4）UTF-8：编码是六个字节&#8220;EF BB BF E4 B8 A5&#8221;，前三个字节&#8220;EF BB BF&#8221;表示这是UTF-8编码，后三个&#8220;E4B8A5&#8221;就是&#8220;严&#8221;的具体编码，它的存储顺序与编码顺序是一致的。</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px"><strong style="font-weight: 800">9. 延伸阅读</strong></p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">*&nbsp;<a style="border-bottom-style: none; text-align: left; padding-bottom: 0px; border-right-style: none; list-style-type: none; font-style: normal; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #223472; border-left-style: none; text-decoration: underline; padding-top: 0px" href="http://www.joelonsoftware.com/articles/Unicode.html" target="_blank">The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets</a>（关于字符集的最基本知识）</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">*&nbsp;<a style="border-bottom-style: none; text-align: left; padding-bottom: 0px; border-right-style: none; list-style-type: none; font-style: normal; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #223472; border-left-style: none; text-decoration: underline; padding-top: 0px" href="http://www.pconline.com.cn/pcedu/empolder/gj/other/0505/616631.html" target="_blank">谈谈Unicode编码</a></p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">*&nbsp;<a style="border-bottom-style: none; text-align: left; padding-bottom: 0px; border-right-style: none; list-style-type: none; font-style: normal; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #223472; border-left-style: none; text-decoration: underline; padding-top: 0px" href="http://www.ietf.org/rfc/rfc3629.txt" target="_blank">RFC3629：UTF-8, a transformation format of ISO 10646</a>（如果实现UTF-8的规定）</p>
<p style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 28px; border-right-style: none; list-style-type: none; font-style: normal; margin: 1em 0px 0px 0.8em; padding-left: 0px; padding-right: 0px; border-top-style: none; color: #111111; font-size: 1.6em; border-left-style: none; text-decoration: none; padding-top: 0px">（完）</p></div><br /><br />转自：<a href="http://www.cnblogs.com/chenwenbiao/archive/2011/08/11/2134503.html">http://www.cnblogs.com/chenwenbiao/archive/2011/08/11/2134503.html</a><img src ="http://www.cppblog.com/LIULIANG/aggbug/195326.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2012-11-18 21:00 <a href="http://www.cppblog.com/LIULIANG/archive/2012/11/18/195326.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Ubuntu常用命令大全 </title><link>http://www.cppblog.com/LIULIANG/archive/2012/11/17/195299.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Sat, 17 Nov 2012 06:17:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2012/11/17/195299.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/195299.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2012/11/17/195299.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/195299.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/195299.html</trackback:ping><description><![CDATA[<div class="postTitle"><a id="viewpost1_TitleUrl" class="postTitle2" href="http://www.blogjava.net/bukebushuo/archive/2009/06/21/283427.html">Ubuntu常用命令大全</a> </div>Ubuntu常用命令大全 
<p>查看软件xxx安装内容<br /><strong>#dpkg -L xxx</strong></p>
<p>查找软件<br /><strong>#apt-cache search 正则表达式</strong><br />查找文件属于哪个包<br /><strong>#dpkg -S filename apt-file search filename</strong></p>
<p>查询软件xxx依赖哪些包<br /><strong>#apt-cache depends xxx</strong></p>
<p>查询软件xxx被哪些包依赖<br /><strong>#apt-cache rdepends xxx</strong></p>
<p>增加一个光盘源<br /><strong>#sudo apt-cdrom add</strong></p>
<p>系统升级<br /><strong>#sudo apt-get update<br />#sudo apt-get upgrade<br />#sudo apt-get dist-upgrade</strong></p>
<p>清除所以删除包的残余配置文件<br /><strong>#dpkg -l |grep ^rc|awk &#8216;{print $2}&#8217; |tr [&#8221;"n&#8221;] [&#8221; &#8220;]|sudo xargs dpkg -P -</strong></p>
<p>编译时缺少h文件的自动处理<br /><strong>#sudo auto-apt run ./configure</strong></p>
<p>查看安装软件时下载包的临时存放目录<br /><strong>#ls /var/cache/apt/archives</strong></p>
<p>备份当前系统安装的所有包的列表<br /><strong>#dpkg &#8211;get-selections | grep -v deinstall &gt; ~/somefile</strong></p>
<p>从上面备份的安装包的列表文件恢复所有包<br /><strong>#dpkg &#8211;set-selections &lt; ~/somefile sudo dselect</strong></p>
<p>清理旧版本的软件缓存<br /><strong>#sudo apt-get autoclean</strong></p>
<p>清理所有软件缓存<br /><strong>#sudo apt-get clean</strong></p>
<p>删除系统不再使用的孤立软件<br /><strong>#sudo apt-get autoremove</strong></p>
<p>查看包在服务器上面的地址<br /><strong>#apt-get -qq &#8211;print-uris install ssh | cut -d"&#8217; -f2</strong></p>
<h3><font color="#ff6600"><u><strong>系统</strong></u></font></h3>
<p>查看内核<br /><strong>#uname -a</strong></p>
<p>查看Ubuntu版本<br /><strong>#cat /etc/issue</strong></p>
<p>查看内核加载的模块<br /><strong>#lsmod</strong></p>
<p>查看PCI设备<br /><strong>#lspci</strong></p>
<p>查看USB设备<br /><strong>#lsusb</strong></p>
<p>查看网卡状态<br /><strong>#sudo ethtool eth0</strong></p>
<p>查看CPU信息<br /><strong>#cat /proc/cpuinfo</strong></p>
<p>显示当前硬件信息<br /><strong>#lshw</strong></p>
<h3><font color="#ff6600"><u><strong>硬盘</strong></u></font></h3>
<p>查看硬盘的分区<br /><strong>#sudo fdisk -l</strong></p>
<p>查看IDE硬盘信息<br /><strong>#sudo hdparm -i /dev/hda</strong></p>
<p>查看STAT硬盘信息<br /><strong>#sudo hdparm -I /dev/sda<br /></strong>或 <strong><br />#sudo apt-get install blktool<br />#sudo blktool /dev/sda id</strong></p>
<p>查看硬盘剩余空间<br /><strong>#df -h<br />#df -H</strong></p>
<p>查看目录占用空间<br /><strong>#du -hs 目录名</strong></p>
<p>优盘没法卸载<br /><strong>#sync fuser -km /media/usbdisk</strong></p>
<h3><font color="#ff6600"><u><strong>内存</strong></u></font></h3>
<p>查看当前的内存使用情况<br /><strong>#free -m</strong></p>
<h3><font color="#ff6600"><u><strong>进程</strong></u></font></h3>
<h3>查看当前有哪些进程<br /><strong>#ps -A</strong></h3>
<p>中止一个进程<br /><strong>#kill 进程号(就是ps -A中的第一列的数字) 或者 killall 进程名</strong></p>
<p>强制中止一个进程(在上面进程中止不成功的时候使用)<br /><strong>#kill -9 进程号 或者 killall -9 进程名</strong></p>
<p>图形方式中止一个程序<br /><strong>#xkill 出现骷髅标志的鼠标，点击需要中止的程序即可</strong></p>
<p>查看当前进程的实时状况<br /><strong>#top</strong></p>
<p>查看进程打开的文件<br /><strong>#lsof -p</strong></p>
<p>ADSL 配置 ADSL<br /><strong>#sudo pppoeconf</strong></p>
<p>ADSL手工拨号<br /><strong>#sudo pon dsl-provider</strong></p>
<p>激活 ADSL<br /><strong>#sudo /etc/ppp/pppoe_on_boot</strong></p>
<p>断开 ADSL<br /><strong>#sudo poff</strong></p>
<p>查看拨号日志<br /><strong>#sudo plog</strong></p>
<p>如何设置动态域名<br />#首先去http://www.3322.org申请一个动态域名<br />#然后修改 /etc/ppp/ip-up 增加拨号时更新域名指令<strong> sudo vim /etc/ppp/ip-up</strong><br />#在最后增加如下行 w3m -no-cookie -dump</p>
<h3><font color="#ff6600"><u><strong>网络</strong></u></font></h3>
<p>根据IP查网卡地址<br /><strong>#arping IP地址</strong></p>
<p>查看当前IP地址<br /><strong>#ifconfig eth0 |awk &#8216;/inet/ {split($2,x,&#8221;:&#8221;);print x[2]}&#8217;</strong></p>
<p>查看当前外网的IP地址<br /><strong>#w3m -no-cookie -dumpwww.edu.cn|grep-o</strong><strong>&#8216;[0-9]"{1,3"}".[0-9]"{1,3"}".[0-9]"{1,3"}".[0-9]"{1,3"}&#8217;<br />#w3m -no-cookie</strong> <strong>-dumpwww.xju.edu.cn|grep-o&#8217;[0-9]"{1,3"}".[0-9]"{1,3"}".[0-9]"{1,3"}".[0-9]"{1,3"}&#8217;</strong> <strong><br />#w3m -no-cookie -dump ip.loveroot.com|grep -o&#8217;[0-9]"{1,3"}".[0-9]"{1,3"}".[0-9]"{1,3"}".[0-9]"{1,3"}&#8217;</strong></p>
<p>查看当前监听80端口的程序<br /><strong>#lsof -i :80</strong></p>
<p>查看当前网卡的物理地址<br /><strong>#arp -a | awk &#8216;{print $4}&#8217; ifconfig eth0 | head -1 | awk &#8216;{print $5}&#8217;</strong></p>
<p>立即让网络支持nat<br /><strong>#sudo echo 1 &gt; /proc/sys/net/ipv4/ip_forward<br />#sudo iptables -t nat -I POSTROUTING -j MASQUERADE</strong></p>
<p>查看路由信息<br /><strong>#netstat -rn sudo route -n</strong></p>
<p>手工增加删除一条路由<br /><strong>#sudo route add -net 192.168.0.0 netmask 255.255.255.0 gw 172.16.0.1<br />#sudo route del -net 192.168.0.0 netmask 255.255.255.0 gw 172.16.0.1</strong></p>
<p>修改网卡MAC地址的方法<br /><strong>#sudo ifconfig eth0 down </strong>关闭网卡<strong><br />#sudo ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE</strong> 然后改地址<br />#<strong>sudo ifconfig eth0 up</strong> 然后启动网卡</p>
<p>统计当前IP连接的个数<br /><strong>#netstat -na|grep ESTABLISHED|awk &#8216;{print $5}&#8217;|awk -F: &#8216;{print $1}&#8217;|sort|uniq -c|sort -r -n<br />#netstat -na|grep SYN|awk &#8216;{print $5}&#8217;|awk -F: &#8216;{print $1}&#8217;|sort|uniq -c|sort -r -n</strong></p>
<p>统计当前20000个IP包中大于100个IP包的IP地址<br /><strong>#tcpdump -tnn -c 20000 -i eth0 | awk -F &#8220;.&#8221; &#8216;{print $1&#8243;.&#8221;$2&#8243;.&#8221;$3&#8243;.&#8221;$4}&#8217; | sort | uniq -c | sort -nr | awk &#8216; $1 &gt; 100 &#8216;</strong></p>
<p>屏蔽IPV6<br /><strong>#echo &#8220;blacklist ipv6&#8243; | sudo tee /etc/modprobe.d/blacklist-ipv6</strong></p>
<h3><font color="#ff6600"><u><strong>服务</strong></u></font></h3>
<p>添加一个服务<br /><strong>#sudo update-rc.d 服务名 defaults 99</strong></p>
<p>删除一个服务<br /><strong>#sudo update-rc.d 服务名 remove</strong></p>
<p>临时重启一个服务<br /><strong>#/etc/init.d/服务名 restart</strong></p>
<p>临时关闭一个服务<br /><strong>#/etc/init.d/服务名 stop</strong></p>
<p>临时启动一个服务<br /><strong>#/etc/init.d/服务名 start</strong></p>
<h3><font color="#ff6600"><u><strong>设置</strong></u></font></h3>
<p>配置默认Java使用哪个<br /><strong>#sudo update-alternatives &#8211;config java</strong></p>
<p>修改用户资料<br /><strong>#sudo chfn userid</strong></p>
<p>给apt设置代理<br /><strong>#export http_proxy=http://xx.xx.xx.xx:xxx</strong></p>
<p>修改系统登录信息<br /><strong>#sudo vim /etc/motd</strong></p>
<h3><font color="#ff6600"><u><strong>中文</strong></u></font></h3>
<p>转换文件名由GBK为UTF8<br /><strong>#sudo apt-get install convmv convmv -r -f cp936 -t utf8 &#8211;notest &#8211;nosmart *</strong></p>
<p>批量转换src目录下的所有文件内容由GBK到UTF8<br /><strong>#find src -type d -exec mkdir -p utf8/{} "; find src -type f -exec iconv -f GBK -t UTF-8 {} -o utf8/{} "; mv utf8/* src rm -fr utf8</strong></p>
<p>转换文件内容由GBK到UTF8<br /><strong>#iconv -f gbk -t utf8 $i &gt; newfile</strong></p>
<p>转换 mp3 标签编码<br /><strong>#sudo apt-get install python-mutagen find . -iname &#8220;*.mp3&#8221; -execdir mid3iconv -e GBK {} ";</strong></p>
<p>控制台下显示中文<br /><strong>#sudo apt-get install zhcon 使用时，输入zhcon即可</strong></p>
<h3><font color="#ff6600"><u><strong>文件</strong></u></font></h3>
<p>快速查找某个文件<br /><strong>#whereis filename</strong><br /><strong>#find 目录 -name 文件名</strong></p>
<p>查看文件类型<br /><strong>#file filename</strong></p>
<p>显示xxx文件倒数6行的内容<br /><strong>#tail -n 6 xxx</strong></p>
<p>让tail不停地读地最新的内容<br /><strong>#tail -n 10 -f /var/log/apache2/access.log</strong></p>
<p>查看文件中间的第五行（含）到第10行（含）的内容<br /><strong>#sed -n &#8216;5,10p&#8217; /var/log/apache2/access.log</strong></p>
<p>查找包含xxx字符串的文件<br /><strong>#grep -l -r xxx .</strong></p>
<p>全盘搜索文件(桌面可视化)<br /><strong>gnome-search-tool</strong></p>
<p>查找关于xxx的命令<br /><strong>#apropos xxx man -k xxx</strong></p>
<p>通过ssh传输文件<br /><strong>#scp -rp /path/filenameusername@remoteIP:/path</strong><br />#将本地文件拷贝到服务器上<br /><strong>#scp -rpusername@remoteIP:/path/filename/path</strong><br />#将远程文件从服务器下载到本地</p>
<p>查看某个文件被哪些应用程序读写<br /><strong>#lsof 文件名</strong></p>
<p>把所有文件的后辍由rm改为rmvb<br /><strong>#rename &#8217;s/.rm$/.rmvb/&#8217; *</strong></p>
<p>把所有文件名中的大写改为小写<br /><strong>#rename &#8216;tr/A-Z/a-z/&#8217; *</strong></p>
<p>删除特殊文件名的文件，如文件名：&#8211;help.txt<br /><strong>#rm &#8212; &#8211;help.txt 或者 rm ./&#8211;help.txt</strong></p>
<p>查看当前目录的子目录<br /><strong>#ls -d */. 或 echo */.</strong></p>
<p>将当前目录下最近30天访问过的文件移动到上级back目录<br /><strong>#find . -type f -atime -30 -exec mv {} ../back ";</strong></p>
<p>将当前目录下最近2小时到8小时之内的文件显示出来<br /><strong>#find . -mmin +120 -mmin -480 -exec more {} ";</strong></p>
<p>删除修改时间在30天之前的所有文件<br /><strong>#find . -type f -mtime +30 -mtime -3600 -exec rm {} ";</strong></p>
<p>查找guest用户的以avi或者rm结尾的文件并删除掉<br /><strong>#find . -name &#8216;*.avi&#8217; -o -name &#8216;*.rm&#8217; -user &#8216;guest&#8217; -exec rm {} ";</strong></p>
<p>查找的不以java和xml结尾,并7天没有使用的文件删除掉<br /><strong>#find . ! -name *.java ! -name &#8216;*.xml&#8217; -atime +7 -exec rm {} ";</strong></p>
<p>统计当前文件个数<br /><strong>#ls /usr/bin|wc -w</strong></p>
<p>统计当前目录个数<br /><strong>#ls -l /usr/bin|grep ^d|wc -l</strong></p>
<p>显示当前目录下2006-01-01的文件名<br /><strong>#ls -l |grep 2006-01-01 |awk &#8216;{print $8}&#8217;</strong></p>
<h3><font color="#ff6600"><u><strong>FTP</strong></u></font></h3>
<p>上传下载文件工具-filezilla<br /><strong>#sudo apt-get install filezilla<br /></strong></p>
<p>filezilla无法列出中文目录？<strong><br />站点-&gt;字符集-&gt;自定义-&gt;输入：GBK</strong></p>
<p>本地中文界面<br /><strong>1）</strong>下载filezilla中文包到本地目录，<strong>如~/</strong><br /><strong>2）#unrar x Filezilla3_zhCN.rar</strong><br /><strong>3) </strong>如果你没有unrar的话，请先<strong>安装rar和unrar</strong><br /><strong>#sudo apt-get install rar unrar</strong><br /><strong>#sudo ln -f /usr/bin/rar /usr/bin/unrar</strong><br /><strong>4）</strong>先<strong>备份</strong>原来的语言包,再安装；实际就是拷贝一个语言包。<br /><strong>#sudo cp /usr/share/locale/zh_CN/filezilla.mo /usr/share/locale/zh_CN/filezilla.mo.bak</strong><br /><strong>#sudo cp ~/locale/zh_CN/filezilla.mo /usr/share/locale/zh_CN/filezilla.mo</strong><br /><strong>5）重启</strong>filezilla,即可！</p>
<h3><font color="#ff6600"><u><strong>解压缩</strong></u></font></h3>
<p>解压缩 xxx.tar.gz<br /><strong>#tar -zxvf xxx.tar.gz</strong></p>
<p>解压缩 xxx.tar.bz2<br /><strong>#tar -jxvf xxx.tar.bz2</strong></p>
<p>压缩aaa bbb目录为xxx.tar.gz<br /><strong>#tar -zcvf xxx.tar.gz aaa bbb</strong></p>
<p>压缩aaa bbb目录为xxx.tar.bz2<br /><strong>#tar -jcvf xxx.tar.bz2 aaa bbb</strong></p>
<p>解压缩 RAR 文件<br /><strong>1) </strong>先安装<br /><strong>#sudo apt-get install rar unrar<br />#sudo ln -f /usr/bin/rar /usr/bin/unrar<br />2)</strong> 解压<br /><strong>#unrar x aaaa.rar</strong></p>
<p>解压缩 ZIP 文件<br /><strong>1) </strong>先安装<br /><strong>#sudo apt-get install zip unzip<br />#sudo ln -f /usr/bin/zip /usr/bin/unzip<br />2)</strong> 解压<br /><strong>#unzip x aaaa.zip</strong></p>
<h3><font color="#ff6600"><u><strong>Nautilus</strong></u></font></h3>
<p>显示隐藏文件<br /><strong>Ctrl+h</strong></p>
<p>显示地址栏<br /><strong>Ctrl+l</strong></p>
<p>特殊 URI 地址<br /><strong>* computer:/// </strong>- 全部挂载的设备和网络<br /><strong>* network:///</strong> - 浏览可用的网络<br /><strong>* burn:///</strong> - 一个刻录 CDs/DVDs 的数据虚拟目录<br /><strong>* smb:///</strong> - 可用的 windows/samba 网络资源<br /><strong>* x-nautilus-desktop:///</strong> - 桌面项目和图标<br /><strong>*file:///</strong>- 本地文件<br /><strong>* trash:///</strong> - 本地回收站目录<br /><strong>* ftp://</strong> - FTP 文件夹<br /><strong>* ssh:// </strong>- SSH 文件夹<br /><strong>* fonts:/// </strong>- 字体文件夹，可将字体文件拖到此处以完成安装<br /><strong>* themes:///</strong> - 系统主题文件夹</p>
<p>查看已安装字体<br /><strong>在nautilus的地址栏里输入&#8221;fonts:///&#8220;，就可以查看本机所有的fonts</strong></p>
<h3><font color="#ff6600"><u><strong>程序</strong></u></font></h3>
<p>详细显示程序的运行信息<br /><strong>#strace -f -F -o outfile</strong></p>
<p><strong>日期和时间</strong></p>
<p>设置日期<br /><strong>#date -s mm/dd/yy</strong></p>
<p>设置时间<br /><strong>#date -s HH:MM</strong></p>
<p>将时间写入CMOS<br /><strong>#hwclock &#8211;systohc</strong></p>
<p>读取CMOS时间<br /><strong>#hwclock &#8211;hctosys</strong></p>
<p>从服务器上同步时间<br /><strong>#sudo ntpdate time.nist.gov<br />#sudo ntpdate time.windows.com</strong></p>
<p><u><strong>控制台</strong></u></p>
<p>不同控制台间切换<br /><strong>Ctrl + ALT + &#8592; Ctrl + ALT + &#8594;</strong></p>
<p>指定控制台切换<br /><strong>Ctrl + ALT + Fn(n:1~7)</strong></p>
<p>控制台下滚屏<br /><strong>SHIFT + pageUp/pageDown</strong></p>
<p>控制台抓图<br /><strong>#setterm -dump n(n:1~7)</strong></p>
<h3><font color="#ff6600"><u><strong>数据库</strong></u></font></h3>
<p>mysql的数据库存放在地方<br /><strong>#/var/lib/mysql</strong></p>
<p>从mysql中导出和导入数据<strong><br />#mysqldump 数据库名 &gt; 文件名 </strong>#导出数据库<br /><strong>#mysqladmin create 数据库名 </strong>#建立数据库<br /><strong>#mysql 数据库名 &lt; 文件名 </strong>#导入数据库</p>
<p>忘了mysql的root口令怎么办<br /><strong>#sudo /etc/init.d/mysql stop</strong><br /><strong>#sudo mysqld_safe &#8211;skip-grant-tables </strong><br /><strong>#sudo mysqladmin -u user password &#8216;newpassword&#8221;<br />#sudo mysqladmin flush-privileges</strong></p>
<p>修改mysql的root口令<br /><strong>#sudo mysqladmin -uroot -p password &#8216;你的新密码&#8217;</strong></p>
<h3><font color="#ff6600"><u><strong>其它</strong></u></font></h3>
<p><strong>下载网站文档</strong><br /><strong>#wget -r -p -np -khttp://www.21cn.com</strong><br />&#183; r：在本机建立服务器端目录结构；<br />&#183; -p: 下载显示HTML文件的所有图片；<br />&#183; -np：只下载目标站点指定目录及其子目录的内容；<br />&#183; -k: 转换非相对链接为相对链接。</p>
<p>如何删除Totem电影播放机的播放历史记录<br /><strong>#rm ~/.recently-used</strong></p>
<p>如何更换gnome程序的快捷键<br />点击菜单，鼠标停留在某条菜单上，键盘输入任意你所需要的键，可以是组合键，会立即生效； 如果要清除该快捷键，请使用backspace</p>
<p>vim 如何显示彩色字符<br /><strong>#sudo cp /usr/share/vim/vimcurrent/vimrc_example.vim /usr/share/vim/vimrc</strong></p>
<p>如何在命令行删除在会话设置的启动程序<br /><strong>#cd ~/.config/autostart rm 需要删除启动程序</strong></p>
<p>如何提高wine的反应速度<br /><strong>#sudo sed -ie &#8216;/GBK/,/^}/d&#8217; /usr/share/X11/locale/zh_CN.UTF-8/XLC_LOCALE</strong></p>
<p><strong>#chgrp</strong><br />[语法]: chgrp [-R] 文件组 文件&#8230;<br />[说明]： 文件的GID表示文件的文件组，文件组可用数字表示， 也可用一个有效的组名表示，此命令改变一个文件的GID，可参看chown。<br />-R 递归地改变所有子目录下所有文件的存取模式<br />[例子]:<br /><strong>＃chgrp group file </strong>将文件 file 的文件组改为 group</p>
<p><strong>#chmod</strong><br />[语法]: chmod [-R] 模式 文件&#8230;<br />或 chmod [ugoa] {+|-|=} [rwxst] 文件&#8230;<br />[说明]: 改变文件的存取模式，存取模式可表示为数字或符号串，例如：<br /><strong>＃chmod nnnn file</strong> ， n为0-7的数字，意义如下:<br />4000 运行时可改变UID<br />2000 运行时可改变GID<br />1000 置粘着位<br />0400 文件主可读<br />0200 文件主可写<br />0100 文件主可执行<br />0040 同组用户可读<br />0020 同组用户可写<br />0010 同组用户可执行<br />0004 其他用户可读<br />0002 其他用户可写<br />0001 其他用户可执行<br />nnnn 就是上列数字相加得到的，例如 chmod 0777 file 是指将文件 file 存取权限置为所有用户可读可写可执行。<br />-R 递归地改变所有子目录下所有文件的存取模式<br />u 文件主<br />g 同组用户<br />o 其他用户<br />a 所有用户<br />+ 增加后列权限<br />- 取消后列权限<br />= 置成后列权限<br />r 可读<br />w 可写<br />x 可执行<br />s 运行时可置UID<br />t 运行时可置GID<br />[例子]:<br /><strong>＃chmod 0666</strong> file1 file2 将文件 file1 及 file2 置为所有用户可读可写<br /><strong>＃chmod u+x file</strong> 对文件 file 增加文件主可执行权限<br /><strong>＃chmod o-rwx</strong> 对文件file 取消其他用户的所有权限</p>
<p><strong>#chown</strong><br />[语法]: chown [-R] 文件主 文件&#8230;<br />[说明]: 文件的UID表示文件的文件主，文件主可用数字表示， 也可用一个有效的用户名表示，此命令改变一个文件的UID，仅当此文件的文件主或超级用户可使用。<br />-R 递归地改变所有子目录下所有文件的存取模式<br />[例子]:<br /><strong>#chown mary file </strong>将文件 file 的文件主改为 mary<br /><strong>#chown 150 file</strong> 将文件 file 的UID改为150</p>
<h3><u><font color="#ff6600">Ubuntu命令行下修改网络配置</font></u></h3>
<p><strong>以eth0为例</strong><br /><strong>1. 以DHCP方式配置网卡</strong><br />编辑文件/etc/network/interfaces:<br /><strong>#sudo vi /etc/network/interfaces</strong><br />并用下面的行来替换有关eth0的行:<br /># The primary network interface - use DHCP to find our address<br />auto eth0<br />iface eth0 inet dhcp<br />用下面的命令使网络设置生效:<br /><strong>#sudo /etc/init.d/networking restart</strong><br />当然,也可以在命令行下直接输入下面的命令来获取地址<strong><br />#sudo dhclient eth0</strong></p>
<p><strong>2. 为网卡配置静态IP地址</strong><br />编辑文件/etc/network/interfaces:<br /><strong>#sudo vi /etc/network/interfaces</strong><br />并用下面的行来替换有关eth0的行:<br /># The primary network interface<br />auto eth0<br />iface eth0 inet static<br />address 192.168.3.90<br />gateway 192.168.3.1<br />netmask 255.255.255.0<br />network 192.168.3.0<br />broadcast 192.168.3.255<br />将上面的ip地址等信息换成你自己就可以了.</p>
<p>用下面的命令使网络设置生效:<br /><strong>#sudo /etc/init.d/networking restart</strong></p>
<p><strong>3. 设定第二个IP地址(虚拟IP地址)</strong><br />编辑文件/etc/network/interfaces:<br /><strong>#sudo vi /etc/network/interfaces</strong><br />在该文件中添加如下的行:<br />auto eth0:1<br />iface eth0:1 inet static<br />address 192.168.1.60<br />netmask 255.255.255.0<br />network x.x.x.x<br />broadcast x.x.x.x<br />gateway x.x.x.x<br />根据你的情况填上所有诸如address,netmask,network,broadcast和gateways等信息.<br />用下面的命令使网络设置生效:<br /><strong>#sudo /etc/init.d/networking restart</strong></p>
<p><strong>4. 设置主机名称(hostname)</strong><br />使用下面的命令来查看当前主机的主机名称:<br /><strong>#sudo /bin/hostname</strong><br />使用下面的命令来设置当前主机的主机名称:<br /><strong>#sudo /bin/hostname newname</strong><br />系统启动时,它会从/etc/hostname来读取主机的名称.</p>
<p><strong>5. 配置DNS</strong><br />首先,你可以在/etc/hosts中加入一些主机名称和这些主机名称对应的IP地址,这是简单使用本机的静态查询.<br />要访问DNS 服务器来进行查询,需要设置/etc/resolv.conf文件.<br />假设DNS服务器的IP地址是192.168.3.2, 那么/etc/resolv.conf文件的内容应为:<br />search test.com<br />nameserver 192.168.3.2</p>
<h3><font color="#ff6600"><u><strong>安装AMP服务</strong></u></font></h3>
<p>如果采用Ubuntu Server CD开始安装时，可以选择安装，这系统会自动装上apache2,php5和mysql5。下面主要说明一下如果不是安装的Ubuntu server时的安装方法。<br />用命令在Ubuntu下架设Lamp其实很简单，用一条命令就完成。在终端输入以下命令：<br /><strong>#sudo apt-get install apache2 mysql-server php5 php5-mysql php5-gd #phpmyadmin</strong><br />装好后，<strong>mysql管理员是root，无密码</strong>，通过http://localhost/phpmyadmin就可以访问mysql了</p>
<p><u><strong>修改 MySql 密码</strong></u><br />终端下输入：<br /><strong>#mysql -u root</strong><br /><strong>#mysql&gt; GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY &#8220;123456&#8243;;</strong><br />&#8217;123456&#8216;是root的密码，可以自由设置，但最好是设个安全点的。<br /><strong>#mysql&gt; quit; </strong>退出mysql</p>
<p><u><strong>apache2的操作命令</strong></u><br />启动：<strong>#sudo /etc/init.d/apache2 start</strong><br />重启：<strong>#sudo /etc/init.d/apache2 restart</strong><br />关闭：<strong>#sudo /etc/init.d/apache2 stop</strong><br /><strong>apache2的默认主目录：/var/www/<br /><br /><br /><br />转自：<a href="http://www.blogjava.net/bukebushuo/archive/2009/08/27/283427.html">http://www.blogjava.net/bukebushuo/archive/2009/08/27/283427.html</a></strong></p><img src ="http://www.cppblog.com/LIULIANG/aggbug/195299.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2012-11-17 14:17 <a href="http://www.cppblog.com/LIULIANG/archive/2012/11/17/195299.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>http请求方式</title><link>http://www.cppblog.com/LIULIANG/archive/2012/11/14/195202.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Wed, 14 Nov 2012 12:39:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2012/11/14/195202.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/195202.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2012/11/14/195202.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/195202.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/195202.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: HTTP请求格式当浏览器向Web服务器发出请求时，它向服务器传递了一个数据块，也就是请求信息，HTTP请求信息由3部分组成：l&nbsp;&nbsp; 请求方法URI协议/版本l&nbsp;&nbsp; 请求头(Request Header)l&nbsp;&nbsp; 请求正文下面是一个HTTP请求的例子：GET/sample.jspHTTP/1.1&nbsp;...&nbsp;&nbsp;<a href='http://www.cppblog.com/LIULIANG/archive/2012/11/14/195202.html'>阅读全文</a><img src ="http://www.cppblog.com/LIULIANG/aggbug/195202.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2012-11-14 20:39 <a href="http://www.cppblog.com/LIULIANG/archive/2012/11/14/195202.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>