﻿<?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++博客-智慧掩盖真相</title><link>http://www.cppblog.com/birdshover/</link><description /><language>zh-cn</language><lastBuildDate>Wed, 08 Apr 2026 22:13:05 GMT</lastBuildDate><pubDate>Wed, 08 Apr 2026 22:13:05 GMT</pubDate><ttl>60</ttl><item><title>用HGE写了个贪吃蛇，自娱自乐一下</title><link>http://www.cppblog.com/birdshover/archive/2009/09/05/95357.html</link><dc:creator>BirdsHover</dc:creator><author>BirdsHover</author><pubDate>Sat, 05 Sep 2009 00:50:00 GMT</pubDate><guid>http://www.cppblog.com/birdshover/archive/2009/09/05/95357.html</guid><wfw:comment>http://www.cppblog.com/birdshover/comments/95357.html</wfw:comment><comments>http://www.cppblog.com/birdshover/archive/2009/09/05/95357.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/birdshover/comments/commentRss/95357.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/birdshover/services/trackbacks/95357.html</trackback:ping><description><![CDATA[

项目为VS2008项目，设置项目属性/C.C++附加包含目录为hge1.8的Include目录，设置链接器/输入 的 附加依赖hge.lib hgehelp.lib，同时，忽略特定库libc.lib<br><br>OK,这样项目就可跑起来了。<br><br>主要用到了hge里的菜单，和绘制矩形，比较简单。<br><br>下载地址：<a href="http://files.cnblogs.com/birdshover/HGETanChiShe.rar" target="_blank">下载地址1</a><br><br>文件大小500K，运行文件和项目文件都在里面。<img src ="http://www.cppblog.com/birdshover/aggbug/95357.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/birdshover/" target="_blank">BirdsHover</a> 2009-09-05 08:50 <a href="http://www.cppblog.com/birdshover/archive/2009/09/05/95357.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于union</title><link>http://www.cppblog.com/birdshover/archive/2009/06/18/88019.html</link><dc:creator>BirdsHover</dc:creator><author>BirdsHover</author><pubDate>Thu, 18 Jun 2009 11:17:00 GMT</pubDate><guid>http://www.cppblog.com/birdshover/archive/2009/06/18/88019.html</guid><wfw:comment>http://www.cppblog.com/birdshover/comments/88019.html</wfw:comment><comments>http://www.cppblog.com/birdshover/archive/2009/06/18/88019.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/birdshover/comments/commentRss/88019.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/birdshover/services/trackbacks/88019.html</trackback:ping><description><![CDATA[
<span style="font-family: 'lucida grande'; font-size: 11px; white-space: pre; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "><span style="FONT-SIZE: 12pt ">union结构中，成员是共享内存的。
有朋友问
union
{  
    int i[2];
    long k;
    char c[4];
}r,*s=&amp;r;
	
s-&gt;i[0]=0x39;
s-&gt;i[1]=0x38;

那么printf("%c\n",s-&gt;c[0]);输出什么？

输出是9，百思不得其解。现在想来，是忽略了输出的是char，而0x39作为asc2码，对应的字符就是9.

union的成员内存共享，那么,&amp;(s-&gt;i[0]) == &amp;(s-&gt;k) == &amp;(s-&gt;c[0])
在s-&gt;i[0]小于等于8bit的情况下，s-&gt;c[0] == s-&gt;i[0]
也就是说s-&gt;c[0] == 0x39 == 57也就是字符'9'

假如s-&gt;[0] &gt; 0x99 那么，s-&gt;[0]就是s-&gt;i[0]的前8bit的值。

特殊一点的情况，s-&gt;i[0]=0x39393939;
那么s-&gt;c[0] == s-&gt;c[1] == s-&gt;c[2] == s-&gt;c[3] == 0x39 == 57 == '9'</span></span><img src ="http://www.cppblog.com/birdshover/aggbug/88019.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/birdshover/" target="_blank">BirdsHover</a> 2009-06-18 19:17 <a href="http://www.cppblog.com/birdshover/archive/2009/06/18/88019.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>CString 按符号切割为数组</title><link>http://www.cppblog.com/birdshover/archive/2009/01/05/71214.html</link><dc:creator>BirdsHover</dc:creator><author>BirdsHover</author><pubDate>Mon, 05 Jan 2009 04:04:00 GMT</pubDate><guid>http://www.cppblog.com/birdshover/archive/2009/01/05/71214.html</guid><wfw:comment>http://www.cppblog.com/birdshover/comments/71214.html</wfw:comment><comments>http://www.cppblog.com/birdshover/archive/2009/01/05/71214.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/birdshover/comments/commentRss/71214.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/birdshover/services/trackbacks/71214.html</trackback:ping><description><![CDATA[
函数:<br><br><div style="background-color: rgb(238, 238, 238); font-size: 13px; border-left-color: rgb(204, 204, 204); padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&#160;SuperCString::Split(CString&#160;rawString,</span><span style="color: #0000FF; ">char</span><span style="color: #000000; ">&#160;sp,CString</span><span style="color: #000000; ">*</span><span style="color: #000000; ">&#160;sp_arr[],</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">*</span><span style="color: #000000; ">&#160;length)<br>{<br>&#160;&#160;&#160;&#160;<br>&#160;&#160;&#160;&#160;CString</span><span style="color: #000000; ">*</span><span style="color: #000000; ">&#160;arr&#160;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&#160;</span><span style="color: #0000FF; ">new</span><span style="color: #000000; ">&#160;CString[</span><span style="color: #000000; ">*</span><span style="color: #000000; ">length];<br>&#160;&#160;&#160;&#160;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&#160;pos&#160;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&#160;</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br>&#160;&#160;&#160;&#160;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&#160;repeatcount&#160;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&#160;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br>&#160;&#160;&#160;&#160;</span><span style="color: #0000FF; ">while</span><span style="color: #000000; ">(pos&#160;</span><span style="color: #000000; ">></span><span style="color: #000000; ">&#160;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">){<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(repeatcount&#160;</span><span style="color: #000000; ">>=</span><span style="color: #000000; ">&#160;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">length){<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color: #0000FF; ">break</span><span style="color: #000000; ">;<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;pos&#160;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&#160;rawString.Find(sp,</span><span style="color: #000000; ">0</span><span style="color: #000000; ">);<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(pos&#160;</span><span style="color: #000000; ">></span><span style="color: #000000; ">&#160;</span><span style="color: #000000; ">-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">)<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;arr[repeatcount]&#160;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&#160;rawString.Mid(</span><span style="color: #000000; ">0</span><span style="color: #000000; ">,pos&#160;</span><span style="color: #000000; ">+</span><span style="color: #000000; ">&#160;</span><span style="color: #000000; ">1</span><span style="color: #000000; ">);<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color: #0000FF; ">else</span><span style="color: #000000; "><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;arr[repeatcount]&#160;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&#160;rawString;<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;rawString&#160;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&#160;rawString.Mid(pos&#160;</span><span style="color: #000000; ">+</span><span style="color: #000000; ">&#160;</span><span style="color: #000000; ">1</span><span style="color: #000000; ">);<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;repeatcount</span><span style="color: #000000; ">++</span><span style="color: #000000; ">;<br>&#160;&#160;&#160;&#160;}<br>&#160;&#160;&#160;&#160;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">length&#160;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&#160;repeatcount;<br>&#160;&#160;&#160;&#160;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">sp_arr&#160;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&#160;arr;<br>}<br></span></div><br>调用方式:<br><div style="background-color: rgb(238, 238, 238); font-size: 13px; border-left-color: rgb(204, 204, 204); padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000; ">&#160;&#160;&#160;&#160;SuperCString&#160;scs;<br>&#160;&#160;&#160;&#160;CString</span><span style="color: #000000; ">*</span><span style="color: #000000; ">&#160;arr&#160;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&#160;</span><span style="color: #0000FF; ">new</span><span style="color: #000000; ">&#160;CString[</span><span style="color: #000000; ">5</span><span style="color: #000000; ">];<br>&#160;&#160;&#160;&#160;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&#160;len&#160;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&#160;</span><span style="color: #000000; ">5</span><span style="color: #000000; ">;<br>&#160;&#160;&#160;&#160;scs.Split(header,</span><span style="color: #000000; ">'</span><span style="color: #000000; ">\n</span><span style="color: #000000; ">'</span><span style="color: #000000; ">,</span><span style="color: #000000; ">&amp;</span><span style="color: #000000; ">arr,</span><span style="color: #000000; ">&amp;</span><span style="color: #000000; ">len);<br>&#160;&#160;&#160;&#160;CString&#160;outaa;<br>&#160;&#160;&#160;&#160;outaa.Format(L</span><span style="color: #000000; ">"</span><span style="color: #000000; ">%d</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,len);<br>&#160;&#160;&#160;&#160;</span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&#160;i&#160;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&#160;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;i</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">len;i</span><span style="color: #000000; ">++</span><span style="color: #000000; ">){<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;outaa&#160;</span><span style="color: #000000; ">+=</span><span style="color: #000000; ">&#160;L</span><span style="color: #000000; ">"</span><span style="color: #000000; ">\r\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">&#160;</span><span style="color: #000000; ">+</span><span style="color: #000000; ">&#160;arr[i];<br>&#160;&#160;&#160;&#160;}<br>&#160;&#160;&#160;&#160;AfxMessageBox(outaa);<br>&#160;&#160;&#160;&#160;</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">;</span></div><img src ="http://www.cppblog.com/birdshover/aggbug/71214.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/birdshover/" target="_blank">BirdsHover</a> 2009-01-05 12:04 <a href="http://www.cppblog.com/birdshover/archive/2009/01/05/71214.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>记录wchar_t 与char的换算</title><link>http://www.cppblog.com/birdshover/archive/2008/12/19/69844.html</link><dc:creator>BirdsHover</dc:creator><author>BirdsHover</author><pubDate>Fri, 19 Dec 2008 08:40:00 GMT</pubDate><guid>http://www.cppblog.com/birdshover/archive/2008/12/19/69844.html</guid><wfw:comment>http://www.cppblog.com/birdshover/comments/69844.html</wfw:comment><comments>http://www.cppblog.com/birdshover/archive/2008/12/19/69844.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/birdshover/comments/commentRss/69844.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/birdshover/services/trackbacks/69844.html</trackback:ping><description><![CDATA[
		<p>开始尝试了以下两个方法<br />//*************************************************************//<br />//wchar_t 转换 为char<br />//*************************************************************//<br />bool wstr2cstr(wchar_t *orig,char * target)<br />{<br /> size_t origsize = wcslen(orig) + 1;<br /> size_t convertedChars = 0;<br /> wcstombs_s(&amp;convertedChars, target, origsize, orig, _TRUNCATE);<br /> return true;<br />}</p>
		<p>//*************************************************************//<br />//char 转换 为wchar_t<br />//*************************************************************//<br />bool cstr2wstr(char *orig,wchar_t * target)<br />{<br /> size_t origsize = strlen(orig) + 1;<br /> size_t convertedChars = 0;<br /> mbstowcs_s(&amp;convertedChars, target, origsize, orig, _TRUNCATE);<br /> return true;<br />}<br /><br />发现wstr2cstr没有反应，cstr2wstr正常。想不通。<br /><br />后来换成<br /><br />//*************************************************************//<br />// 将 宽字节wchar_t* 转换 单字节char*<br />//*************************************************************//<br />void UnicodeToAnsi( const wchar_t* szStr, char* pResult)<br />{<br /> int nLen = WideCharToMultiByte( CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL );<br /> if (nLen == 0)<br /> {<br />    return;<br /> }<br /> WideCharToMultiByte( CP_ACP, 0, szStr, -1, pResult, nLen, NULL, NULL );<br />}<br />//*************************************************************//<br />//将 单字节char* 转换为 宽字节 wchar*<br />//*************************************************************//<br />void AnsiToUnicode( const char* szStr , wchar_t* pResult )<br />{<br /> int nLen = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, NULL, 0 );<br /> if (nLen == 0)<br /> {<br />    return;<br /> }<br /> MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, pResult, nLen );<br />}<br /><br />就好了</p>
