multiclient(recv_PEEK).c
资源名称:NetProg03.rar [点击查看]
上传用户:gzxinfenfa
上传日期:2015-02-06
资源大小:279k
文件大小:4k
源码类别:
ICQ弱点检测代码
开发平台:
Visual C++
- /* 模拟多个客户
- * case 1: 两个socket顺序读写
- * case 2: 使用I/O 复用技术完成两个socket读写
- * 使用RECV判断客户端连接是否关闭
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <errno.h>
- #include <netdb.h>
- #include <arpa/inet.h>
- #include <unistd.h>
- #include <sys/time.h>
- #include <sys/select.h>
- #define MAXDATASIZE 128
- #define max(a,b) ((a)>(b)?(a):(b))
- int main(int argc,char **argv){
- int sockfd1,sockfd2,nbytes;
- char buf[MAXDATASIZE];
- struct sockaddr_in srvaddr1,srvaddr2;
- int port1, port2;
- int isMultiplex = 0;
- if(argc<3){
- printf("usage:%s port1 port2n");
- exit(0);
- }
- //指定服务器地址
- port1=atoi(argv[1]);
- bzero(&srvaddr1,sizeof(srvaddr1));
- srvaddr1.sin_family = AF_INET;
- srvaddr1.sin_port = htons(port1);
- if(get_ip("127.0.0.1",&srvaddr1.sin_addr)==-1){
- perror(strerror(errno));
- exit(1);
- }
- port2=atoi(argv[2]);
- bzero(&srvaddr2,sizeof(srvaddr2));
- srvaddr2.sin_family = AF_INET;
- srvaddr2.sin_port = htons(port2);
- if(get_ip("localhost",&srvaddr2.sin_addr)==-1){
- perror(strerror(errno));
- exit(1);
- }
- if(argc==4) isMultiplex=1;
- //1.创建网络端点
- sockfd1 = socket(AF_INET,SOCK_STREAM,0);
- sockfd2 = socket(AF_INET,SOCK_STREAM,0);
- if(sockfd1 == -1 || sockfd2 == -1){
- printf("create socket errorn");
- exit(1);
- }
- //2.连接服务器
- if( connect(sockfd1,(struct sockaddr *)&srvaddr1,sizeof(struct sockaddr))==-1 ||
- connect(sockfd2,(struct sockaddr *)&srvaddr2,sizeof(struct sockaddr))==-1){
- printf("connect errorn");
- exit(1);
- }
- //3.发送请求
- write(sockfd1, "Hello-1", 7);
- write(sockfd2, "Hello-2", 7);
- //4.接收响应
- struct timeval starttime,endtime;
- gettimeofday(&starttime,NULL); // get Now time
- printf("start time:%ld ",starttime.tv_sec);
- if(isMultiplex == 0){
- printf(" -- Sequence mode.n");
- if((nbytes=read(sockfd1,buf,MAXDATASIZE))==-1){
- printf("read errorn");
- exit(1);
- }
- buf[nbytes]=' ';
- gettimeofday(&endtime,NULL);
- printf("(%ld) server1 respons:%sn",endtime.tv_sec,buf);
- if((nbytes=read(sockfd2,buf,MAXDATASIZE))==-1){
- printf("read errorn");
- exit(1);
- }
- buf[nbytes]=' ';
- gettimeofday(&endtime,NULL);
- printf("(%ld) server2 respons:%sn",endtime.tv_sec,buf);
- }
- else{ // I/O 多路复用
- int fd1_finished=0;
- int fd2_finished=0;
- int maxfd ;
- fd_set rdset;
- struct timeval tv;
- printf(" -- I/O multiplex mode.n");
- while(fd1_finished == 0 || fd2_finished == 0){
- FD_ZERO(&rdset);
- maxfd = -1;
- if(fd1_finished == 0) {
- FD_SET(sockfd1,&rdset);
- maxfd = sockfd1;
- }
- if(fd2_finished == 0){
- FD_SET(sockfd2,&rdset);
- if( maxfd < 0 ) maxfd = sockfd2;
- else
- maxfd = max(sockfd1, sockfd2);
- }
- tv.tv_sec=0;
- tv.tv_usec=100;
- int n=select(maxfd+1,&rdset,NULL,NULL,&tv);
- if(n<=0)
- continue;
- else{
- if( (0 == fd1_finished) && FD_ISSET(sockfd1,&rdset)){
- if( recv(sockfd1, buf, 1, MSG_PEEK) == 0 ){
- fd1_finished=1;
- }
- else {
- if((nbytes=read(sockfd1,buf,MAXDATASIZE))==-1){
- printf("read errorn");
- exit(1);
- }
- buf[nbytes]=' ';
- gettimeofday(&endtime,NULL);
- printf("(%ld) server1 respons:%sn",endtime.tv_sec,buf);
- }
- }
- if( (0 == fd2_finished) && FD_ISSET(sockfd2,&rdset)){
- if( recv(sockfd2, buf, 1, MSG_PEEK) == 0 ){
- fd2_finished=1;
- }
- else {
- if((nbytes=read(sockfd2,buf,MAXDATASIZE))==-1){
- printf("read errorn");
- exit(1);
- }
- buf[nbytes]=' ';
- gettimeofday(&endtime,NULL);
- printf("(%ld) server2 respons:%sn",endtime.tv_sec,buf);
- }
- }
- }
- }
- }
- //关闭socket
- close(sockfd1);
- close(sockfd2);
- return 0;
- }
- //将字符串形式的IP地址转换为网络字节顺序的二进制形式
- //或根据主机名得到其第一个网络字节顺序的二进制形式
- int get_ip(char *address,struct in_addr *inaddr){
- struct hostent *he;
- if(inet_aton(address,inaddr)!=0){
- return 0;
- }
- he = gethostbyname(address);
- if(he!=NULL){
- *inaddr = *((struct in_addr *)(he->h_addr_list[0]));
- return 0;
- }
- return -1;
- }