<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[linux C 根据域名获取http响应报文，C语言HTTP请求GET。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Tue, 26 Oct 2010 05:28:32 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	今天没事写了个小demo, 根据域名获取http响应报文, 程序也不是很完善<br/>其实主要是要练习一下非阻塞I/O处理.<br/><br/>上代码:<br/><br/>/*<br/>* Author: looyao<br/>*/<br/><br/>#include &lt;stdio.h&gt;<br/>#include &lt;sys/socket.h&gt;<br/>#include &lt;arpa/inet.h&gt;<br/>#include &lt;netdb.h&gt;<br/>#include &lt;fcntl.h&gt;<br/>#include &lt;stdlib.h&gt;<br/>#include &lt;errno.h&gt;<br/>#include &lt;unistd.h&gt;<br/>#include &lt;string.h&gt;<br/>#include &lt;signal.h&gt;<br/><br/>static int set_fl(int fd, int flags)<br/>&#123;<br/>int val;<br/><br/>if ((val = fcntl(fd,F_GETFL,0)) &lt; 0) &#123;<br/>printf(&quot;fcntl F_GETFL error&#92;n&quot;);<br/>return -1;<br/>&#125;<br/><br/>val &#124;= flags;<br/><br/>if (fcntl(fd,F_SETFL,val) &lt; 0) &#123;<br/>printf(&quot;fcntl F_SETFL error&#92;n&quot;);<br/>return -1;<br/>&#125;<br/><br/>return 0;<br/>&#125;<br/><br/>void timeout()<br/>&#123;<br/>printf(&quot;time out&#92;n&quot;);<br/>exit(0);<br/>&#125;<br/><br/>int main(int argc, char **argv)<br/>&#123;<br/>int sockfd;<br/>struct sockaddr_in servaddr;<br/>struct hostent *host;<br/>char *msg = strdup(&quot;GET / HTTP/1.1&#92;r&#92;nHOST: &quot;);<br/>char rbuf[32], res_header[1024];<br/>char *host_ip, *ptr, *pos;<br/>int ntowrite, nwrite, nread, nread_res_header = 0, content_length, nread_content_length = 0;<br/><br/>signal(SIGALRM, timeout);<br/><br/>if (argc &lt; 2) &#123;<br/>printf(&quot;请输入域名参数&#92;n&quot;);<br/>exit(0);<br/>&#125; else if (argc &gt; 2) &#123;<br/>printf(&quot;输入的参数过多&#92;n&quot;);<br/>exit(0);<br/>&#125;<br/><br/>msg = realloc(msg, strlen(msg) + strlen(argv[1]) + sizeof(&quot;&#92;r&#92;n&#92;r&#92;n&quot;));<br/>sprintf(msg + strlen(msg), &quot;%s&#92;r&#92;n&#92;r&#92;n&quot;, argv[1]);<br/><br/>sockfd = socket(AF_INET, SOCK_STREAM, 0);<br/>if (sockfd &lt; 0) &#123;<br/>perror(&quot;socket&quot;);<br/>exit(0);<br/>&#125;<br/><br/>host = gethostbyname(argv[1]);<br/>if (host == NULL) &#123;<br/>printf(&quot;无法获取此域名IP&#92;n&quot;);<br/>goto END;<br/>&#125;<br/><br/>host_ip = inet_ntoa(*((struct in_addr *)host-&gt;h_addr));<br/><br/>bzero(&amp;servaddr, sizeof(servaddr));<br/>servaddr.sin_family = AF_INET;<br/>servaddr.sin_port = htons(80);<br/>inet_pton(AF_INET, host_ip, &amp;servaddr.sin_addr);<br/>if( connect(sockfd, (struct sockaddr *) &amp;servaddr, sizeof(servaddr)) &lt; 0) &#123;<br/>perror(&quot;connect&quot;);<br/>exit(0);<br/>&#125;<br/><br/>if (set_fl(sockfd, O_NONBLOCK) &lt; 0) &#123;<br/>printf(&quot;set nonblock error&quot;);<br/>goto END;<br/>&#125;<br/><br/>ntowrite = strlen(msg);<br/>ptr = msg;<br/>alarm(10);<br/>while (ntowrite &gt; 0) &#123;<br/>nwrite = write(sockfd, ptr, ntowrite);<br/>if (nwrite &lt;= 0) &#123;<br/>perror(&quot;write:&quot;);<br/>goto END;<br/>&#125;<br/>ptr += nwrite;<br/>ntowrite -= nwrite;<br/>&#125;<br/>alarm(0);<br/><br/>/* read response header */<br/>ptr = res_header;<br/>alarm(10);<br/>while (1) &#123;<br/>errno = 0;<br/>nread = read(sockfd, ptr, sizeof(res_header) - nread_res_header);<br/>if(nread &lt; 0)&#123;<br/>if(errno == EAGAIN)&#123;<br/>continue;<br/>&#125;<br/>perror(&quot;read&quot;);<br/>goto END;<br/>&#125;<br/>nread_res_header += nread;<br/>ptr += nread;<br/>if((pos = strstr(res_header, &quot;&#92;r&#92;n&#92;r&#92;n&quot;)) != NULL &#124;&#124; nread_res_header == sizeof(res_header))&#123;<br/>break;<br/>&#125;<br/>&#125;<br/>alarm(0);<br/><br/>if (!(pos = strstr(res_header, &quot;Content-Length:&quot;))) &#123;<br/>printf(&quot;响应报文错误&quot;);<br/>goto END;<br/>&#125; else &#123;<br/>pos += 16;<br/>if (!(ptr = strstr(pos, &quot;&#92;r&#92;n&quot;))) &#123;<br/>printf(&quot;响应报文错误&quot;);<br/>goto END;<br/>&#125;<br/>char buf[10];<br/>snprintf(buf, (ptr - pos) &gt; 10 ? 10 : (ptr - pos) + 1, &quot;%s&quot;, pos);<br/>content_length = atoi(buf);<br/>&#125;<br/><br/>write(STDOUT_FILENO, res_header, nread_res_header);<br/>if ((pos = strstr(ptr, &quot;&#92;r&#92;n&#92;r&#92;n&quot;))) &#123;<br/>pos += 4;<br/>nread_content_length = nread_res_header - (pos - res_header);<br/>&#125; else &#123;<br/>/* 此处有bug, 如果响应头很长, 没有读到&#92;r&#92;n&#92;r&#92;n, 但是, 先这样吧 :D */<br/>goto END;<br/>&#125;<br/><br/>alarm(10);<br/>while (1) &#123;<br/>errno = 0;<br/>memset(rbuf, 0, sizeof(rbuf));<br/>nread = read(sockfd, rbuf, sizeof(rbuf));<br/>if (nread &lt; 0) &#123;<br/>if (errno == EAGAIN) &#123;<br/>continue;<br/>&#125;<br/>perror(&quot;read&quot;);<br/>goto END;<br/>&#125;<br/>write(STDOUT_FILENO, rbuf, nread);<br/>nread_content_length += nread;<br/>if (nread_content_length &gt;= content_length)&#123;<br/>break;<br/>&#125;<br/>&#125;<br/>alarm(0);<br/><br/>END:<br/>close(sockfd);<br/>free(msg);<br/>return 0;<br/>&#125;<br/><br/><br/>编译后运行 ./demo www.baidu.com<br/>响应的报文如下:<br/>HTTP/1.1 200 OK<br/>Date: Thu, 29 Apr 2010 08:57:48 GMT<br/>Server: BWS/1.0<br/>Content-Length: 3521<br/>Content-Type: text/html;charset=gb2312<br/>Cache-Control: private<br/>Expires: Thu, 29 Apr 2010 08:57:48 GMT<br/>Set-Cookie: BAIDUID=4573491A3ED9E0B16CA37586CFB9460E:FG=1; expires=Thu, 29-Apr-40 08:57:48 GMT; path=/; domain=.baidu.com<br/>P3P: CP=&quot; OTI DSP COR IVA OUR IND COM &quot;<br/><br/>&lt;!doctype html&gt;&lt;html&gt;&lt;head&gt;&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html;charset=gb2312&quot;&gt;&lt;title&gt;百度一下，你就知道&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/title&gt;&lt;style&gt;body&#123;font:12px arial;text-align:center;background:#fff&#125;body,p,form&#123;margin:0;padding:0&#125;body,form,#lg&#123;position:relative&#125;td&#123;text-align:left&#125;img&#123;border:0&#125;a&#123;color:#00c&#125;a:active&#123;color:#f60&#125;#u&#123;padding:7px 10px 3px 0;text-align:right&#125;#m&#123;width:650px;margin:0 auto&#125;#nv&#123;font-size:16px;margin:0 0 4px -32px&#125;#nv a,#nv b,#su,#lk&#123;font-size:14px&#125;#lg&#123;margin:-17px 0 9px&#125;#fm&#123;padding-left:111px;text-align:left&#125;#kw&#123;width:391px;line-height:16px;padding:3px 1px;margin:0 6px 0 0;font:16px arial&#125;#su&#123;width:78px;height:28px;line-height:24px&#125;#kw,#su&#123;vertical-align:middle&#125;#lk&#123;margin:33px 0&#125;#lk span&#123;font:14px &quot;宋体&quot;&#125;#lm&#123;height:60px&#125;#lh&#123;margin:16px 0 5px;font:12px &quot;宋体&quot;&#125;#lh a&#123;font:12px arial&#125;#hp&#123;position:absolute;line-height:14px;margin:0 0 0 6px;top:-1px;*top:2px&#125;#cp,#cp a&#123;color:#77c&#125;&lt;/style&gt;&lt;/head&gt;<br/>&lt;body&gt;&lt;p id=&quot;u&quot;&gt;&lt;a href=&quot;http://passport.baidu.com/?login&amp;tpl=mn&quot;&gt;登录&lt;/a&gt;&lt;/p&gt;&lt;div id=&quot;m&quot;&gt;&lt;p id=&quot;lg&quot;&gt;&lt;img src=&quot;http://www.baidu.com/img/baidu_logo.gif&quot; width=&quot;270&quot; height=&quot;129&quot; usemap=&quot;#mp&quot;&gt;&lt;/p&gt;&lt;p id=&quot;nv&quot;&gt;&lt;a href=&quot;http://news.baidu.com&quot;&gt;新&amp;nbsp;闻&lt;/a&gt;　&lt;b&gt;网&amp;nbsp;页&lt;/b&gt;　&lt;a href=&quot;http://tieba.baidu.com&quot;&gt;贴&amp;nbsp;吧&lt;/a&gt;　&lt;a href=&quot;http://zhidao.baidu.com&quot;&gt;知&amp;nbsp;道&lt;/a&gt;　&lt;a href=&quot;http://mp3.baidu.com&quot;&gt;MP3&lt;/a&gt;　&lt;a href=&quot;http://image.baidu.com&quot;&gt;图&amp;nbsp;片&lt;/a&gt;　&lt;a href=&quot;http://video.baidu.com&quot;&gt;视&amp;nbsp;频&lt;/a&gt;　&lt;a href=&quot;http://map.baidu.com&quot;&gt;地&amp;nbsp;图&lt;/a&gt;&lt;/p&gt;&lt;div id=&quot;fm&quot;&gt;&lt;form name=&quot;f&quot; action=&quot;s&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;wd&quot; id=&quot;kw&quot; maxlength=&quot;100&quot;&gt;&lt;input type=&quot;submit&quot; value=&quot;百度一下&quot; id=&quot;su&quot;&gt;&lt;span id=&quot;hp&quot;&gt;&lt;a href=&quot;/gaoji/preferences.html&quot;&gt;设置&lt;/a&gt;&lt;br&gt;&lt;a href=&quot;/gaoji/advanced.html&quot;&gt;高级&lt;/a&gt;&lt;/span&gt;&lt;/form&gt;&lt;/div&gt;<br/>&lt;p id=&quot;lk&quot;&gt;&lt;a href=&quot;http://hi.baidu.com&quot;&gt;空间&lt;/a&gt;　&lt;a href=&quot;http://baike.baidu.com&quot;&gt;百科&lt;/a&gt;　&lt;a href=&quot;http://www.hao123.com&quot;&gt;hao123&lt;/a&gt;&lt;span&gt; &#124; &lt;a href=&quot;/more/&quot;&gt;更多&amp;gt;&amp;gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p id=&quot;lm&quot;&gt;&lt;/p&gt;&lt;p&gt;&lt;a id=&quot;st&quot; onClick=&quot;this.style.behavior=&#039;url(#default#homepage)&#039;;this.setHomePage(&#039;http://www.baidu.com&#039;)&quot; href=&quot;http://utility.baidu.com/traf/click.php?id=215&amp;url=http://www.baidu.com&quot;&gt;把百度设为主页&lt;/a&gt;&lt;/p&gt;&lt;p id=&quot;lh&quot;&gt;&lt;a href=&quot;http://e.baidu.com/?refer=888&quot;&gt;加入百度推广&lt;/a&gt; &#124; &lt;a href=&quot;http://top.baidu.com&quot;&gt;搜索风云榜&lt;/a&gt; &#124; &lt;a href=&quot;http://home.baidu.com&quot;&gt;关于百度&lt;/a&gt; &#124; &lt;a href=&quot;http://ir.baidu.com&quot;&gt;About Baidu&lt;/a&gt;&lt;/p&gt;&lt;p id=&quot;cp&quot;&gt;&amp;copy;2010 Baidu &lt;a href=&quot;/duty/&quot;&gt;使用百度前必读&lt;/a&gt; &lt;a href=&quot;http://www.miibeian.gov.cn&quot; target=&quot;_blank&quot;&gt;京ICP证030173号&lt;/a&gt; &lt;img src=&quot;http://gimg.baidu.com/img/gs.gif&quot;&gt;&lt;/p&gt;&lt;/div&gt;&lt;map name=&quot;mp&quot;&gt;&lt;area shape=&quot;rect&quot; coords=&quot;43,22,227,91&quot; href=&quot;http://hi.baidu.com/baidu/&quot; target=&quot;_blank&quot; title=&quot;点此进入 百度的空间&quot;&gt;&lt;/map&gt;&lt;/body&gt;<br/>&lt;script&gt;var w=window,d=document,n=navigator,k=d.f.wd,a=d.getElementById(&quot;nv&quot;).getElementsByTagName(&quot;a&quot;);if(n.userAgent.indexOf(&quot;MSIE&quot;)==-1&#124;&#124;window.opera)&#123;d.getElementById(&quot;st&quot;).style.display=&quot;none&quot;&#125;;for(var i=0;i&lt;a.length;i++)&#123;a[i].onclick=function()&#123;if(k.value.length&gt;0)&#123;var o=this,h=o.href,q=encodeURIComponent(k.value);if(h.indexOf(&quot;q=&quot;)!=-1)&#123;o.href=h.replace(/q=[^&amp;$]*/,&quot;q=&quot;+q)&#125;else&#123;this.href+=&quot;?q=&quot;+q&#125;&#125;&#125;&#125;;(function()&#123;if(/q=([^&amp;]+)/.test(location.search))&#123;k.value=decodeURIComponent(RegExp.$1)&#125;&#125;)();if(n.cookieEnabled&amp;&amp;!/sug?=0/.test(d.cookie))&#123;d.write(&#039;&lt;script src=http://www.baidu.com/js/bdsug.js?v=1.0.3.0&gt;&lt;&#92;/script&gt;&#039;)&#125;;if(w.attachEvent)&#123;w.attachEvent(&quot;onload&quot;,function()&#123;k.focus();&#125;)&#125;else&#123;w.addEventListener(&#039;load&#039;,function()&#123;k.focus()&#125;,true)&#125;;w.onunload=function()&#123;&#125;&lt;/script&gt;&lt;/html&gt;&lt;!--ae75535d4a224623--&gt;<br/><br/><br/>非阻塞要不断的轮询, 浪费了CPU时间, 但是说与使用select机制相比, 到底那个机制更快, <br/>也需要看情况, 我会继续试验.<br/>对于read的结束条件, 具体看通信的协议.<br/>http协议主要是看Content-Length, 还有一种是Transfer-Encoding: chunked,<br/>我这个小demo实现的是根据Content-Length判断read结束.<br/>over, 未完待续...<br/>来源：http://hi.baidu.com/teng0210/blog/item/4955e8946e992b47d0135e2a.html<br/><br/><br/>C语言HTTP请求GET，因对《所谓黑客揭秘》，大家都攻击代码的要求。所以我开始了为期两周的C/C++学习。由于没有任何C/C++基础，而是临时突击，所以代码肯定漏洞甚多，请观者见谅。同时欢迎拍砖或者支出代码错误，以求大家共同进步。：<br/><br/><textarea name="code" class="C" rows="15" cols="100">
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;winsock.h&gt;
#include &lt;stdlib.h&gt;

void DownLoad(char *url)&#123;
&nbsp;&nbsp;WSADATA WsaData;
&nbsp;&nbsp;SOCKET socketfd;
&nbsp;&nbsp;SOCKADDR_IN addr;
&nbsp;&nbsp;HOSTENT *pURL;
&nbsp;&nbsp;int i=0;
&nbsp;&nbsp;char myurl[BUFSIZ];
&nbsp;&nbsp;char *pHost =0,* pGet =0;
&nbsp;&nbsp;char host[BUFSIZ],GET[BUFSIZ];
&nbsp;&nbsp;char Header[BUFSIZ];
&nbsp;&nbsp;FILE * pf;

&nbsp;&nbsp;static char text[BUFSIZ];

&nbsp;&nbsp;if (WSAStartup(MAKEWORD(2,2),&amp;WsaData))
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;The socket failed&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;return;
&nbsp;&nbsp;&#125;
&nbsp;&nbsp;strcpy(myurl,url);

&nbsp;&nbsp;for (i=0,pHost= myurl;*pHost!=&#039;/&#039;&amp;&amp;*pHost!=&#039;/0&#039;;pHost++,i++)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;host[i] = *pHost;
&nbsp;&nbsp;&#125;
&nbsp;&nbsp;host[i]=&#039;/0&#039;;
&nbsp;&nbsp;strcpy(GET,pHost);
&nbsp;&nbsp;strnset(Header,&#039;/0&#039;,BUFSIZ);


&nbsp;&nbsp;socketfd = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
&nbsp;&nbsp;pURL = gethostbyname(host);

&nbsp;&nbsp;addr.sin_addr.s_addr = *((unsigned long *)pURL-&gt;h_addr);
&nbsp;&nbsp;addr.sin_family = AF_INET;
&nbsp;&nbsp;addr.sin_port = htons(80);

&nbsp;&nbsp;

&nbsp;&nbsp;strcat(Header,&quot;GET / &quot;);
&nbsp;&nbsp;strcat(Header,GET);
&nbsp;&nbsp;strcat(Header,&quot;HTTP/1.1/r/n&quot;);
&nbsp;&nbsp;strcat(Header,&quot;Accept: */*/r/n&quot;);
&nbsp;&nbsp;strcat(Header,&quot;Accept-Language: zh-cn/r/n&quot;);
&nbsp;&nbsp;strcat(Header,&quot;User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.2)/r/n&quot;);
&nbsp;&nbsp;//strcat(Header,&quot;Accept-Encoding: gzip, deflate/r/n&quot;);
&nbsp;&nbsp;strcat(Header,&quot;Host: &quot;);
&nbsp;&nbsp;strcat(Header,host);
&nbsp;&nbsp;strcat(Header,&quot;/r/nConnection: Keep-Alive/r/n&quot;);
&nbsp;&nbsp;
&nbsp;&nbsp;strcat(Header,&quot;/r/n/r/n&quot;);
&nbsp;&nbsp;
&nbsp;&nbsp;connect(socketfd,(SOCKADDR *)&amp;addr,sizeof(addr));
&nbsp;&nbsp;send(socketfd,Header,strlen(Header),0);
&nbsp;&nbsp;printf(&quot;%s&quot;,Header);
&nbsp;&nbsp;pf = fopen(&quot;1.txt&quot;,&quot;w&quot;);
&nbsp;&nbsp;while(recv(socketfd,text,BUFSIZ,0)&gt;0)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;%s&quot;,text);
&nbsp;&nbsp;&nbsp;&nbsp;fputs(text,pf);
&nbsp;&nbsp;&nbsp;&nbsp;strnset(text,&#039;/0&#039;,BUFSIZ);
&nbsp;&nbsp;&#125;
&nbsp;&nbsp;fclose(pf);
&nbsp;&nbsp;closesocket(socketfd);
&nbsp;&nbsp;WSACleanup();

&nbsp;&nbsp;
&#125;
int main()&#123;
&nbsp;&nbsp;char a[256];
&nbsp;&nbsp;printf(&quot;http://&quot;);
&nbsp;&nbsp;scanf(&quot;%s&quot;,a);
&nbsp;&nbsp;DownLoad(a);
&#125;
</textarea><br/><br/>来自：http://blog.csdn.net/aofengdaxia/article/details/5984431
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] linux C 根据域名获取http响应报文，C语言HTTP请求GET。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>