<img src ="http://www.cppblog.com/birdshover/aggbug/69844.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/birdshover/" target="_blank">BirdsHover</a> 2008-12-19 16:40 <a href="http://www.cppblog.com/birdshover/archive/2008/12/19/69844.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>conio.h，像C#一样ReadKey</title><link>http://www.cppblog.com/birdshover/archive/2008/12/18/69758.html</link><dc:creator>BirdsHover</dc:creator><author>BirdsHover</author><pubDate>Thu, 18 Dec 2008 11:39:00 GMT</pubDate><guid>http://www.cppblog.com/birdshover/archive/2008/12/18/69758.html</guid><wfw:comment>http://www.cppblog.com/birdshover/comments/69758.html</wfw:comment><comments>http://www.cppblog.com/birdshover/archive/2008/12/18/69758.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/birdshover/comments/commentRss/69758.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/birdshover/services/trackbacks/69758.html</trackback:ping><description><![CDATA[getchar()函数不是很好用， <br />//-------------------------------------------------------------------- #<br /> <br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">#include </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">stdafx.h</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br />#include </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">windows.h</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br />#include  </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"> <br /></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">  std;<br /><br />#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">conio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /><br /></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> ESC_CODE  27</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> KEYB_UP   72</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> KEYB_DOWN 80</span><span style="COLOR: #000000"><br /><br /></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> _tmain(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> argc, _TCHAR</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> argv[])<br />{<br />    </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> ch;<br /><br />    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Push up or down key, Esc to exit program.\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br /><br />    </span><span style="COLOR: #0000ff">do</span><span style="COLOR: #000000"><br />    {<br />        </span><span style="COLOR: #0000ff">switch</span><span style="COLOR: #000000">(ch</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">getch())<br />        {<br />        </span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000"> KEYB_UP:<br />            printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">up\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />            </span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br />        </span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000"> KEYB_DOWN:<br />            printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">down\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />            </span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;    <br />        }<br />    }</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(ch </span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000"> ESC_CODE);<br /><br />    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br />}</span></div><br />//-------------------------------------------------------------------- <br /><br />上面这段代码就可以让Console处于等待输入状态了。<img src ="http://www.cppblog.com/birdshover/aggbug/69758.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/birdshover/" target="_blank">BirdsHover</a> 2008-12-18 19:39 <a href="http://www.cppblog.com/birdshover/archive/2008/12/18/69758.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>bat 命令备忘</title><link>http://www.cppblog.com/birdshover/archive/2008/11/28/68095.html</link><dc:creator>BirdsHover</dc:creator><author>BirdsHover</author><pubDate>Fri, 28 Nov 2008 10:22:00 GMT</pubDate><guid>http://www.cppblog.com/birdshover/archive/2008/11/28/68095.html</guid><wfw:comment>http://www.cppblog.com/birdshover/comments/68095.html</wfw:comment><comments>http://www.cppblog.com/birdshover/archive/2008/11/28/68095.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/birdshover/comments/commentRss/68095.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/birdshover/services/trackbacks/68095.html</trackback:ping><description><![CDATA[
		<div class="tit"> </div>
		<p>
		</p>
		<table style="TABLE-LAYOUT: fixed">
				<tbody>
						<tr>
								<td>
										<div class="cnt" id="blog_text">
												<table style="TABLE-LAYOUT: fixed">
														<tbody>
																<tr>
																		<td>
																				<div class="cnt">
																						<pre>1 echo 和 @</pre>
																						<pre>@                      #关闭单行回显
echo off               #从下一行开始关闭回显
@echo off              #从本行开始关闭回显。一般批处理第一行都是这个
echo on                #从下一行开始打开回显
echo                   #显示当前是 echo off 状态还是 echo on 状态
<span>echo.                  #输出一个”回车换行”，空白行
                          #(</span><span>同</span>echo, echo; echo+ echo[ echo] echo/ echo\)
 
