Ubuntu 下安装使用
1、安装依赖包CTAGS
sudo apt-get install ctage
2、下载及安装 Webbench
http://home.tiscali.cz/~cz210552/webbench.html
解压:
tar -zxvf webbench-1.5.tar.gz
切换到解压后的目录:
cd webbench-1.5
编译:
make
安装:
sudo make install
webbench使用
#webbench -? (查看命令帮助)
常用参数 说明,-c 表示客户端数,-t 表示时间
./webbench -c 500 -t 30 http://xyzp.xaut.edu.cn/Plugins/YongHu_plug/Pages/loginbysjy.aspx
代码学习:
众所周知,C程序的主函数有两个参数,其中第一个参数是整数,可以获得包括程序名字的参数个数,第二个参数是字符数组或字符指针的指针,可以按顺序获得命令行上各个字符串的参数。其原型是:
int main(int argc, char const *argv[])
或者
int main(int argc, char const **argv)
有鉴于此,在Unix和Linux的正式项目上,程序员通常会使用getopt()或者getopt_long()来获得输入的参数。两者的区别在于getopt()仅支持短格式参数,而getopt_long()既支持短格式参数,也支持长格式参数。
./webbench -V
1.5
./webbench --version
1.5
关于getopt_long()的具体用法参考:man getopt_long
在处理命令行参数时,用到一个变量 optind, 原来是系统定义的。
可以在命令行中,通过 man optind 来看相关信息
optind: the index of the next element to be processed in the argv. The system initializes it to 1. The caller can reset it to 1 to restart scanning of the same argv or scanning a new argument vector.
当调用 getopt() , getopt_long() 之类的函数时, optind 的值会变化。如:
执行 $ ./a.out -ab 当调用 一次 getopt() , 则 optind 值会 +1
进程间通信的方式之管道:管道分为无名管道(匿名管道)和命名管道
使用无名管道,则是通信的进程之间需要一个父子关系,通信的两个进程一定是由一个共同的祖先进程启动。但是无名管道没有数据交叉的问题。
使用命名管道可以解决无名管道中出现的通信的两个进程一定是由通一个共同的祖先进程启动的问题,但是为了数据的安全,很多时候要采用阻塞的FIFO,让写操作变成原子操作。
1 /* $Id: socket.c 1.1 1995/01/01 07:11:14 cthuang Exp $ 2 * 3 * This module has been modified by Radim Kolar for OS/2 emx 4 */ 5 6 /*********************************************************************** 7 module: socket.c 8 program: popclient 9 SCCS ID: @(#)socket.c 1.5 4/1/94 10 programmer: Virginia Tech Computing Center 11 compiler: DEC RISC C compiler (Ultrix 4.1) 12 environment: DEC Ultrix 4.3 13 description: UNIX sockets code. 14 ***********************************************************************/ 15 16 #include <sys/types.h> 17 #include <sys/socket.h> 18 #include <fcntl.h> 19 #include <netinet/in.h> 20 #include <arpa/inet.h> 21 #include <netdb.h> 22 #include <sys/time.h> 23 #include <string.h> 24 #include <unistd.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <stdarg.h> 28 29 30 /* 31 根据通信地址和端口号建立网络连接 32 @host:网络地址 33 @clientPort:端口号 34 成功返回建立连接套接字 35 建立套接字失败返回-1 36 */ 37 int Socket(const char *host, int clientPort) 38 { 39 int sock; 40 unsigned long inaddr; 41 struct sockaddr_in ad; 42 struct hostent *hp; 43 44 memset(&ad, 0, sizeof(ad)); 45 ad.sin_family = AF_INET; 46 //将点分十进制的IP地址转化为无符号的长整形 47 inaddr = inet_addr(host); 48 if (inaddr != INADDR_NONE) 49 memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr)); 50 else 51 { 52 //如果host是域名,则通过域名获取IP地址 53 hp = gethostbyname(host); 54 if (hp == NULL) 55 return -1; 56 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length); 57 } 58 ad.sin_port = htons(clientPort); 59 60 sock = socket(AF_INET, SOCK_STREAM, 0); 61 if (sock < 0) 62 return sock; 63 if (connect(sock, (struct sockaddr *)&ad, sizeof(ad)) < 0) 64 return -1; 65 return sock; 66 }