webench是运行在linux操作系统下的一款web压力测试工具,其源码也只有几百行。本文在webbench 1.5版本的基础,对源码进行了详细的注释。
webbench.c
<span style="font-size:14px;"><code class="language-c hljs has-numbering"><span class="hljs-preprocessor">#include "socket.c"</span> <span class="hljs-preprocessor">#include <unistd.h></span> <span class="hljs-preprocessor">#include <sys/param.h></span> <span class="hljs-preprocessor">#include <rpc/types.h></span> <span class="hljs-preprocessor">#include <getopt.h></span> <span class="hljs-preprocessor">#include <strings.h></span> <span class="hljs-preprocessor">#include <time.h></span> <span class="hljs-preprocessor">#include <signal.h></span> <span class="hljs-comment">/* values */</span> <span class="hljs-keyword">volatile</span> <span class="hljs-keyword">int</span> timerexpired=<span class="hljs-number">0</span>; <span class="hljs-keyword">int</span> speed=<span class="hljs-number">0</span>; <span class="hljs-keyword">int</span> failed=<span class="hljs-number">0</span>; <span class="hljs-keyword">int</span> bytes=<span class="hljs-number">0</span>; <span class="hljs-comment">/* globals */</span> <span class="hljs-keyword">int</span> http10=<span class="hljs-number">1</span>; <span class="hljs-comment">/* 0 - http/0.9, 1 - http/1.0, 2 - http/1.1 */</span> <span class="hljs-comment">/* Allow: GET, HEAD, OPTIONS, TRACE */</span> <span class="hljs-preprocessor">#define METHOD_GET 0</span> <span class="hljs-preprocessor">#define METHOD_HEAD 1</span> <span class="hljs-preprocessor">#define METHOD_OPTIONS 2</span> <span class="hljs-preprocessor">#define METHOD_TRACE 3</span> <span class="hljs-preprocessor">#define PROGRAM_VERSION "1.5"</span> <span class="hljs-keyword">int</span> method=METHOD_GET; <span class="hljs-keyword">int</span> clients=<span class="hljs-number">1</span>; <span class="hljs-keyword">int</span> force=<span class="hljs-number">0</span>; <span class="hljs-keyword">int</span> force_reload=<span class="hljs-number">0</span>; <span class="hljs-keyword">int</span> proxyport=<span class="hljs-number">80</span>; <span class="hljs-keyword">char</span> *proxyhost=NULL; <span class="hljs-keyword">int</span> benchtime=<span class="hljs-number">30</span>; <span class="hljs-comment">/* internal */</span> <span class="hljs-keyword">int</span> mypipe[<span class="hljs-number">2</span>]; <span class="hljs-keyword">char</span> host[MAXHOSTNAMELEN]; <span class="hljs-preprocessor">#define REQUEST_SIZE 2048</span> <span class="hljs-keyword">char</span> request[REQUEST_SIZE]; <span class="hljs-keyword">static</span> <span class="hljs-keyword">const</span> <span class="hljs-keyword">struct</span> option long_options[]= { {<span class="hljs-string">"force"</span>,no_argument,&force,<span class="hljs-number">1</span>}, {<span class="hljs-string">"reload"</span>,no_argument,&force_reload,<span class="hljs-number">1</span>}, {<span class="hljs-string">"time"</span>,required_argument,NULL,<span class="hljs-string">'t'</span>}, {<span class="hljs-string">"help"</span>,no_argument,NULL,<span class="hljs-string">'?'</span>}, {<span class="hljs-string">"http09"</span>,no_argument,NULL,<span class="hljs-string">'9'</span>}, {<span class="hljs-string">"http10"</span>,no_argument,NULL,<span class="hljs-string">'1'</span>}, {<span class="hljs-string">"http11"</span>,no_argument,NULL,<span class="hljs-string">'2'</span>}, {<span class="hljs-string">"get"</span>,no_argument,&method,METHOD_GET}, {<span class="hljs-string">"head"</span>,no_argument,&method,METHOD_HEAD}, {<span class="hljs-string">"options"</span>,no_argument,&method,METHOD_OPTIONS}, {<span class="hljs-string">"trace"</span>,no_argument,&method,METHOD_TRACE}, {<span class="hljs-string">"version"</span>,no_argument,NULL,<span class="hljs-string">'V'</span>}, {<span class="hljs-string">"proxy"</span>,required_argument,NULL,<span class="hljs-string">'p'</span>}, {<span class="hljs-string">"clients"</span>,required_argument,NULL,<span class="hljs-string">'c'</span>}, {NULL,<span class="hljs-number">0</span>,NULL,<span class="hljs-number">0</span>} }; <span class="hljs-comment">/* prototypes */</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> benchcore(<span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span>* host,<span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span> port, <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *request); <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> bench(<span class="hljs-keyword">void</span>); <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> build_request(<span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *url); <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> alarm_handler(<span class="hljs-keyword">int</span> signal) { timerexpired=<span class="hljs-number">1</span>; } <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> usage(<span class="hljs-keyword">void</span>) { <span class="hljs-built_in">fprintf</span>(stderr, <span class="hljs-string">"webbench [option]... URL\n"</span> <span class="hljs-string">" -f|--force Don't wait for reply from server.\n"</span> <span class="hljs-string">" -r|--reload Send reload request - Pragma: no-cache.\n"</span> <span class="hljs-string">" -t|--time <sec> Run benchmark for <sec> seconds. Default 30.\n"</span> <span class="hljs-string">" -p|--proxy <server:port> Use proxy server for request.\n"</span> <span class="hljs-string">" -c|--clients <n> Run <n> HTTP clients at once. Default one.\n"</span> <span class="hljs-string">" -9|--http09 Use HTTP/0.9 style requests.\n"</span> <span class="hljs-string">" -1|--http10 Use HTTP/1.0 protocol.\n"</span> <span class="hljs-string">" -2|--http11 Use HTTP/1.1 protocol.\n"</span> <span class="hljs-string">" --get Use GET request method.\n"</span> <span class="hljs-string">" --head Use HEAD request method.\n"</span> <span class="hljs-string">" --options Use OPTIONS request method.\n"</span> <span class="hljs-string">" --trace Use TRACE request method.\n"</span> <span class="hljs-string">" -?|-h|--help This information.\n"</span> <span class="hljs-string">" -V|--version Display program version.\n"</span> ); }; <span class="hljs-keyword">int</span> main(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">char</span> *argv[]) { <span class="hljs-keyword">int</span> opt=<span class="hljs-number">0</span>; <span class="hljs-keyword">int</span> options_index=<span class="hljs-number">0</span>; <span class="hljs-keyword">char</span> *tmp=NULL; <span class="hljs-keyword">if</span>(argc==<span class="hljs-number">1</span>) { usage(); <span class="hljs-keyword">return</span> <span class="hljs-number">2</span>; } <span class="hljs-comment">//解析出所有的命令行参数</span> <span class="hljs-keyword">while</span>((opt=getopt_long(argc,argv,<span class="hljs-string">"912Vfrt:p:c:?h"</span>,long_options,&options_index))!=EOF ) { <span class="hljs-keyword">switch</span>(opt) { <span class="hljs-keyword">case</span> <span class="hljs-number">0</span> : <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">'f'</span>: force=<span class="hljs-number">1</span>;<span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">'r'</span>: force_reload=<span class="hljs-number">1</span>;<span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">'9'</span>: http10=<span class="hljs-number">0</span>;<span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">'1'</span>: http10=<span class="hljs-number">1</span>;<span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">'2'</span>: http10=<span class="hljs-number">2</span>;<span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">'V'</span>: <span class="hljs-built_in">printf</span>(PROGRAM_VERSION<span class="hljs-string">"\n"</span>);<span class="hljs-built_in">exit</span>(<span class="hljs-number">0</span>); <span class="hljs-keyword">case</span> <span class="hljs-string">'t'</span>: benchtime=atoi(optarg);<span class="hljs-keyword">break</span>;<span class="hljs-comment">//一个客户端运行时间 </span> <span class="hljs-keyword">case</span> <span class="hljs-string">'p'</span>: <span class="hljs-comment">/* proxy server parsing server:port */</span> tmp=<span class="hljs-built_in">strrchr</span>(optarg,<span class="hljs-string">':'</span>);<span class="hljs-comment">//使用代理服务器</span> proxyhost=optarg; <span class="hljs-keyword">if</span>(tmp==NULL) { <span class="hljs-keyword">break</span>; } <span class="hljs-keyword">if</span>(tmp==optarg) { <span class="hljs-built_in">fprintf</span>(stderr,<span class="hljs-string">"Error in option --proxy %s: Missing hostname.\n"</span>,optarg); <span class="hljs-keyword">return</span> <span class="hljs-number">2</span>; } <span class="hljs-keyword">if</span>(tmp==optarg+<span class="hljs-built_in">strlen</span>(optarg)-<span class="hljs-number">1</span>) { <span class="hljs-built_in">fprintf</span>(stderr,<span class="hljs-string">"Error in option --proxy %s Port number is missing.\n"</span>,optarg); <span class="hljs-keyword">return</span> <span class="hljs-number">2</span>; } *tmp=<span class="hljs-string">'\0'</span>; proxyport=atoi(tmp+<span class="hljs-number">1</span>);<span class="hljs-keyword">break</span>;<span class="hljs-comment">//代理的端口</span> <span class="hljs-keyword">case</span> <span class="hljs-string">':'</span>: <span class="hljs-keyword">case</span> <span class="hljs-string">'h'</span>: <span class="hljs-keyword">case</span> <span class="hljs-string">'?'</span>: usage();<span class="hljs-keyword">return</span> <span class="hljs-number">2</span>;<span class="hljs-keyword">break</span>;<span class="hljs-comment">//打印出应用程序使用帮助信息</span> <span class="hljs-keyword">case</span> <span class="hljs-string">'c'</span>: clients=atoi(optarg);<span class="hljs-keyword">break</span>;<span class="hljs-comment">//模拟多少个客户端同时进行请求</span> } } <span class="hljs-comment">//参数中没有URL</span> <span class="hljs-keyword">if</span>(optind==argc) { <span class="hljs-built_in">fprintf</span>(stderr,<span class="hljs-string">"webbench: Missing URL!\n"</span>); usage(); <span class="hljs-keyword">return</span> <span class="hljs-number">2</span>; } <span class="hljs-keyword">if</span>(clients==<span class="hljs-number">0</span>) clients=<span class="hljs-number">1</span>; <span class="hljs-keyword">if</span>(benchtime==<span class="hljs-number">0</span>) benchtime=<span class="hljs-number">60</span>; <span class="hljs-comment">/* Copyright */</span> <span class="hljs-built_in">fprintf</span>(stderr,<span class="hljs-string">"Webbench - Simple Web Benchmark "</span>PROGRAM_VERSION<span class="hljs-string">"\n"</span> <span class="hljs-string">"Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.\n"</span> ); <span class="hljs-comment">/** 生成http请求的字符串 ***/</span> build_request(argv[optind]); <span class="hljs-comment">/* print bench info */</span> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"\nBenchmarking: "</span>); <span class="hljs-keyword">switch</span>(method) { <span class="hljs-keyword">case</span> METHOD_GET: <span class="hljs-keyword">default</span>: <span class="hljs-built_in">printf</span>(<span class="hljs-string">"GET"</span>);<span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> METHOD_OPTIONS: <span class="hljs-built_in">printf</span>(<span class="hljs-string">"OPTIONS"</span>);<span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> METHOD_HEAD: <span class="hljs-built_in">printf</span>(<span class="hljs-string">"HEAD"</span>);<span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> METHOD_TRACE: <span class="hljs-built_in">printf</span>(<span class="hljs-string">"TRACE"</span>);<span class="hljs-keyword">break</span>; } <span class="hljs-built_in">printf</span>(<span class="hljs-string">" %s"</span>,argv[optind]); <span class="hljs-keyword">switch</span>(http10) { <span class="hljs-keyword">case</span> <span class="hljs-number">0</span>: <span class="hljs-built_in">printf</span>(<span class="hljs-string">" (using HTTP/0.9)"</span>);<span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-number">2</span>: <span class="hljs-built_in">printf</span>(<span class="hljs-string">" (using HTTP/1.1)"</span>);<span class="hljs-keyword">break</span>; } <span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n"</span>); <span class="hljs-keyword">if</span>(clients==<span class="hljs-number">1</span>) <span class="hljs-built_in">printf</span>(<span class="hljs-string">"1 client"</span>); <span class="hljs-keyword">else</span> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d clients"</span>,clients); <span class="hljs-built_in">printf</span>(<span class="hljs-string">", running %d sec"</span>, benchtime); <span class="hljs-keyword">if</span>(force) <span class="hljs-built_in">printf</span>(<span class="hljs-string">", early socket close"</span>); <span class="hljs-keyword">if</span>(proxyhost!=NULL) <span class="hljs-built_in">printf</span>(<span class="hljs-string">", via proxy server %s:%d"</span>,proxyhost,proxyport); <span class="hljs-keyword">if</span>(force_reload) <span class="hljs-built_in">printf</span>(<span class="hljs-string">", forcing reload"</span>); <span class="hljs-built_in">printf</span>(<span class="hljs-string">".\n"</span>); <span class="hljs-keyword">return</span> bench(); } <span class="hljs-keyword">void</span> build_request(<span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *url) { <span class="hljs-keyword">char</span> tmp[<span class="hljs-number">10</span>]; <span class="hljs-keyword">int</span> i; bzero(host,MAXHOSTNAMELEN); bzero(request,REQUEST_SIZE); <span class="hljs-keyword">if</span>(force_reload && proxyhost!=NULL && http10<<span class="hljs-number">1</span>) http10=<span class="hljs-number">1</span>; <span class="hljs-keyword">if</span>(method==METHOD_HEAD && http10<<span class="hljs-number">1</span>) http10=<span class="hljs-number">1</span>; <span class="hljs-keyword">if</span>(method==METHOD_OPTIONS && http10<<span class="hljs-number">2</span>) http10=<span class="hljs-number">2</span>; <span class="hljs-keyword">if</span>(method==METHOD_TRACE && http10<<span class="hljs-number">2</span>) http10=<span class="hljs-number">2</span>; <span class="hljs-comment">/** 设置请求的方法 **/</span> <span class="hljs-keyword">switch</span>(method) { <span class="hljs-keyword">default</span>: <span class="hljs-keyword">case</span> METHOD_GET: <span class="hljs-built_in">strcpy</span>(request,<span class="hljs-string">"GET"</span>);<span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> METHOD_HEAD: <span class="hljs-built_in">strcpy</span>(request,<span class="hljs-string">"HEAD"</span>);<span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> METHOD_OPTIONS: <span class="hljs-built_in">strcpy</span>(request,<span class="hljs-string">"OPTIONS"</span>);<span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> METHOD_TRACE: <span class="hljs-built_in">strcpy</span>(request,<span class="hljs-string">"TRACE"</span>);<span class="hljs-keyword">break</span>; } <span class="hljs-built_in">strcat</span>(request,<span class="hljs-string">" "</span>); <span class="hljs-comment">//找到http://或https://中的://</span> <span class="hljs-keyword">if</span>(NULL==<span class="hljs-built_in">strstr</span>(url,<span class="hljs-string">"://"</span>))<span class="hljs-comment">//如果没有则说明请求的URL不合法</span> { <span class="hljs-built_in">fprintf</span>(stderr, <span class="hljs-string">"\n%s: is not a valid URL.\n"</span>,url); <span class="hljs-built_in">exit</span>(<span class="hljs-number">2</span>); } <span class="hljs-keyword">if</span>(<span class="hljs-built_in">strlen</span>(url)><span class="hljs-number">1500</span>)<span class="hljs-comment">//请求的url字符串太长。</span> { <span class="hljs-built_in">fprintf</span>(stderr,<span class="hljs-string">"URL is too long.\n"</span>); <span class="hljs-built_in">exit</span>(<span class="hljs-number">2</span>); } <span class="hljs-keyword">if</span>(proxyhost==NULL) <span class="hljs-keyword">if</span> (<span class="hljs-number">0</span>!=strncasecmp(<span class="hljs-string">"http://"</span>,url,<span class="hljs-number">7</span>)) <span class="hljs-comment">//除了开头的http://如果还有http://报错URL不合法</span> { <span class="hljs-built_in">fprintf</span>(stderr,<span class="hljs-string">"\nOnly HTTP protocol is directly supported, set --proxy for others.\n"</span>); <span class="hljs-built_in">exit</span>(<span class="hljs-number">2</span>); } <span class="hljs-comment">/* protocol/host delimiter */</span> i=<span class="hljs-built_in">strstr</span>(url,<span class="hljs-string">"://"</span>)-url+<span class="hljs-number">3</span>;<span class="hljs-comment">//找到://后面第一个字符在URL字符串所在下标值,也就是域名或ip地址的第一个字符</span> <span class="hljs-comment">/* printf("%d\n",i); */</span> <span class="hljs-keyword">if</span>(<span class="hljs-built_in">strchr</span>(url+i,<span class="hljs-string">'/'</span>)==NULL) {<span class="hljs-comment">//://后面没有/不正确的URL</span> <span class="hljs-built_in">fprintf</span>(stderr,<span class="hljs-string">"\nInvalid URL syntax - hostname don't ends with '/'.\n"</span>); <span class="hljs-built_in">exit</span>(<span class="hljs-number">2</span>); } <span class="hljs-keyword">if</span>(proxyhost==NULL) { <span class="hljs-comment">//URL地址中带有端口号</span> <span class="hljs-keyword">if</span>(index(url+i,<span class="hljs-string">':'</span>)!=NULL && index(url+i,<span class="hljs-string">':'</span>)<index(url+i,<span class="hljs-string">'/'</span>)) { <span class="hljs-comment">//解析出域名或IP地址,放到host中。</span> <span class="hljs-built_in">strncpy</span>(host,url+i,<span class="hljs-built_in">strchr</span>(url+i,<span class="hljs-string">':'</span>)-url-i); bzero(tmp,<span class="hljs-number">10</span>); <span class="hljs-comment">//解析出URL中的端口</span> <span class="hljs-built_in">strncpy</span>(tmp,index(url+i,<span class="hljs-string">':'</span>)+<span class="hljs-number">1</span>,<span class="hljs-built_in">strchr</span>(url+i,<span class="hljs-string">'/'</span>)-index(url+i,<span class="hljs-string">':'</span>)-<span class="hljs-number">1</span>); proxyport=atoi(tmp); <span class="hljs-keyword">if</span>(proxyport==<span class="hljs-number">0</span>) proxyport=<span class="hljs-number">80</span>; } <span class="hljs-keyword">else</span> { <span class="hljs-comment">//URL中没有端口,直接解析出主机域名或IP地址放到host中。</span> <span class="hljs-built_in">strncpy</span>(host,url+i,<span class="hljs-built_in">strcspn</span>(url+i,<span class="hljs-string">"/"</span>)); } <span class="hljs-comment">//将要请求的URI放到GET或HEAE等后面</span> <span class="hljs-built_in">strcat</span>(request+<span class="hljs-built_in">strlen</span>(request),url+i+<span class="hljs-built_in">strcspn</span>(url+i,<span class="hljs-string">"/"</span>)); } <span class="hljs-keyword">else</span> { <span class="hljs-comment">//使用代理,直接将整个输入的URL都做为get或head后面的参数</span> <span class="hljs-built_in">strcat</span>(request,url); } <span class="hljs-keyword">if</span>(http10==<span class="hljs-number">1</span>) <span class="hljs-built_in">strcat</span>(request,<span class="hljs-string">" HTTP/1.0"</span>); <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (http10==<span class="hljs-number">2</span>) <span class="hljs-built_in">strcat</span>(request,<span class="hljs-string">" HTTP/1.1"</span>); <span class="hljs-built_in">strcat</span>(request,<span class="hljs-string">"\r\n"</span>); <span class="hljs-keyword">if</span>(http10><span class="hljs-number">0</span>) <span class="hljs-built_in">strcat</span>(request,<span class="hljs-string">"User-Agent: WebBench "</span>PROGRAM_VERSION<span class="hljs-string">"\r\n"</span>); <span class="hljs-keyword">if</span>(proxyhost==NULL && http10><span class="hljs-number">0</span>) { <span class="hljs-built_in">strcat</span>(request,<span class="hljs-string">"Host: "</span>); <span class="hljs-built_in">strcat</span>(request,host);<span class="hljs-comment">//不使用代理,在http请求中需要设置Host</span> <span class="hljs-built_in">strcat</span>(request,<span class="hljs-string">"\r\n"</span>); } <span class="hljs-keyword">if</span>(force_reload && proxyhost!=NULL) { <span class="hljs-built_in">strcat</span>(request,<span class="hljs-string">"Pragma: no-cache\r\n"</span>); } <span class="hljs-keyword">if</span>(http10><span class="hljs-number">1</span>) <span class="hljs-built_in">strcat</span>(request,<span class="hljs-string">"Connection: close\r\n"</span>);<span class="hljs-comment">//不使用长连接</span> <span class="hljs-comment">//http语法,http请求头结束需要一个空行\r\n</span> <span class="hljs-keyword">if</span>(http10><span class="hljs-number">0</span>) <span class="hljs-built_in">strcat</span>(request,<span class="hljs-string">"\r\n"</span>); <span class="hljs-comment">// printf("Req=%s\n",request);</span> } <span class="hljs-comment">/* vraci system rc error kod */</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> bench(<span class="hljs-keyword">void</span>) { <span class="hljs-keyword">int</span> i,j,k; pid_t pid=<span class="hljs-number">0</span>; FILE *f; <span class="hljs-comment">/* check avaibility of target server */</span> <span class="hljs-comment">//创建一个连接到服务器的socket标识</span> i=Socket(proxyhost==NULL?host:proxyhost,proxyport); <span class="hljs-keyword">if</span>(i<<span class="hljs-number">0</span>) { <span class="hljs-built_in">fprintf</span>(stderr,<span class="hljs-string">"\nConnect to server failed. Aborting benchmark.\n"</span>); <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>; } close(i); <span class="hljs-comment">/* create pipe */</span> <span class="hljs-keyword">if</span>(pipe(mypipe))<span class="hljs-comment">//创建管道,mypipe[0]为管道里的读取端,mypipe[1]为管道里的写入端</span> { perror(<span class="hljs-string">"pipe failed."</span>); <span class="hljs-keyword">return</span> <span class="hljs-number">3</span>; } <span class="hljs-comment">/* fork childs */</span> <span class="hljs-keyword">for</span>(i=<span class="hljs-number">0</span>;i<clients;i++) { pid=fork();<span class="hljs-comment">//使用fork创建clients个进程,用于模拟clients个客户端用于请求</span> <span class="hljs-keyword">if</span>(pid <= (pid_t) <span class="hljs-number">0</span>) { <span class="hljs-comment">/* child process or error*/</span> sleep(<span class="hljs-number">1</span>); <span class="hljs-comment">/* make childs faster */</span> <span class="hljs-keyword">break</span>;<span class="hljs-comment">//如果返回的是子进程,或者子进程创建失败,退出循环,否则子进程会再创建进程。</span> } } <span class="hljs-keyword">if</span>( pid< (pid_t) <span class="hljs-number">0</span>) { <span class="hljs-built_in">fprintf</span>(stderr,<span class="hljs-string">"problems forking worker no. %d\n"</span>,i); perror(<span class="hljs-string">"fork failed."</span>); <span class="hljs-keyword">return</span> <span class="hljs-number">3</span>; } <span class="hljs-keyword">if</span>(pid== (pid_t) <span class="hljs-number">0</span>) { <span class="hljs-comment">//pid=0是子进程,进行http请求</span> <span class="hljs-keyword">if</span>(proxyhost==NULL) benchcore(host,proxyport,request); <span class="hljs-keyword">else</span> benchcore(proxyhost,proxyport,request); <span class="hljs-comment">/* write results to pipe */</span> f=fdopen(mypipe[<span class="hljs-number">1</span>],<span class="hljs-string">"w"</span>);<span class="hljs-comment">//mypipe[1]进行写入</span> <span class="hljs-keyword">if</span>(f==NULL) { perror(<span class="hljs-string">"open pipe for writing failed."</span>); <span class="hljs-keyword">return</span> <span class="hljs-number">3</span>; } <span class="hljs-comment">/* fprintf(stderr,"Child - %d %d\n",speed,failed); */</span> <span class="hljs-built_in">fprintf</span>(f,<span class="hljs-string">"%d %d %d\n"</span>,speed,failed,bytes);<span class="hljs-comment">//向管道里写成功数,失败数,即服务器返回的字节数</span> fclose(f); <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; } <span class="hljs-keyword">else</span> { <span class="hljs-comment">//返回到父进程中</span> f=fdopen(mypipe[<span class="hljs-number">0</span>],<span class="hljs-string">"r"</span>);<span class="hljs-comment">//mypipe[0]用于读取</span> <span class="hljs-keyword">if</span>(f==NULL) { perror(<span class="hljs-string">"open pipe for reading failed."</span>); <span class="hljs-keyword">return</span> <span class="hljs-number">3</span>; } setvbuf(f,NULL,_IONBF,<span class="hljs-number">0</span>); speed=<span class="hljs-number">0</span>; failed=<span class="hljs-number">0</span>; bytes=<span class="hljs-number">0</span>; <span class="hljs-comment">/** 统计clients个进程的数据信息 ***/</span> <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>) {<span class="hljs-comment">//需要进行循环,否则的话,由于子进程的创建,很有可能会返回父进程。</span> pid=<span class="hljs-built_in">fscanf</span>(f,<span class="hljs-string">"%d %d %d"</span>,&i,&j,&k);<span class="hljs-comment">//阻塞进行读取数据,等所有的子进程都返回再退出循环</span> <span class="hljs-keyword">if</span>(pid<<span class="hljs-number">2</span>) {<span class="hljs-comment">//有进程成了僵尸进程,退出循环,不再进行统计</span> <span class="hljs-built_in">fprintf</span>(stderr,<span class="hljs-string">"Some of our childrens died.\n"</span>); <span class="hljs-keyword">break</span>; } speed+=i;<span class="hljs-comment">//统计成功请求数</span> failed+=j;<span class="hljs-comment">//统计失败请求数</span> bytes+=k;<span class="hljs-comment">//统计服务器总共返回的字节数</span> <span class="hljs-comment">/* fprintf(stderr,"*Knock* %d %d read=%d\n",speed,failed,pid); */</span> <span class="hljs-keyword">if</span>(--clients==<span class="hljs-number">0</span>) <span class="hljs-keyword">break</span>;<span class="hljs-comment">//成功读取一个客户端成功执行返回</span> } fclose(f); <span class="hljs-built_in">printf</span>(<span class="hljs-string">"\nSpeed=%d pages/min, %d bytes/sec.\nRequests: %d susceed, %d failed.\n"</span>, (<span class="hljs-keyword">int</span>)((speed+failed)/(benchtime/<span class="hljs-number">60.0f</span>)),<span class="hljs-comment">//每分钟的请求数</span> (<span class="hljs-keyword">int</span>)(bytes/(<span class="hljs-keyword">float</span>)benchtime),<span class="hljs-comment">//每秒服务器返回的字节数</span> speed,<span class="hljs-comment">//成功请求数</span> failed);<span class="hljs-comment">//失败请求数</span> } <span class="hljs-keyword">return</span> i; } <span class="hljs-keyword">void</span> benchcore(<span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *host,<span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span> port,<span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *req) { <span class="hljs-keyword">int</span> rlen; <span class="hljs-keyword">char</span> buf[<span class="hljs-number">1500</span>]; <span class="hljs-keyword">int</span> s,i; <span class="hljs-keyword">struct</span> sigaction sa; <span class="hljs-comment">/* setup alarm signal handler */</span> sa.sa_handler=alarm_handler; sa.sa_flags=<span class="hljs-number">0</span>; <span class="hljs-keyword">if</span>(sigaction(SIGALRM,&sa,NULL)) <span class="hljs-built_in">exit</span>(<span class="hljs-number">3</span>); alarm(benchtime); rlen=<span class="hljs-built_in">strlen</span>(req); nexttry:<span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>) { <span class="hljs-keyword">if</span>(timerexpired) {<span class="hljs-comment">//客户端执行时间到</span> <span class="hljs-keyword">if</span>(failed><span class="hljs-number">0</span>) { <span class="hljs-comment">/* fprintf(stderr,"Correcting failed by signal\n"); */</span> failed--; } <span class="hljs-keyword">return</span>;<span class="hljs-comment">//返回到调用benchcore()函数处</span> } <span class="hljs-comment">//创建连接到服务器的socket标识</span> s=Socket(host,port); <span class="hljs-keyword">if</span>(s<<span class="hljs-number">0</span>) { failed++;<span class="hljs-keyword">continue</span>;} <span class="hljs-comment">//向服务器发送rlen个http的请求字符</span> <span class="hljs-keyword">if</span>(rlen!=write(s,req,rlen)) {failed++;close(s);<span class="hljs-keyword">continue</span>;} <span class="hljs-keyword">if</span>(http10==<span class="hljs-number">0</span>) <span class="hljs-keyword">if</span>(shutdown(s,<span class="hljs-number">1</span>)) { failed++;close(s);<span class="hljs-keyword">continue</span>;} <span class="hljs-keyword">if</span>(force==<span class="hljs-number">0</span>) { <span class="hljs-comment">/* read all available data from socket */</span> <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>) { <span class="hljs-keyword">if</span>(timerexpired) <span class="hljs-keyword">break</span>; <span class="hljs-comment">//读取从服务器返回的数据</span> i=read(s,buf,<span class="hljs-number">1500</span>); <span class="hljs-comment">/* fprintf(stderr,"%d\n",i); */</span> <span class="hljs-keyword">if</span>(i<<span class="hljs-number">0</span>) { <span class="hljs-comment">//读取失败</span> failed++; close(s); <span class="hljs-keyword">goto</span> nexttry; } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(i==<span class="hljs-number">0</span>) <span class="hljs-keyword">break</span>;<span class="hljs-comment">//读取完毕</span> <span class="hljs-keyword">else</span> bytes+=i;<span class="hljs-comment">//计算返回字节数</span> } } <span class="hljs-keyword">if</span>(close(s)) {failed++;<span class="hljs-keyword">continue</span>;} speed++; } } </code></span><ul style="display: block;" class="pre-numbering"><li><span style="font-size:14px;">1</span></li><li><span style="font-size:14px;">2</span></li><li><span style="font-size:14px;">3</span></li><li><span style="font-size:14px;">4</span></li><li><span style="font-size:14px;">5</span></li><li><span style="font-size:14px;">6</span></li><li><span style="font-size:14px;">7</span></li><li><span style="font-size:14px;">8</span></li><li><span style="font-size:14px;">9</span></li><li><span style="font-size:14px;">10</span></li><li><span style="font-size:14px;">11</span></li><li><span style="font-size:14px;">12</span></li><li><span style="font-size:14px;">13</span></li><li><span style="font-size:14px;">14</span></li><li><span style="font-size:14px;">15</span></li><li><span style="font-size:14px;">16</span></li><li><span style="font-size:14px;">17</span></li><li><span style="font-size:14px;">18</span></li><li><span style="font-size:14px;">19</span></li><li><span style="font-size:14px;">20</span></li><li><span style="font-size:14px;">21</span></li><li><span style="font-size:14px;">22</span></li><li><span style="font-size:14px;">23</span></li><li><span style="font-size:14px;">24</span></li><li><span style="font-size:14px;">25</span></li><li><span style="font-size:14px;">26</span></li><li><span style="font-size:14px;">27</span></li><li><span style="font-size:14px;">28</span></li><li><span style="font-size:14px;">29</span></li><li><span style="font-size:14px;">30</span></li><li><span style="font-size:14px;">31</span></li><li><span style="font-size:14px;">32</span></li><li><span style="font-size:14px;">33</span></li><li><span style="font-size:14px;">34</span></li><li><span style="font-size:14px;">35</span></li><li><span style="font-size:14px;">36</span></li><li><span style="font-size:14px;">37</span></li><li><span style="font-size:14px;">38</span></li><li><span style="font-size:14px;">39</span></li><li><span style="font-size:14px;">40</span></li><li><span style="font-size:14px;">41</span></li><li><span style="font-size:14px;">42</span></li><li><span style="font-size:14px;">43</span></li><li><span style="font-size:14px;">44</span></li><li><span style="font-size:14px;">45</span></li><li><span style="font-size:14px;">46</span></li><li><span style="font-size:14px;">47</span></li><li><span style="font-size:14px;">48</span></li><li><span style="font-size:14px;">49</span></li><li><span style="font-size:14px;">50</span></li><li><span style="font-size:14px;">51</span></li><li><span style="font-size:14px;">52</span></li><li><span style="font-size:14px;">53</span></li><li><span style="font-size:14px;">54</span></li><li><span style="font-size:14px;">55</span></li><li><span style="font-size:14px;">56</span></li><li><span style="font-size:14px;">57</span></li><li><span style="font-size:14px;">58</span></li><li><span style="font-size:14px;">59</span></li><li><span style="font-size:14px;">60</span></li><li><span style="font-size:14px;">61</span></li><li><span style="font-size:14px;">62</span></li><li><span style="font-size:14px;">63</span></li><li><span style="font-size:14px;">64</span></li><li><span style="font-size:14px;">65</span></li><li><span style="font-size:14px;">66</span></li><li><span style="font-size:14px;">67</span></li><li><span style="font-size:14px;">68</span></li><li><span style="font-size:14px;">69</span></li><li><span style="font-size:14px;">70</span></li><li><span style="font-size:14px;">71</span></li><li><span style="font-size:14px;">72</span></li><li><span style="font-size:14px;">73</span></li><li><span style="font-size:14px;">74</span></li><li><span style="font-size:14px;">75</span></li><li><span style="font-size:14px;">76</span></li><li><span style="font-size:14px;">77</span></li><li><span style="font-size:14px;">78</span></li><li><span style="font-size:14px;">79</span></li><li><span style="font-size:14px;">80</span></li><li><span style="font-size:14px;">81</span></li><li><span style="font-size:14px;">82</span></li><li><span style="font-size:14px;">83</span></li><li><span style="font-size:14px;">84</span></li><li><span style="font-size:14px;">85</span></li><li><span style="font-size:14px;">86</span></li><li><span style="font-size:14px;">87</span></li><li><span style="font-size:14px;">88</span></li><li><span style="font-size:14px;">89</span></li><li><span style="font-size:14px;">90</span></li><li><span style="font-size:14px;">91</span></li><li><span style="font-size:14px;">92</span></li><li><span style="font-size:14px;">93</span></li><li><span style="font-size:14px;">94</span></li><li><span style="font-size:14px;">95</span></li><li><span style="font-size:14px;">96</span></li><li><span style="font-size:14px;">97</span></li><li><span style="font-size:14px;">98</span></li><li><span style="font-size:14px;">99</span></li><li><span style="font-size:14px;">100</span></li><li><span style="font-size:14px;">101</span></li><li><span style="font-size:14px;">102</span></li><li><span style="font-size:14px;">103</span></li><li><span style="font-size:14px;">104</span></li><li><span style="font-size:14px;">105</span></li><li><span style="font-size:14px;">106</span></li><li><span style="font-size:14px;">107</span></li><li><span style="font-size:14px;">108</span></li><li><span style="font-size:14px;">109</span></li><li><span style="font-size:14px;">110</span></li><li><span style="font-size:14px;">111</span></li><li><span style="font-size:14px;">112</span></li><li><span style="font-size:14px;">113</span></li><li><span style="font-size:14px;">114</span></li><li><span style="font-size:14px;">115</span></li><li><span style="font-size:14px;">116</span></li><li><span style="font-size:14px;">117</span></li><li><span style="font-size:14px;">118</span></li><li><span style="font-size:14px;">119</span></li><li><span style="font-size:14px;">120</span></li><li><span style="font-size:14px;">121</span></li><li><span style="font-size:14px;">122</span></li><li><span style="font-size:14px;">123</span></li><li><span style="font-size:14px;">124</span></li><li><span style="font-size:14px;">125</span></li><li><span style="font-size:14px;">126</span></li><li><span style="font-size:14px;">127</span></li><li><span style="font-size:14px;">128</span></li><li><span style="font-size:14px;">129</span></li><li><span style="font-size:14px;">130</span></li><li><span style="font-size:14px;">131</span></li><li><span style="font-size:14px;">132</span></li><li><span style="font-size:14px;">133</span></li><li><span style="font-size:14px;">134</span></li><li><span style="font-size:14px;">135</span></li><li><span style="font-size:14px;">136</span></li><li><span style="font-size:14px;">137</span></li><li><span style="font-size:14px;">138</span></li><li><span style="font-size:14px;">139</span></li><li><span style="font-size:14px;">140</span></li><li><span style="font-size:14px;">141</span></li><li><span style="font-size:14px;">142</span></li><li><span style="font-size:14px;">143</span></li><li><span style="font-size:14px;">144</span></li><li><span style="font-size:14px;">145</span></li><li><span style="font-size:14px;">146</span></li><li><span style="font-size:14px;">147</span></li><li><span style="font-size:14px;">148</span></li><li><span style="font-size:14px;">149</span></li><li><span style="font-size:14px;">150</span></li><li><span style="font-size:14px;">151</span></li><li><span style="font-size:14px;">152</span></li><li><span style="font-size:14px;">153</span></li><li><span style="font-size:14px;">154</span></li><li><span style="font-size:14px;">155</span></li><li><span style="font-size:14px;">156</span></li><li><span style="font-size:14px;">157</span></li><li><span style="font-size:14px;">158</span></li><li><span style="font-size:14px;">159</span></li><li><span style="font-size:14px;">160</span></li><li><span style="font-size:14px;">161</span></li><li><span style="font-size:14px;">162</span></li><li><span style="font-size:14px;">163</span></li><li><span style="font-size:14px;">164</span></li><li><span style="font-size:14px;">165</span></li><li><span style="font-size:14px;">166</span></li><li><span style="font-size:14px;">167</span></li><li><span style="font-size:14px;">168</span></li><li><span style="font-size:14px;">169</span></li><li><span style="font-size:14px;">170</span></li><li><span style="font-size:14px;">171</span></li><li><span style="font-size:14px;">172</span></li><li><span style="font-size:14px;">173</span></li><li><span style="font-size:14px;">174</span></li><li><span style="font-size:14px;">175</span></li><li><span style="font-size:14px;">176</span></li><li><span style="font-size:14px;">177</span></li><li><span style="font-size:14px;">178</span></li><li><span style="font-size:14px;">179</span></li><li><span style="font-size:14px;">180</span></li><li><span style="font-size:14px;">181</span></li><li><span style="font-size:14px;">182</span></li><li><span style="font-size:14px;">183</span></li><li><span style="font-size:14px;">184</span></li><li><span style="font-size:14px;">185</span></li><li><span style="font-size:14px;">186</span></li><li><span style="font-size:14px;">187</span></li><li><span style="font-size:14px;">188</span></li><li><span style="font-size:14px;">189</span></li><li><span style="font-size:14px;">190</span></li><li><span style="font-size:14px;">191</span></li><li><span style="font-size:14px;">192</span></li><li><span style="font-size:14px;">193</span></li><li><span style="font-size:14px;">194</span></li><li><span style="font-size:14px;">195</span></li><li><span style="font-size:14px;">196</span></li><li><span style="font-size:14px;">197</span></li><li><span style="font-size:14px;">198</span></li><li><span style="font-size:14px;">199</span></li><li><span style="font-size:14px;">200</span></li><li><span style="font-size:14px;">201</span></li><li><span style="font-size:14px;">202</span></li><li><span style="font-size:14px;">203</span></li><li><span style="font-size:14px;">204</span></li><li><span style="font-size:14px;">205</span></li><li><span style="font-size:14px;">206</span></li><li><span style="font-size:14px;">207</span></li><li><span style="font-size:14px;">208</span></li><li><span style="font-size:14px;">209</span></li><li><span style="font-size:14px;">210</span></li><li><span style="font-size:14px;">211</span></li><li><span style="font-size:14px;">212</span></li><li><span style="font-size:14px;">213</span></li><li><span style="font-size:14px;">214</span></li><li><span style="font-size:14px;">215</span></li><li><span style="font-size:14px;">216</span></li><li><span style="font-size:14px;">217</span></li><li><span style="font-size:14px;">218</span></li><li><span style="font-size:14px;">219</span></li><li><span style="font-size:14px;">220</span></li><li><span style="font-size:14px;">221</span></li><li><span style="font-size:14px;">222</span></li><li><span style="font-size:14px;">223</span></li><li><span style="font-size:14px;">224</span></li><li><span style="font-size:14px;">225</span></li><li><span style="font-size:14px;">226</span></li><li><span style="font-size:14px;">227</span></li><li><span style="font-size:14px;">228</span></li><li><span style="font-size:14px;">229</span></li><li><span style="font-size:14px;">230</span></li><li><span style="font-size:14px;">231</span></li><li><span style="font-size:14px;">232</span></li><li><span style="font-size:14px;">233</span></li><li><span style="font-size:14px;">234</span></li><li><span style="font-size:14px;">235</span></li><li><span style="font-size:14px;">236</span></li><li><span style="font-size:14px;">237</span></li><li><span style="font-size:14px;">238</span></li><li><span style="font-size:14px;">239</span></li><li><span style="font-size:14px;">240</span></li><li><span style="font-size:14px;">241</span></li><li><span style="font-size:14px;">242</span></li><li><span style="font-size:14px;">243</span></li><li><span style="font-size:14px;">244</span></li><li><span style="font-size:14px;">245</span></li><li><span style="font-size:14px;">246</span></li><li><span style="font-size:14px;">247</span></li><li><span style="font-size:14px;">248</span></li><li><span style="font-size:14px;">249</span></li><li><span style="font-size:14px;">250</span></li><li><span style="font-size:14px;">251</span></li><li><span style="font-size:14px;">252</span></li><li><span style="font-size:14px;">253</span></li><li><span style="font-size:14px;">254</span></li><li><span style="font-size:14px;">255</span></li><li><span style="font-size:14px;">256</span></li><li><span style="font-size:14px;">257</span></li><li><span style="font-size:14px;">258</span></li><li><span style="font-size:14px;">259</span></li><li><span style="font-size:14px;">260</span></li><li><span style="font-size:14px;">261</span></li><li><span style="font-size:14px;">262</span></li><li><span style="font-size:14px;">263</span></li><li><span style="font-size:14px;">264</span></li><li><span style="font-size:14px;">265</span></li><li><span style="font-size:14px;">266</span></li><li><span style="font-size:14px;">267</span></li><li><span style="font-size:14px;">268</span></li><li><span style="font-size:14px;">269</span></li><li><span style="font-size:14px;">270</span></li><li><span style="font-size:14px;">271</span></li><li><span style="font-size:14px;">272</span></li><li><span style="font-size:14px;">273</span></li><li><span style="font-size:14px;">274</span></li><li><span style="font-size:14px;">275</span></li><li><span style="font-size:14px;">276</span></li><li><span style="font-size:14px;">277</span></li><li><span style="font-size:14px;">278</span></li><li><span style="font-size:14px;">279</span></li><li><span style="font-size:14px;">280</span></li><li><span style="font-size:14px;">281</span></li><li><span style="font-size:14px;">282</span></li><li><span style="font-size:14px;">283</span></li><li><span style="font-size:14px;">284</span></li><li><span style="font-size:14px;">285</span></li><li><span style="font-size:14px;">286</span></li><li><span style="font-size:14px;">287</span></li><li><span style="font-size:14px;">288</span></li><li><span style="font-size:14px;">289</span></li><li><span style="font-size:14px;">290</span></li><li><span style="font-size:14px;">291</span></li><li><span style="font-size:14px;">292</span></li><li><span style="font-size:14px;">293</span></li><li><span style="font-size:14px;">294</span></li><li><span style="font-size:14px;">295</span></li><li><span style="font-size:14px;">296</span></li><li><span style="font-size:14px;">297</span></li><li><span style="font-size:14px;">298</span></li><li><span style="font-size:14px;">299</span></li><li><span style="font-size:14px;">300</span></li><li><span style="font-size:14px;">301</span></li><li><span style="font-size:14px;">302</span></li><li><span style="font-size:14px;">303</span></li><li><span style="font-size:14px;">304</span></li><li><span style="font-size:14px;">305</span></li><li><span style="font-size:14px;">306</span></li><li><span style="font-size:14px;">307</span></li><li><span style="font-size:14px;">308</span></li><li><span style="font-size:14px;">309</span></li><li><span style="font-size:14px;">310</span></li><li><span style="font-size:14px;">311</span></li><li><span style="font-size:14px;">312</span></li><li><span style="font-size:14px;">313</span></li><li><span style="font-size:14px;">314</span></li><li><span style="font-size:14px;">315</span></li><li><span style="font-size:14px;">316</span></li><li><span style="font-size:14px;">317</span></li><li><span style="font-size:14px;">318</span></li><li><span style="font-size:14px;">319</span></li><li><span style="font-size:14px;">320</span></li><li><span style="font-size:14px;">321</span></li><li><span style="font-size:14px;">322</span></li><li><span style="font-size:14px;">323</span></li><li><span style="font-size:14px;">324</span></li><li><span style="font-size:14px;">325</span></li><li><span style="font-size:14px;">326</span></li><li><span style="font-size:14px;">327</span></li><li><span style="font-size:14px;">328</span></li><li><span style="font-size:14px;">329</span></li><li><span style="font-size:14px;">330</span></li><li><span style="font-size:14px;">331</span></li><li><span style="font-size:14px;">332</span></li><li><span style="font-size:14px;">333</span></li><li><span style="font-size:14px;">334</span></li><li><span style="font-size:14px;">335</span></li><li><span style="font-size:14px;">336</span></li><li><span style="font-size:14px;">337</span></li><li><span style="font-size:14px;">338</span></li><li><span style="font-size:14px;">339</span></li><li><span style="font-size:14px;">340</span></li><li><span style="font-size:14px;">341</span></li><li><span style="font-size:14px;">342</span></li><li><span style="font-size:14px;">343</span></li><li><span style="font-size:14px;">344</span></li><li><span style="font-size:14px;">345</span></li><li><span style="font-size:14px;">346</span></li><li><span style="font-size:14px;">347</span></li><li><span style="font-size:14px;">348</span></li><li><span style="font-size:14px;">349</span></li><li><span style="font-size:14px;">350</span></li><li><span style="font-size:14px;">351</span></li><li><span style="font-size:14px;">352</span></li><li><span style="font-size:14px;">353</span></li><li><span style="font-size:14px;">354</span></li><li><span style="font-size:14px;">355</span></li><li><span style="font-size:14px;">356</span></li><li><span style="font-size:14px;">357</span></li><li><span style="font-size:14px;">358</span></li><li><span style="font-size:14px;">359</span></li><li><span style="font-size:14px;">360</span></li><li><span style="font-size:14px;">361</span></li><li><span style="font-size:14px;">362</span></li><li><span style="font-size:14px;">363</span></li><li><span style="font-size:14px;">364</span></li><li><span style="font-size:14px;">365</span></li><li><span style="font-size:14px;">366</span></li><li><span style="font-size:14px;">367</span></li><li><span style="font-size:14px;">368</span></li><li><span style="font-size:14px;">369</span></li><li><span style="font-size:14px;">370</span></li><li><span style="font-size:14px;">371</span></li><li><span style="font-size:14px;">372</span></li><li><span style="font-size:14px;">373</span></li><li><span style="font-size:14px;">374</span></li><li><span style="font-size:14px;">375</span></li><li><span style="font-size:14px;">376</span></li><li><span style="font-size:14px;">377</span></li><li><span style="font-size:14px;">378</span></li><li><span style="font-size:14px;">379</span></li><li><span style="font-size:14px;">380</span></li><li><span style="font-size:14px;">381</span></li><li><span style="font-size:14px;">382</span></li><li><span style="font-size:14px;">383</span></li><li><span style="font-size:14px;">384</span></li><li><span style="font-size:14px;">385</span></li><li><span style="font-size:14px;">386</span></li><li><span style="font-size:14px;">387</span></li><li><span style="font-size:14px;">388</span></li><li><span style="font-size:14px;">389</span></li><li><span style="font-size:14px;">390</span></li><li><span style="font-size:14px;">391</span></li><li><span style="font-size:14px;">392</span></li><li><span style="font-size:14px;">393</span></li><li><span style="font-size:14px;">394</span></li><li><span style="font-size:14px;">395</span></li><li><span style="font-size:14px;">396</span></li><li><span style="font-size:14px;">397</span></li><li><span style="font-size:14px;">398</span></li><li><span style="font-size:14px;">399</span></li><li><span style="font-size:14px;">400</span></li><li><span style="font-size:14px;">401</span></li><li><span style="font-size:14px;">402</span></li><li><span style="font-size:14px;">403</span></li><li><span style="font-size:14px;">404</span></li><li><span style="font-size:14px;">405</span></li><li><span style="font-size:14px;">406</span></li><li><span style="font-size:14px;">407</span></li><li><span style="font-size:14px;">408</span></li><li><span style="font-size:14px;">409</span></li><li><span style="font-size:14px;">410</span></li><li><span style="font-size:14px;">411</span></li><li><span style="font-size:14px;">412</span></li><li><span style="font-size:14px;">413</span></li><li><span style="font-size:14px;">414</span></li><li><span style="font-size:14px;">415</span></li><li><span style="font-size:14px;">416</span></li><li><span style="font-size:14px;">417</span></li><li><span style="font-size:14px;">418</span></li><li><span style="font-size:14px;">419</span></li><li><span style="font-size:14px;">420</span></li><li><span style="font-size:14px;">421</span></li><li><span style="font-size:14px;">422</span></li><li><span style="font-size:14px;">423</span></li><li><span style="font-size:14px;">424</span></li><li><span style="font-size:14px;">425</span></li><li><span style="font-size:14px;">426</span></li><li><span style="font-size:14px;">427</span></li><li><span style="font-size:14px;">428</span></li><li><span style="font-size:14px;">429</span></li><li><span style="font-size:14px;">430</span></li><li><span style="font-size:14px;">431</span></li><li><span style="font-size:14px;">432</span></li><li><span style="font-size:14px;">433</span></li><li><span style="font-size:14px;">434</span></li><li><span style="font-size:14px;">435</span></li><li><span style="font-size:14px;">436</span></li><li><span style="font-size:14px;">437</span></li><li><span style="font-size:14px;">438</span></li><li><span style="font-size:14px;">439</span></li><li><span style="font-size:14px;">440</span></li></ul>
socket.c
<span style="font-size:14px;"><code class="language-c hljs has-numbering"><span class="hljs-preprocessor">#include <sys/types.h></span> <span class="hljs-preprocessor">#include <sys/socket.h></span> <span class="hljs-preprocessor">#include <fcntl.h></span> <span class="hljs-preprocessor">#include <netinet/in.h></span> <span class="hljs-preprocessor">#include <arpa/inet.h></span> <span class="hljs-preprocessor">#include <netdb.h></span> <span class="hljs-preprocessor">#include <sys/time.h></span> <span class="hljs-preprocessor">#include <string.h></span> <span class="hljs-preprocessor">#include <unistd.h></span> <span class="hljs-preprocessor">#include <stdio.h></span> <span class="hljs-preprocessor">#include <stdlib.h></span> <span class="hljs-preprocessor">#include <stdarg.h></span> <span class="hljs-keyword">int</span> Socket(<span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *host, <span class="hljs-keyword">int</span> clientPort) { <span class="hljs-keyword">int</span> sock; <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">long</span> inaddr; <span class="hljs-keyword">struct</span> sockaddr_in ad; <span class="hljs-keyword">struct</span> hostent *hp; <span class="hljs-built_in">memset</span>(&ad, <span class="hljs-number">0</span>, <span class="hljs-keyword">sizeof</span>(ad)); ad.sin_family = AF_INET; inaddr = inet_addr(host); <span class="hljs-keyword">if</span> (inaddr != INADDR_NONE) <span class="hljs-built_in">memcpy</span>(&ad.sin_addr, &inaddr, <span class="hljs-keyword">sizeof</span>(inaddr)); <span class="hljs-keyword">else</span> { hp = gethostbyname(host); <span class="hljs-keyword">if</span> (hp == NULL) <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>; <span class="hljs-built_in">memcpy</span>(&ad.sin_addr, hp->h_addr, hp->h_length); } ad.sin_port = htons(clientPort); sock = socket(AF_INET, SOCK_STREAM, <span class="hljs-number">0</span>); <span class="hljs-keyword">if</span> (sock < <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span> sock; <span class="hljs-keyword">if</span> (connect(sock, (<span class="hljs-keyword">struct</span> sockaddr *)&ad, <span class="hljs-keyword">sizeof</span>(ad)) < <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>; <span class="hljs-keyword">return</span> sock; } </code></span>
下面是使用webbench运行的一个测试