2 errorlevel</pre>
																						<pre>echo %errorlevel%
每个命令运行结束，可以用这个命令行格式查看返回码
默认值为0，一般命令执行出错会设 errorlevel 为1</pre>
																						<pre>3 dir</pre>
																						<pre>dir                   #显示当前目录中的文件和子目录
dir /a                #显示当前目录中的文件和子目录，包括隐藏文件和系统文件
dir c: /a:d           #显示 C 盘当前目录中的目录
dir c: /a:-d          #显示 C 盘根目录中的文件
dir c:\ <span>/b/p</span>          #/b只显示文件名，<span>/p</span><span>分页显示</span>
dir *.exe <span>/s</span>          #显示当前目录和子目录里所有的.exe文件
</pre>
																						<pre>4 cd</pre>
																						<pre>
																								<span>cd\                   #进入根目录</span>
cd                    #显示当前目录
<span>cd /d d:\sdk          #可以同时更改盘符和目录</span></pre>
																						<pre>5 md</pre>
																						<pre>
																								<span>md d:\a\b\c  </span>         #如果 d:\a 不存在，将会自动创建中级目录
#如果命令扩展名被停用，则需要键入 <span>mkdir \a\b\c。</span></pre>
																						<pre>6 rd</pre>
																						<pre>rd abc                #删除当前目录里的 abc 子目录，要求为空目录
rd <span>/s/q</span> d:\temp       #删除 d:\temp 文件夹及其子文件夹和文件，/q<span>安静模式</span></pre>
																						<pre>7 del</pre>
																						<pre>del d:\test.txt       #删除指定文件，不能是隐藏、系统、只读文件
del /q/a/f d:\temp\*.*
删除 d:\temp 文件夹里面的所有文件，包括隐藏、只读、系统文件，不包括子目录
del /q/a/f/s d:\temp\*.*
删除 d:\temp 及子文件夹里面的所有文件，包括隐藏、只读、系统文件，不包括子目录</pre>
																						<pre>8 ren
 
ren d:\temp tmp       #支持对文件夹的重命名</pre>
																						<pre>9 cls
清屏</pre>
																						<pre>10 type
显示文件内容</pre>
																						<pre>type c:\boot.ini      #显示指定文件的内容，程序文件一般会显示乱码
type *.txt            #显示当前目录里所有.txt文件的内容</pre>
																						<pre>11 copy
拷贝文件</pre>
																						<pre>copy c:\test.txt d:\test.bak
复制 c:\test.txt 文件到 d:\ ，并重命名为 test.bak</pre>
																						<pre>
																								<span>copy con test.txt</span>
从屏幕上等待输入，按 Ctrl+Z 结束输入，输入内容存为test.txt文件
<span>con代表屏幕，prn代表打印机，nul代表空设备</span></pre>
																						<pre>copy 1.txt + 2.txt 3.txt
合并 1.txt 和 2.txt 的内容，保存为 3.txt 文件
如果不指定 3.txt ，则保存到 1.txt</pre>
																						<pre>
																								<span>copy test.txt +
</span>复制文件到自己，实际上是修改了文件日期</pre>
																						<pre>12 title
设置cmd窗口的标题</pre>
																						<pre>title 新标题          #可以看到cmd窗口的标题栏变了</pre>
																						<pre>13 ver
显示系统版本</pre>
																						<pre>14 label 和 vol
设置卷标</pre>
																						<pre>vol                   #显示卷标
label                 #显示卷标，同时提示输入新卷标
label c:system        #设置C盘的卷标为 system</pre>
																						<pre>15 pause
暂停命令</pre>
																						<pre>16 <span>rem 和 ::</span>
注释命令</pre>
																						<pre>注释行不执行操作</pre>
																						<pre>17 date 和 time
日期和时间</pre>
																						<pre>date            #显示当前日期，并提示输入新日期，按\"回车\"略过输入
date/t          #只显示当前日期，不提示输入新日期
time            #显示当前时间，并提示输入新时间，按\"回车\"略过输入
time/t          #只显示当前时间，不提示输入新时间</pre>
																						<pre>
																								<span>18 goto 和 :
跳转命令</span>
																						</pre>
																						<pre>:label          #行首为:表示该行是标签行，标签行不执行操作
goto label      #跳转到指定的标签那一行</pre>
																						<pre>19 find (外部命令)
查找命令</pre>
																						<pre>find \"abc\" c:\test.txt
在 c:\test.txt 文件里查找含 abc 字符串的行
如果找不到，将设 errorlevel 返回码为1</pre>
																						<pre>find <span>/i</span> “abc” c:\test.txt
查找含 abc 的行，忽略大小写</pre>
																						<pre>find /c \"abc\" c:\test.txt
显示含 abc 的行的行数</pre>
																						<pre>20 more (外部命令)
逐屏显示</pre>
																						<pre>
																								<span>more c:\test.txt</span>      #逐屏显示 c:\test.txt 的文件内容</pre>
																						<pre>21 tree
显示目录结构</pre>
																						<pre>tree d:\              #显示D盘的文件目录结构</pre>
																						<pre>
																								<span>22 &amp;</span>
顺序执行多条命令，而不管命令是否执行成功</pre>
																						<pre>
																								<span>23 &amp;&amp;</span>
顺序执行多条命令，当碰到执行出错的命令后将不执行后面的命令</pre>
																						<pre>find \"ok\" c:\test.txt &amp;&amp; echo 成功
如果找到了\"ok\"字样，就显示\"成功\"，找不到就不显示</pre>
																						<pre>
																								<span>24 ||</span>
顺序执行多条命令，当碰到执行正确的命令后将不执行后面的命令</pre>
																						<pre>find \"ok\" c:\test.txt || echo 不成功
如果找不到\"ok\"字样，就显示\"不成功\"，找到了就不显示</pre>
																						<pre>25 |
管道命令</pre>
																						<pre>dir *.* /s/a | find /c \".exe\"
管道命令表示先执行 dir 命令，对其输出的结果执行后面的 find 命令
该命令行结果：输出当前文件夹及所有子文件夹里的.exe文件的个数</pre>
																						<pre>type c:\test.txt|more
这个和 more c:\test.txt 的效果是一样的</pre>
																						<pre>26 &gt; 和 &gt;&gt;
输出重定向命令</pre>
																						<pre>&gt; 清除文件中原有的内容后再写入
&gt;&gt; 追加内容到文件末尾，而不会清除原有的内容
主要将本来显示在屏幕上的内容输出到指定文件中</pre>
																						<pre>指定文件如果不存在，则自动生成该文件</pre>
																						<pre>type c:\test.txt &gt;prn
屏幕上不显示文件内容，转向输出到打印机</pre>
																						<pre>echo hello world&gt;con
在屏幕上显示hello world，实际上所有输出都是默认 &gt;con 的</pre>
																						<pre>copy c:\test.txt f: &gt;nul
拷贝文件，并且不显示\"文件复制成功\"的提示信息，但如果f盘不存在，还是会显示出错信息</pre>
																						<pre>
																								<span>copy c:\test.txt f: &gt;nul 2&gt;nul</span>
不显示”文件复制成功”的提示信息，并且f盘不存在的话，也不显示错误提示信息</pre>
																						<pre>echo ^^W ^&gt; ^W&gt;c:\test.txt
生成的文件内容为 ^W &gt; W
^ 和 &gt; 是控制命令，要把它们输出到文件，<span>必须在前面加个 ^ 符号</span></pre>
																						<pre>
																								<span>27 &lt;</span>
从文件中获得输入信息，而不是从屏幕上</pre>
																						<pre>一般用于 date time label 等需要等待输入的命令
@echo off
echo 2005-05-01&gt;temp.txt
date &lt;temp.txt
del temp.txt
这样就可以不等待输入直接修改当前日期</pre>
																						<pre>
																								<span>28 %0 %1 %2 %3 %4 %5 %6 %7 %8 %9 %*</span>
命令行传递给批处理的参数</pre>
																						<pre>%0 批处理文件本身
%1 第一个参数
%9 第九个参数
%* 从第一个参数开始的所有参数
 
批参数(%n)的替代已被增强。您可以使用以下语法:
 
      %~1           - 删除引号(\")，扩充 %1
      %~f1          - 将 %1 扩充到一个完全合格的路径名
      %~d1          - 仅将 %1 扩充到一个驱动器号
      %~p1          - 仅将 %1 扩充到一个路径
      %~n1          - 仅将 %1 扩充到一个文件名
      %~x1          - 仅将 %1 扩充到一个文件扩展名
      %~s1          - 扩充的路径指含有短名
      %~a1          - 将 %1 扩充到文件属性
      %~t1          - 将 %1 扩充到文件的日期/时间
      %~z1          - 将 %1 扩充到文件的大小
      %~$PATH : 1 - 查找列在 PATH 环境变量的目录，并将 %1
                    扩充到找到的第一个完全合格的名称。如果环境
                    变量名未被定义，或者没有找到文件，此组合键会
                    扩充到空字符串
 
可以组合修定符来取得多重结果:
 
     %~dp1         - 只将 %1 扩展到驱动器号和路径
     %~nx1         - 只将 %1 扩展到文件名和扩展名
     %~dp$PATH:1 - 在列在 PATH 环境变量中的目录里查找 %1，
                    并扩展到找到的第一个文件的驱动器号和路径。
     %~ftza1       - 将 %1 扩展到类似 DIR 的输出行。
</pre>
																						<pre>可以参照 call/? 或 for/? 看出每个参数的含意</pre>
																						<pre>echo load \"%%1\" \"%%2\"&gt;c:\test.txt
生成的文件内容为 load \"%1\" \"%2\"
批处理文件里，用这个格式把命令行参数输出到文件</pre>
																						<pre>29 if
判断命令</pre>
																						<pre>if \"%1\"==\"/a\" echo 第一个参数是/a</pre>
																						<pre>if /i \"%1\" equ \"/a\" echo 第一个参数是/a
/i 表示不区分大小写，equ 和 == 是一样的，其它运算符参见 if/?</pre>
																						<pre>if exist c:\test.bat echo 存在c:\test.bat文件</pre>
																						<pre>if not exist c:\windows (
      echo 不存在c:\windows文件夹
      )</pre>
																						<pre>if exist c:\test.bat (
      echo 存在c:\test.bat
      ) else (
      echo 不存在c:\test.bat
      )</pre>
																						<pre>
																								<span>30 setlocal 和 endlocal</span>
设置”命令扩展名”和”延缓环境变量扩充”</pre>
																						<pre>SETLOCAL ENABLEEXTENSIONS              #启用\"命令扩展名\"
SETLOCAL DISABLEEXTENSIONS             #停用\"命令扩展名\"
SETLOCAL ENABLEDELAYEDEXPANSION        #启用\"延缓环境变量扩充\"
SETLOCAL DISABLEDELAYEDEXPANSION       #停用\"延缓环境变量扩充\"
ENDLOCAL                               #恢复到使用SETLOCAL语句以前的状态</pre>
																						<pre>
																								<span>“命令扩展名”默认为启用
“延缓环境变量扩充”默认为停用</span>
批处理结束系统会自动恢复默认值</pre>
																						<pre>可以修改注册表以禁用\"命令扩展名\"，详见 cmd /? 。所以用到\"命令扩展名\"的程
序，建议在开头和结尾加上 SETLOCAL ENABLEEXTENSIONS 和 ENDLOCAL 语句，以确
保程序能在其它系统上正确运行</pre>
																						<pre>\"延缓环境变量扩充\"主要用于 if 和 for 的符合语句，在 set 的说明里有其实用例程</pre>
																						<pre>31 set
设置变量</pre>
																						<pre>引用变量可在变量名前后加 % ，即 %变量名%</pre>
																						<pre>set                      #显示目前所有可用的变量，包括系统变量和自定义的变量
echo %SystemDrive%       #显示系统盘盘符。系统变量可以直接引用
set p                    #显示所有以p开头的变量，要是一个也没有就设errorlevel=1
set p=aa1bb1aa2bb2       #设置变量p，并赋值为 = 后面的字符串，即aa1bb1aa2bb2
echo %p%                 #显示变量p代表的字符串，即aa1bb1aa2bb2
<span>echo %p:~6%</span>              #显示变量p中第6个字符以后的所有字符，即aa2bb2
echo %p:~6,3%            #显示第6个字符以后的3个字符，即aa2
echo %p:~0,3%            #显示前3个字符，即aa1
echo %p:~-2%             #显示最后面的2个字符，即b2
echo %p:~0,-2%           #显示除了最后2个字符以外的其它字符，即aa1bb1aa2b
echo %p:aa=c%            #用c替换变量p中所有的aa，即显示c1bb1c2bb2
echo %p:aa=%             #将变量p中的所有aa字符串置换为空，即显示1bb12bb2
echo %p:*bb=c%           #第一个bb及其之前的所有字符被替换为c，即显示c1aa2bb2
set p=%p:*bb=c%          #设置变量p，赋值为 %p:*bb=c% ，即c1aa2bb2
<span>set /a p=39</span>              #设置p为数值型变量，值为39
set /a p=39/10           #支持运算符，有小数时用去尾法，39/10=3.9，去尾得3，p=3
set /a p=p/10            #用 /a 参数时，在 = 后面的变量可以不加%直接引用
set /a p=”1&amp;0″           #”与”运算，要加引号。其它支持的运算符参见set/?
set p=                   #取消p变量</pre>
																						<pre>
																								<span>set /p p=请输入</span>
屏幕上显示”请输入”，并会将输入的字符串赋值给变量p
注意这条可以用来取代 choice 命令</pre>
																						<pre>注意变量在 if 和 for 的复合语句里是一次性全部替换的，如
@echo off
set p=aaa
if %p%==aaa (
      echo %p%
      set p=bbb
      echo %p%
      )
结果将显示
aaa
aaa
因为在读取 if 语句时已经将所有 %p% 替换为aaa
这里的\"替换\"，在 /? 帮助里就是指\"扩充\"、\"环境变量扩充\"
<span>可以启用”延缓环境变量扩充”，用 ! 来引用变量，即 !变量名!</span>
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set p=aaa
if %p%==aaa (
      echo %p%
      set p=bbb
      echo !p!
      )
ENDLOCAL
结果将显示
aaa
bbb</pre>
																						<pre>还有几个动态变量，运行 set 看不到
%CD%                    #代表当前目录的字符串
%DATE%                  #当前日期
%TIME%                  #当前时间
<span>%RANDOM%                #随机整数，介于0~32767</span>
%ERRORLEVEL%            #当前 ERRORLEVEL 值
%CMDEXTVERSION%         #当前命令处理器扩展名版本号
%CMDCMDLINE%            #调用命令处理器的原始命令行
可以用echo命令查看每个变量值，如 echo %time%
注意 %time% 精确到毫秒，在批处理需要延时处理时可以用到</pre>
																						<pre>32 start
批处理中调用外部程序的命令，否则等外部程序完成后才继续执行剩下的指令</pre>
																						<pre>33 call
批处理中调用另外一个批处理的命令，否则剩下的批处理指令将不会被执行
有时有的应用程序用start调用出错的，也可以call调用</pre>
																						<pre>34 choice (外部命令)
选择命令</pre>
																						<pre>让用户输入一个字符，从而选择运行不同的命令，返回码errorlevel为1234……
win98里是choice.com
win2000pro里没有，可以从win98里拷过来
win2003里是choice.exe</pre>
																						<pre>choice /N /C y /T 5 /D y&gt;nul
延时5秒</pre>
																						<pre>35 assoc 和 ftype
文件关联</pre>
																						<pre>assoc 设置'文件扩展名'关联，关联到'文件类型'
ftype 设置'文件类型'关联，关联到'执行程序和参数'</pre>
																						<pre>当你双击一个.txt文件时，windows并不是根据.txt直接判断用 notepad.exe 打开
而是先判断.txt属于 txtfile '文件类型'
再调用 txtfile 关联的命令行 txtfile=%SystemRoot%\system32\NOTEPAD.EXE %1</pre>
																						<pre>可以在\"文件夹选项\"→\"文件类型\"里修改这2种关联</pre>
																						<pre>assoc             #显示所有'文件扩展名'关联
assoc .txt        #显示.txt代表的'文件类型'，结果显示 .txt=txtfile
assoc .doc        #显示.doc代表的'文件类型'，结果显示 .doc=Word.Document.8
assoc .exe        #显示.exe代表的'文件类型'，结果显示 .exe=exefile
ftype             #显示所有'文件类型'关联
ftype exefile     #显示exefile类型关联的命令行，结果显示 exefile=\"%1\" %* </pre>
																						<pre>assoc .txt=Word.Document.8
设置.txt为word类型的文档，可以看到.txt文件的图标都变了</pre>
																						<pre>assoc .txt=txtfile
恢复.txt的正确关联
 
ftype exefile=\"%1\" %*
恢复 exefile 的正确关联
如果该关联已经被破坏，可以运行 command.com ，再输入这条命令</pre>
																						<pre>
																								<span>36 pushd 和 popd
切换当前目录</span>
																						</pre>
																						<pre>@echo off
c: &amp; cd\ &amp; md mp3         #在 C:\ 建立 mp3 文件夹
md d:\mp4                 #在 D:\ 建立 mp4 文件夹
cd /d d:\mp4              #更改当前目录为 d:\mp4
pushd c:\mp3              #保存当前目录，并切换当前目录为 c:\mp3
popd                      #恢复当前目录为刚才保存的 d:\mp4</pre>
																						<pre>
																								<span>37 for
循环命令</span>
																						</pre>
																						<pre>这个比较复杂，请对照 for/? 来看</pre>
																						<pre>for %%i in (c: d: e: f:) do echo %%i
依次调用小括号里的每个字符串，执行 do 后面的命令
注意%%i，在批处理中 for 语句调用参数用2个%
默认的字符串分隔符是\"空格键\"，\"Tab键\"，\"回车键\"</pre>
																						<pre>for %%i in (*.txt) do find \"abc\" %%i
对当前目录里所有的txt文件执行 find 命令</pre>
																						<pre>for /r . %%i in (*.txt) do find \"abc\" %%i
在当前目录和子目录里所有的.txt文件中搜索包含 abc 字符串的行</pre>
																						<pre>for /r . %%i in (.) do echo %%~pni
显示当前目录名和所有子目录名，包括路径，不包括盘符</pre>
																						<pre>for /r d:\mp3 %%i in (*.mp3) do echo %%i&gt;&gt;d:\mp3.txt
把 d:\mp3 及其子目录里的mp3文件的文件名都存到 d:\mp3.txt 里去</pre>
																						<pre>for /l %%i in (2,1,8) do echo %%i
生成2345678的一串数字，2是数字序列的开头，8是结尾，1表示每次加1</pre>
																						<pre>for /f %%i in ('set') do echo %%i
对 set 命令的输出结果循环调用，每行一个</pre>
																						<pre>for /f \"eol=P\" %%i in ('set') do echo %%i
取 set 命令的输出结果，忽略以 P 开头的那几行</pre>
																						<pre>for /f %%i in (d:\mp3.txt) do echo %%i
显示 d:\mp3.txt 里的每个文件名，每行一个，不支持带空格的名称</pre>
																						<pre>for /f \"delims=\" %%i in (d:\mp3.txt) do echo %%i
显示 d:\mp3.txt 里的每个文件名，每行一个，支持带空格的名称</pre>
																						<pre>for /f \"skip=5 tokens=4\" %%a in ('dir') do echo %%a
对 dir 命令的结果，跳过前面5行，余下的每行取第4列
每列之间的分隔符为默认的\"空格\"
可以注意到 dir 命令输出的前5行是没有文件名的</pre>
																						<pre>for /f \"tokens=1,2,3 delims=- \" %%a in ('date /t') do (
      echo %%a
      echo %%b
      echo %%c
      )
对 date /t 的输出结果，每行取1、2、3列
第一列对应指定的 %%a ，后面的 %%b 和 %%c 是派生出来的，对应其它列
分隔符指定为 - 和\"空格\"，注意 delims=- 后面有个\"空格\"
其中 tokens=1,2,3 若用 tokens=1-3 替换，效果是一样的</pre>
																						<pre>for /f \"tokens=2* delims=- \" %%a in ('date /t') do echo %%b
取第2列给 %%a ，其后的列都给 %%b</pre>
																						<pre>32 subst (外部命令)
映射磁盘。</pre>
																						<pre>subst z: <a>\server\d</a>       #这样输入z:就可以访问<a>\server\d</a>了
subst z: /d               #取消该映射
subst                     #显示目前所有的映时</pre>
																						<pre>38    xcopy (外部命令)
文件拷贝</pre>
																						<pre>xcopy d:\mp3 e:\mp3 /s/e/i/y
复制 d:\mp3 文件夹、所有子文件夹和文件到 e:\ ，覆盖已有文件
加 /i 表示如果 e:\ 没有 mp3 文件夹就自动新建一个，否则会有询问</pre>
																				</div>
																		</td>
																</tr>
														</tbody>
												</table>
										</div>
								</td>
						</tr>
				</tbody>
		</table>
<img src ="http://www.cppblog.com/birdshover/aggbug/68095.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/birdshover/" target="_blank">BirdsHover</a> 2008-11-28 18:22 <a href="http://www.cppblog.com/birdshover/archive/2008/11/28/68095.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Raw Socket 之HTTP，效果不理想</title><link>http://www.cppblog.com/birdshover/archive/2008/11/10/66526.html</link><dc:creator>BirdsHover</dc:creator><author>BirdsHover</author><pubDate>Mon, 10 Nov 2008 08:43:00 GMT</pubDate><guid>http://www.cppblog.com/birdshover/archive/2008/11/10/66526.html</guid><wfw:comment>http://www.cppblog.com/birdshover/comments/66526.html</wfw:comment><comments>http://www.cppblog.com/birdshover/archive/2008/11/10/66526.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/birdshover/comments/commentRss/66526.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/birdshover/services/trackbacks/66526.html</trackback:ping><description><![CDATA[包已经发出，但是伪造IP，无法实现3次握手，在服务器端，包将被丢弃。继续研究。<br /><br /><div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> stdafx.h : 标准系统包含文件的包含文件，<br /></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 或是经常使用但不常更改的<br /></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 特定于项目的包含文件<br /></span><span style="COLOR: #008000">//<br /></span><span style="COLOR: #000000"><br />#pragma once<br /><br />#include </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">targetver.h</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br /><br />#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br />#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">tchar.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br />#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"> <br /><br /></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000"> std;<br /><br />typedef unsigned </span><span style="COLOR: #0000ff">short</span><span style="COLOR: #000000"> USHORT;<br />typedef unsigned </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> UCHAR;<br /><br /></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> URG 0x20 </span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> ACK 0x10 </span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> PSH 0x08 </span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> RST 0x04 </span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> SYN 0x02 </span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> FIN 0x01 </span><span style="COLOR: #000000"><br /><br />typedef </span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000"> ip_hdr </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">定义IP首部 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">{ <br />    unsigned </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> h_verlen; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">4位首部长度,4位IP版本号 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    unsigned </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> tos; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">8位服务类型TOS </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    unsigned </span><span style="COLOR: #0000ff">short</span><span style="COLOR: #000000"> total_len; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">16位总长度（字节） </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    unsigned </span><span style="COLOR: #0000ff">short</span><span style="COLOR: #000000"> ident; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">16位标识 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    unsigned </span><span style="COLOR: #0000ff">short</span><span style="COLOR: #000000"> frag_and_flags; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">3位标志位 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    unsigned </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> ttl; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">8位生存时间 TTL </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    unsigned </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> proto; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">8位协议 (TCP, UDP 或其他) </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    unsigned </span><span style="COLOR: #0000ff">short</span><span style="COLOR: #000000"> checksum; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">16位IP首部校验和 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    unsigned </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> sourceIP; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">32位源IP地址 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    unsigned </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> destIP; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">32位目的IP地址 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">}IPHEADER; <br /><br />typedef </span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000"> tsd_hdr </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">定义TCP伪首部 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">{ <br />    unsigned </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> saddr; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">源地址 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    unsigned </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> daddr; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">目的地址 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> mbz; <br />    </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> ptcl; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">协议类型 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    unsigned </span><span style="COLOR: #0000ff">short</span><span style="COLOR: #000000"> tcpl; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">TCP长度 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">}PSDHEADER; <br /><br />typedef </span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000"> tcp_hdr </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">定义TCP首部 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">{ <br />    USHORT th_sport; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">16位源端口 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    USHORT th_dport; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">16位目的端口 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    unsigned </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> th_seq; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">32位序列号 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    unsigned </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> th_ack; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">32位确认号 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    unsigned </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> th_lenres; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">4位首部长度/6位保留字 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    unsigned </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> th_flag; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">6位标志位 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    USHORT th_win; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">16位窗口大小 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    USHORT th_sum; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">16位校验和 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    USHORT th_urp; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">16位紧急数据偏移量 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">}TCPHEADER; <br /><br /><br /><br /><br /><br /></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> TODO: 在此处引用程序需要的其他头文件<br /></span></div><br /><div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #000000">#include </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">StdAfx.h</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br />#include </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">HttpWeb.h</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br /><br />#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">winsock2.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"> <br />#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">ws2tcpip.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"> <br />#pragma comment(lib,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">ws2_32.lib</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">)  <br /></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> winsock_version 0x0101</span><span style="COLOR: #000000"><br /><br /></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">CheckSum:计算校验和的子函数 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">USHORT HttpWeb::checksum(USHORT </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">buffer, </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> size) <br />{ <br />    unsigned </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> cksum</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; <br />    </span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(size </span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">) <br />    { <br />        cksum </span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">buffer</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">; <br />        size </span><span style="COLOR: #000000">-=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(USHORT); <br />    } <br />    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(size) <br />    { <br />        cksum </span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(UCHAR</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)buffer; <br />    } <br /><br />    cksum </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> (cksum </span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">16</span><span style="COLOR: #000000">) </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> (cksum </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0xffff</span><span style="COLOR: #000000">); <br />    cksum </span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000"> (cksum </span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">16</span><span style="COLOR: #000000">); <br />    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> (USHORT)(</span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">cksum); <br />};<br /><br />HttpWeb::HttpWeb(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br />{<br />}<br /><br />HttpWeb::</span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">HttpWeb(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br />{<br />}<br /><br /></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> UseAge()<br />{<br />    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">********************************************************************\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); <br />    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Vote For yeshj\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); <br />    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\t Written by yurow\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); <br />    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\t Email: yurow@163.com\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); <br />    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\t Blog: birdshover.cnblogs.com\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); <br />    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Useage: sniffer ip to vote\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); <br />    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">********************************************************************\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); <br />}<br /><br /></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000"> HttpWeb::Post(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> host[], </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> path[], </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> parms[],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> port)<br />{<br />    UseAge();<br /><br />    WSADATA WSAData; <br />    SOCKET sock;<br />    SOCKADDR_IN addr_in; <br />    IPHEADER ipHeader; <br />    TCPHEADER tcpHeader; <br />    PSDHEADER psdHeader; <br /><br />    </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> szSendBuf[</span><span style="COLOR: #000000">4000</span><span style="COLOR: #000000">] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> {</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">}; <br />    BOOL flag;<br />    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> rect,nTimeOver; <br />    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (WSAStartup(MAKEWORD(</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">), </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">WSAData)</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">) <br />    {<br />        printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">WSAStartup Error!\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); <br />        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">; <br />    }<br /><br />    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> ((sock </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> WSASocket(AF_INET,SOCK_RAW,IPPROTO_RAW,NULL,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,WSA_FLAG_OVERLAPPED)) </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> INVALID_SOCKET) <br />    {<br />        printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Socket Setup Error!\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); <br />        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">; <br />    }<br /><br />    flag</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">; <br />    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (setsockopt(sock,IPPROTO_IP, IP_HDRINCL,(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">flag,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(flag)) </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> SOCKET_ERROR) <br />    { <br />        printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">setsockopt IP_HDRINCL error!\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); <br />        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">; <br />    } <br /><br />    nTimeOver</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1000</span><span style="COLOR: #000000">; <br />    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">nTimeOver, </span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(nTimeOver)) </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> SOCKET_ERROR) <br />    { <br />        printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">setsockopt SO_SNDTIMEO error!\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); <br />        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">; <br />    } <br /><br />    </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> requestString[</span><span style="COLOR: #000000">4000</span><span style="COLOR: #000000">] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> {</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">};<br />    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(strlen(parms) </span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)<br />        strcat(requestString,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">POST </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />    </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br />        strcat(requestString,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">GET </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />    strcat(requestString,path);<br />    strcat(requestString,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> HTTP/1.1\r\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />    strcat(requestString,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Host: </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />    strcat(requestString,host);<br />    strcat(requestString,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\r\nAccept: */*\r\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />    strcat(requestString,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Accept-Language: zh-cn\r\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />    strcat(requestString,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; Trident/4.0;)\r\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />    strcat(requestString,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Accept-Encoding: gzip, deflate\r\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />    strcat(requestString,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Connection: Keep-Alive\r\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />    <br />    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(strlen(parms) </span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">){<br />        strcat(requestString,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Content-Type: application/x-www-form-urlencoded\r\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />        strcat(requestString,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Content-Length: </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />        </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> p </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">[</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">];<br />        itoa(strlen(parms),p,</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">);   <br />        strcat(requestString,p);<br />        strcat(requestString,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\r\n\r\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />        strcat(requestString,parms);<br />        strcat(requestString,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\r\n\r\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />    }<br />    cout </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> requestString;<br /><br />    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> postDataLength </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> strlen(requestString);<br />    LPHOSTENT lphostent </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> gethostbyname(host);<br />    <br />    <br />    addr_in.sin_family </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> AF_INET; <br />    addr_in.sin_port </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> htons(port); <br />    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">addr_in.sin_addr= *((LPIN_ADDR)*lphostent-&gt;h_addr_list); </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    addr_in.sin_addr </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(in_addr </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)lphostent</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">h_addr_list[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]; <br />    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">填充IP首部 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    ipHeader.h_verlen</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">( </span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">|</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(ipHeader)</span><span style="COLOR: #000000">/</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(unsigned </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">)); <br />    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">ipHeader.tos=0; </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    ipHeader.total_len </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> htons(</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(ipHeader) </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(tcpHeader)); <br />    ipHeader.ident</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">; <br />    ipHeader.frag_and_flags</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; <br />    ipHeader.ttl </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">128</span><span style="COLOR: #000000">; <br />    ipHeader.proto </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> IPPROTO_TCP; <br />    ipHeader.checksum </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; <br />    ipHeader.sourceIP </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> inet_addr(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">192.168.19.1</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); <br />    ipHeader.destIP </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> addr_in.sin_addr.S_un.S_addr;<br /><br />    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">填充TCP首部 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    tcpHeader.th_dport </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> htons(port);  </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">目标端口</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    tcpHeader.th_sport</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">htons(</span><span style="COLOR: #000000">4000</span><span style="COLOR: #000000">); </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">源端口号 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    tcpHeader.th_seq </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> htonl(</span><span style="COLOR: #000000">0x12345678</span><span style="COLOR: #000000">); <br />    tcpHeader.th_ack</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; <br />    tcpHeader.th_lenres </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> (</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(tcpHeader) </span><span style="COLOR: #000000">/</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">4</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">|</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">); <br />    tcpHeader.th_flag </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">修改这里来实现不同的标志位探测，2是SYN，1是FIN，16是ACK探测 等等 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    tcpHeader.th_win</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> htons(</span><span style="COLOR: #000000">512</span><span style="COLOR: #000000">); <br />    tcpHeader.th_urp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; <br />    tcpHeader.th_sum</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; <br /><br />    psdHeader.saddr </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> ipHeader.sourceIP; <br />    psdHeader.daddr </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> ipHeader.destIP; <br />    psdHeader.mbz </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; <br />    psdHeader.ptcl </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> IPPROTO_TCP; <br />    psdHeader.tcpl </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> htons(</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(tcpHeader)); <br /><br /><br />    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">计算校验和 </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    memcpy(szSendBuf, </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">psdHeader, </span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(psdHeader)); <br />    memcpy(szSendBuf</span><span style="COLOR: #000000">+</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(psdHeader), </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">tcpHeader, </span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(tcpHeader)); <br />    tcpHeader.th_sum</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">checksum((USHORT </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)szSendBuf,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(psdHeader)</span><span style="COLOR: #000000">+</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(tcpHeader)); <br /><br />    memcpy(szSendBuf, </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">ipHeader, </span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(ipHeader)); <br />    memcpy(szSendBuf</span><span style="COLOR: #000000">+</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(ipHeader), </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">tcpHeader, </span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(tcpHeader)); <br />    memset(szSendBuf</span><span style="COLOR: #000000">+</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(ipHeader)</span><span style="COLOR: #000000">+</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(tcpHeader), </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">); <br />    ipHeader.checksum</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">checksum((USHORT </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)szSendBuf, </span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(ipHeader)</span><span style="COLOR: #000000">+</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(tcpHeader)); <br /><br />    memcpy(szSendBuf, </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">ipHeader, </span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(ipHeader)); <br /><br />    memcpy(szSendBuf </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(ipHeader)</span><span style="COLOR: #000000">+</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(tcpHeader),requestString,postDataLength);<br /><br />    cout </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> szSendBuf </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> endl;<br /><br />    rect</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sendto(sock, szSendBuf, </span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(ipHeader)</span><span style="COLOR: #000000">+</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(tcpHeader) </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> postDataLength, </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">, (</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000"> sockaddr</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">addr_in, </span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(addr_in)); <br />    <br />    cout </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> rect </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\t</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(ipHeader) </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(tcpHeader) </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> postDataLength </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> endl;<br />    <br />    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (rect </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> SOCKET_ERROR </span><span style="COLOR: #000000">||</span><span style="COLOR: #000000"> rect </span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">10000L</span><span style="COLOR: #000000">) <br />    { <br />        printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">send error!:%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,WSAGetLastError()); <br />        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">; <br />    } <br />    </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"> <br />        printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">send ok!\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); <br /><br />    closesocket(sock); <br />    WSACleanup(); <br /><br />    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br />}<br /></span></div><br /><img src ="http://www.cppblog.com/birdshover/aggbug/66526.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/birdshover/" target="_blank">BirdsHover</a> 2008-11-10 16:43 <a href="http://www.cppblog.com/birdshover/archive/2008/11/10/66526.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>TCP/IP 头分析</title><link>http://www.cppblog.com/birdshover/archive/2008/11/07/66224.html</link><dc:creator>BirdsHover</dc:creator><author>BirdsHover</author><pubDate>Fri, 07 Nov 2008 09:37:00 GMT</pubDate><guid>http://www.cppblog.com/birdshover/archive/2008/11/07/66224.html</guid><wfw:comment>http://www.cppblog.com/birdshover/comments/66224.html</wfw:comment><comments>http://www.cppblog.com/birdshover/archive/2008/11/07/66224.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/birdshover/comments/commentRss/66224.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/birdshover/services/trackbacks/66224.html</trackback:ping><description><![CDATA[一次向baidu发送的http请求<br /><br /><b>IP头<br /><br /></b>45 00 00 28 00 f6 40 00 40 06 84 46 c0 a8 12 23 dc b5 06 13<br /><br />45 —— 4代表IP4版本，50代表IP头长度20；<br />00 —— 8位0代表服务类型，定义了数据传输的优先级、延迟、吞吐量和可靠性等特性<br /><blockquote><blockquote><blockquote><blockquote>8个BIT的含义是： <br /><br />000 前三位不用 <br /><br />0 表示最小时延，如Telnet服务使用该位 <br /><br />0 
表示吞吐量，如FTP服务使用该位 <br /><br />0 表示可靠性，如SNMP服务使用该位 <br /><br />0 表示最小代价 <br /><br />0 不用 <br /></blockquote></blockquote></blockquote></blockquote>00 28 —— 总长度40字节<br />00 f6 —— IP包标识，主机使用它唯一确定每个发送的数据报 <br />40 00 —— 表示片标志，占3个BIT。各位含义分别为：第一个“0”不用，第二个“0”为分片标志位，“1”表示分片，“0”表示不分版本。第三个0为是否最后一片标志位，0表示最后一片，1表示还有更多的片。 
表示片偏移为0个Bytes。该部分占13个BIT。 <br />40 —— 表示生存时间TTL值为40,该部分占8个BIT。 <br />06 —— 6（TCP），表示协议类型为TCP，协议代码是6。如果是UDP协议，则此处的协议代码应为17。如果是ICMP协议，则此处的协议代码应为1。该部分占8个BIT。 
<br /><br />84 46 —— 表示IP包头校验和为，括号内的Correct表示此IP数据包是正确的，没有被非法修改过。该部分占16个BIT，用十六进制表示。 <br /><br />c0 a8 12 23 —— 源IP地址<br />dc b5 06 13 —— 指向IP地址<br /><br /><b>TCP头信息</b><br />04 0f 00 50 3e e1 60 31 ee 34 89 49 50 10 20 00 bf 50 00 00<br /><br />04 0f —— 端口 1039<br />00 50 —— 80端口<br />3e e1 60 31 —— 表示初始连接的请求号，即SEQ值。该部分占32个BIT，值从1到2的32次方减1。<br />              —— 对方应答也应该是这个值<br />ee 34 89 49 —— 响应码<br />50 —— 数据偏移<br />10 —— 标记 ，该值用两个十六进制数来表示。该部分长度为6个BIT，6个标志位的含义分别是：<br /><blockquote>0 URG，紧急数据标志，为1表示有紧急数据，应立即进行传递。 <br /><br />0 ACK，确认标志位，为1表示此数据包为应答数据包 <br /><br />0 
PSH，PUSH标志位，为1表示此数据包应立即进行传递。 <br /><br />0 RST：复位标志位。如果收到不属于本机的数据包，则返回一个RST 
<br /><br />0 SYN：连接请求标志位。为1表示为发起连接的请求数据包。 <br /><br />0 FIN：结束连接请求标志位。为1表示是结束连接的请求数据包。  <br /></blockquote>20 00 —— 表示窗口，该部分占16个BIT。 <br />bf 50 —— 表示校验和。该部分占16个BIT，用十六进制表示。<br />00 00 —— 表示紧急指针为0。该部分占16个BIT。 <br /><br />DLC的头和结尾在raw socket中不用设置。<br /><br />Http报文问题。<br /><br />在一次HTTP请求中，在 紧急指针 结束后，跟了 608 字节的Http Header<br /><br />47 45 54 20 2f 69 6d 67 2f 62 61 69 64 75 5f 6c 6f 67 6f 2e 67 69 66 20 48 54 54 50 2f 31 2e 31 0d 0a 41 63 63 65 70 74 3a 20 2a 2f 2a 0d 0a 52 65 66 65 72 65 72 3a 20 68 74 74 70 3a 2f 2f 77 77 77 2e 62 61 69 64 75 2e 63 6f 6d 2f 0d 0a 41 63 63 65 70 74 2d 4c 61 6e 67 75 61 67 65 3a 20 7a 68 2d 63 6e 0d 0a 55 73 65 72 2d 41 67 65 6e 74 3a 20 4d 6f 7a 69 6c 6c 61 2f 34 2e 30 20 28 63 6f 6d 70 61 74 69 62 6c 65 3b 20 4d 53 49 45 20 38 2e 30 3b 20 57 69 6e 64 6f 77 73 20 4e 54 20 35 2e 32 3b 20 54 72 69 64 65 6e 74 2f 34 2e 30 3b 20 2e 4e 45 54 20 43 4c 52 20 31 2e 31 2e 34 33 32 32 3b 20 2e 4e 45 54 20 43 4c 52 20 32 2e 30 2e 35 30 37 32 37 3b 20 2e 4e 45 54 20 43 4c 52 20 33 2e 30 2e 30 34 35 30 36 2e 36 34 38 3b 20 2e 4e 45 54 20 43 4c 52 20 33 2e 35 2e 32 31 30 32 32 3b 20 2e 4e 45 54 20 43 4c 52 20 33 2e 30 2e 34 35 30 36 2e 32 31 35 32 3b 20 2e 4e 45 54 20 43 4c 52 20 33 2e 35 2e 33 30 37 32 39 29 0d 0a 55 41 2d 43 50 55 3a 20 78 38 36 0d 0a 41 63 63 65 70 74 2d 45 6e 63 6f 64 69 6e 67 3a 20 67 7a 69 70 2c 20 64 65 66 6c 61 74 65 0d 0a 49 66 2d 4d 6f 64 69 66 69 65 64 2d 53 69 6e 63 65 3a 20 57 65 64 2c 20 33 30 20 4a 75 6c 20 32 30 30 38 20 31 30 3a 32 33 3a 30 30 20 47 4d 54 0d 0a 49 66 2d 4e 6f 6e 65 2d 4d 61 74 63 68 3a 20 22 35 64 31 2d 34 38 39 30 34 31 30 34 22 0d 0a 48 6f 73 74 3a 20 77 77 77 2e 62 61 69 64 75 2e 63 6f 6d 0d 0a 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 4b 65 65 70 2d 41 6c 69 76 65 0d 0a 43 6f 6f 6b 69 65 3a 20 42 41 49 44 55 49 44 3d 38 45 38 34 31 39 36 35 32 43 32 38 43 34 31 44 37 41 35 44 34 42 42 32 35 32 34 36 31 33 32 32 3a 46 47 3d 31 3b 20 42 44 53 54 41 54 3d 36 62 39 64 65 66 35 36 38 32 37 61 38 35 34 33 33 30 34 65 32 35 31 66 39 35 63 61 64 31 63 38 61 37 38 36 63 39 31 37 37 66 33 65 36 37 30 39 63 39 33 64 37 30 63 66 33 61 63 37 39 33 33 64 0d 0a 0d 0a<br /><br />接下来要试试 Raw Socket发送HTTP报文了。<br /><br /><br /><img src ="http://www.cppblog.com/birdshover/aggbug/66224.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/birdshover/" target="_blank">BirdsHover</a> 2008-11-07 17:37 <a href="http://www.cppblog.com/birdshover/archive/2008/11/07/66224.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>搞不清楚状况，Stack around the variable 'dest' was corrupted.</title><link>http://www.cppblog.com/birdshover/archive/2008/11/05/66043.html</link><dc:creator>BirdsHover</dc:creator><author>BirdsHover</author><pubDate>Wed, 05 Nov 2008 08:55:00 GMT</pubDate><guid>http://www.cppblog.com/birdshover/archive/2008/11/05/66043.html</guid><wfw:comment>http://www.cppblog.com/birdshover/comments/66043.html</wfw:comment><comments>http://www.cppblog.com/birdshover/archive/2008/11/05/66043.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/birdshover/comments/commentRss/66043.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/birdshover/services/trackbacks/66043.html</trackback:ping><description><![CDATA[就是一个POST的提交，结构搞晕我了。<br /><br />搞不清楚为什么GET方式没效果，而POST有效果，却提示“Run-Time Check Failure #2 - Stack around the variable 'dest' was corrupted.”<br />好晕倒啊！！老衲没办法了，毕竟对com不熟。记下状况以后来研究研究。<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Visitor.h</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">stdio.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">winsock.h</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> <br />#pragma comment(lib,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">ws2_32.lib</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)  <br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> winsock_version 0x0101</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);">  std;<br /><br />#include </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">URL.h</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br /><br />Visitor::Visitor(</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">)<br />{<br />}<br /><br />Visitor::</span><span style="color: rgb(0, 0, 0);">~</span><span style="color: rgb(0, 0, 0);">Visitor(</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">)<br />{<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> Visitor::POST(</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> host,</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> pathAndQuery, </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> parms)<br />{<br />    </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> requestString </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);">[</span><span style="color: rgb(0, 0, 0);">4000</span><span style="color: rgb(0, 0, 0);">];<br />    memset(requestString,</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">4000</span><span style="color: rgb(0, 0, 0);">);<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(strlen(parms) </span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)<br />        strcat(requestString,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">POST </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />    </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br />        strcat(requestString,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">GET </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />    strcat(requestString,pathAndQuery);<br />    strcat(requestString,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> HTTP/1.1\r\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />    strcat(requestString,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Host: </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />    strcat(requestString,host);<br />    <br />    strcat(requestString,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">\r\nAccept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />    strcat(requestString,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Accept-Language: zh-cn,zh;q=0.5\r\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />    strcat(requestString,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648;\r\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />    strcat(requestString,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">UA-CPU: x86\r\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />    strcat(requestString,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7\r\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">strcat(requestString,"Accept-Encoding: gzip, deflate\r\n");</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    strcat(requestString,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Connection: Keep-Alive\r\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />    <br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(strlen(parms) </span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">){<br />        strcat(requestString,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Content-Type: application/x-www-form-urlencoded\r\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        strcat(requestString,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Content-Length: </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> p </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);">[</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">];<br />        itoa(strlen(parms),p,</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">);   <br />        strcat(requestString,p);<br />        strcat(requestString,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">\r\n\r\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        strcat(requestString,parms);<br />    }<br />    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">cout &lt;&lt; requestString;</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);"><br />    WSADATA wsadata;<br />    LPHOSTENT lphostent; <br />    SOCKET hsocket;<br />    SOCKADDR_IN saServer;<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> nRet;<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(WSAStartup(winsock_version,</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">wsadata))<br />    {<br />        <br />    }<br />    lphostent </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> gethostbyname(host);<br />    hsocket </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);<br />    saServer.sin_family </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> AF_INET;<br />    saServer.sin_port </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> htons(</span><span style="color: rgb(0, 0, 0);">80</span><span style="color: rgb(0, 0, 0);">);<br />    saServer.sin_addr </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">((LPIN_ADDR)</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">lphostent</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">h_addr_list);<br />    nRet </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> connect(hsocket, (LPSOCKADDR)</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">saServer, </span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(SOCKADDR_IN));<br />    nRet </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> send(hsocket, requestString, strlen(requestString), </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">); <br />    </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> dest[</span><span style="color: rgb(0, 0, 0);">1000</span><span style="color: rgb(0, 0, 0);">];<br />    nRet</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;   <br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> bytes </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />    </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(nRet</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)  <br />    {<br />        nRet </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> recv(hsocket,(LPSTR)dest,</span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(dest),</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">);<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(nRet </span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)   <br />            dest[nRet] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;   <br />        </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">{<br />            </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">dest[0] = 0;  </span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />        }<br />        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">printf("\nReceived   bytes:%d\n",nRet);   </span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);">{<br />        cout </span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);"> dest;<br />        }</span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);">(exception ex){}<br />        bytes </span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);"> nRet;<br />    }<br />    closesocket(hsocket);<br />    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">delete []dest;</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">\nReceived   bytes:%d\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,bytes);   <br />    delete requestString;<br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">""</span><span style="color: rgb(0, 0, 0);">;<br />}<br /></span></div><br /><img src ="http://www.cppblog.com/birdshover/aggbug/66043.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/birdshover/" target="_blank">BirdsHover</a> 2008-11-05 16:55 <a href="http://www.cppblog.com/birdshover/archive/2008/11/05/66043.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>