www.cnblogs.com/ruiyqinrui

开源、架构、Linux C/C++/python AI BI 运维开发自动化运维。 春风桃李花 秋雨梧桐叶。“力尽不知热 但惜夏日长”。夏不惜,秋不获。@ruiY--秦瑞

python爬虫,C编程,嵌入式开发.hadoop大数据,桉树,onenebula云计算架构.linux运维及驱动开发.

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

测试Mellanox卡硬件性能程序

  1 //本文件是服务器的代码
  2 #include <netinet/in.h>    // for sockaddr_in
  3 #include <sys/types.h>    // for socket
  4 #include <sys/socket.h>    // for socket
  5 #include <stdio.h>        // for printf
  6 #include <stdlib.h>        // for exit
  7 #include <string.h>        // for bzero
  8 /*
  9 #include <sys/types.h>
 10 #include <sys/stat.h>
 11 #include <fcntl.h>
 12 #include <unistd.h>
 13 */
 14 #define HELLO_WORLD_SERVER_PORT    6666
 15 #define LENGTH_OF_LISTEN_QUEUE  20
 16 #define BUFFER_SIZE 1024
 17 #define FILE_NAME_MAX_SIZE 512
 18 
 19 int main(int argc, char **argv)
 20 {
 21     //设置一个socket地址结构server_addr,代表服务器internet地址, 端口
 22     struct sockaddr_in server_addr;
 23     bzero(&server_addr,sizeof(server_addr)); //把一段内存区的内容全部设置为0
 24     server_addr.sin_family = AF_INET;
 25     server_addr.sin_addr.s_addr = htons(INADDR_ANY);
 26     server_addr.sin_port = htons(HELLO_WORLD_SERVER_PORT);
 27 
 28     //创建用于internet的流协议(TCP)socket,用server_socket代表服务器socket
 29     int server_socket = socket(PF_INET,SOCK_STREAM,0);
 30     if( server_socket < 0)
 31     {
 32         printf("Create Socket Failed!");
 33         exit(1);
 34     }
 35    
 36     //把socket和socket地址结构联系起来
 37     if( bind(server_socket,(struct sockaddr*)&server_addr,sizeof(server_addr)))
 38     {
 39         printf("Server Bind Port : %d Failed!", HELLO_WORLD_SERVER_PORT);
 40         exit(1);
 41     }
 42    
 43     //server_socket用于监听
 44     if ( listen(server_socket, LENGTH_OF_LISTEN_QUEUE) )
 45     {
 46         printf("Server Listen Failed!");
 47         exit(1);
 48     }
 49     while (1) //服务器端要一直运行
 50     {
 51         //定义客户端的socket地址结构client_addr
 52         struct sockaddr_in client_addr;
 53         socklen_t length = sizeof(client_addr);
 54 
 55         //接受一个到server_socket代表的socket的一个连接
 56         //如果没有连接请求,就等待到有连接请求--这是accept函数的特性
 57         //accept函数返回一个新的socket,这个socket(new_server_socket)用于同连接到的客户的通信
 58         //new_server_socket代表了服务器和客户端之间的一个通信通道
 59         //accept函数把连接到的客户端信息填写到客户端的socket地址结构client_addr中
 60         int new_server_socket = accept(server_socket,(struct sockaddr*)&client_addr,&length);
 61         if ( new_server_socket < 0)
 62         {
 63             printf("Server Accept Failed!\n");
 64             break;
 65         }
 66        
 67         char buffer[BUFFER_SIZE];
 68         bzero(buffer, BUFFER_SIZE);
 69         length = recv(new_server_socket,buffer,BUFFER_SIZE,0);//这里先接收客户端发来的要获取的文件名
 70         if (length < 0)
 71         {
 72             printf("Server Recieve Data Failed!\n");
 73             break;
 74         }
 75         char file_name[FILE_NAME_MAX_SIZE+1];
 76         bzero(file_name, FILE_NAME_MAX_SIZE+1);
 77         strncpy(file_name, buffer, strlen(buffer)>FILE_NAME_MAX_SIZE?FILE_NAME_MAX_SIZE:strlen(buffer));
 78 //        int fp = open(file_name, O_RDONLY);
 79 //        if( fp < 0 )
 80         FILE * fp = fopen(file_name,"r");
 81         if(NULL == fp )
 82         {
 83             printf("File:\t%s Not Found\n", file_name);
 84         }
 85         else
 86         {
 87             bzero(buffer, BUFFER_SIZE);
 88             int file_block_length = 0;
 89 //            while( (file_block_length = read(fp,buffer,BUFFER_SIZE))>0)
 90             while( (file_block_length = fread(buffer,sizeof(char),BUFFER_SIZE,fp))>0)
 91             {
 92                 printf("file_block_length = %d\n",file_block_length);
 93                 //发送buffer中的字符串到new_server_socket,实际是给客户端
 94                 if(send(new_server_socket,buffer,file_block_length,0)<0)
 95                 {
 96                     printf("Send File:\t%s Failed\n", file_name);
 97                     break;
 98                 }
 99                 bzero(buffer, BUFFER_SIZE);
100             }                                                                 //这段代码是循环读取文件的一段数据,在循环调用send,发送到客户端,这里强调一点的TCP每次接受最多是1024字节,多了就会分片,因此每次发送时尽量不要超过1024字节。
101 //            close(fp);
102             fclose(fp);
103             printf("File:\t%s Transfer Finished\n",file_name);
104         }
105         //关闭与客户端的连接
106         close(new_server_socket);
107     }
108     //关闭监听用的socket
109     close(server_socket);
110     return 0;
111 }
  1 //本文件是客户机的代码
  2 #include <netinet/in.h>    // for sockaddr_in
  3 #include <sys/types.h>    // for socket
  4 #include <sys/socket.h>    // for socket
  5 #include <stdio.h>        // for printf
  6 #include <stdlib.h>        // for exit
  7 #include <string.h>        // for bzero
  8 /*
  9 #include <sys/types.h>
 10 #include <sys/stat.h>
 11 #include <fcntl.h>
 12 #include <unistd.h>
 13 */
 14 
 15 #define HELLO_WORLD_SERVER_PORT    6666
 16 #define BUFFER_SIZE 1024
 17 #define FILE_NAME_MAX_SIZE 512
 18 
 19 int main(int argc, char **argv)
 20 {
 21     if (argc != 2)
 22     {
 23         printf("Usage: ./%s ServerIPAddress\n",argv[0]);
 24         exit(1);
 25     }
 26 
 27     //设置一个socket地址结构client_addr,代表客户机internet地址, 端口
 28     struct sockaddr_in client_addr;
 29     bzero(&client_addr,sizeof(client_addr)); //把一段内存区的内容全部设置为0
 30     client_addr.sin_family = AF_INET;    //internet协议族
 31     client_addr.sin_addr.s_addr = htons(INADDR_ANY);//INADDR_ANY表示自动获取本机地址
 32     client_addr.sin_port = htons(0);    //0表示让系统自动分配一个空闲端口
 33     //创建用于internet的流协议(TCP)socket,用client_socket代表客户机socket
 34     int client_socket = socket(AF_INET,SOCK_STREAM,0);
 35     if( client_socket < 0)
 36     {
 37         printf("Create Socket Failed!\n");
 38         exit(1);
 39     }
 40     //把客户机的socket和客户机的socket地址结构联系起来
 41     if( bind(client_socket,(struct sockaddr*)&client_addr,sizeof(client_addr)))
 42     {
 43         printf("Client Bind Port Failed!\n");
 44         exit(1);
 45     }
 46 
 47     //设置一个socket地址结构server_addr,代表服务器的internet地址, 端口
 48     struct sockaddr_in server_addr;
 49     bzero(&server_addr,sizeof(server_addr));
 50     server_addr.sin_family = AF_INET;
 51     if(inet_aton(argv[1],&server_addr.sin_addr) == 0) //服务器的IP地址来自程序的参数
 52     {
 53         printf("Server IP Address Error!\n");
 54         exit(1);
 55     }
 56     server_addr.sin_port = htons(HELLO_WORLD_SERVER_PORT);
 57     socklen_t server_addr_length = sizeof(server_addr);
 58     //向服务器发起连接,连接成功后client_socket代表了客户机和服务器的一个socket连接
 59     if(connect(client_socket,(struct sockaddr*)&server_addr, server_addr_length) < 0)
 60     {
 61         printf("Can Not Connect To %s!\n",argv[1]);
 62         exit(1);
 63     }
 64 
 65     char file_name[FILE_NAME_MAX_SIZE+1];
 66     bzero(file_name, FILE_NAME_MAX_SIZE+1);
 67     printf("Please Input File Name On Server:\t");
 68     scanf("%s", file_name);
 69    
 70     char buffer[BUFFER_SIZE];
 71     bzero(buffer,BUFFER_SIZE);
 72     strncpy(buffer, file_name, strlen(file_name)>BUFFER_SIZE?BUFFER_SIZE:strlen(file_name));
 73     //向服务器发送buffer中的数据
 74     send(client_socket,buffer,BUFFER_SIZE,0);
 75 
 76 //    int fp = open(file_name, O_WRONLY|O_CREAT);
 77 //    if( fp < 0 )
 78     FILE * fp = fopen(file_name,"w");
 79     if(NULL == fp )
 80     {
 81         printf("File:\t%s Can Not Open To Write\n", file_name);
 82         exit(1);
 83     }
 84    
 85     //从服务器接收数据到buffer中
 86     bzero(buffer,BUFFER_SIZE);
 87     int length = 0;
 88     while( length = recv(client_socket,buffer,BUFFER_SIZE,0))       //循环接收,再写到文件
 89     {
 90         if(length < 0)
 91         {
 92             printf("Recieve Data From Server %s Failed!\n", argv[1]);
 93             break;
 94         }
 95 //        int write_length = write(fp, buffer,length);
 96         int write_length = fwrite(buffer,sizeof(char),length,fp);
 97         if (write_length<length)
 98         {
 99             printf("File:\t%s Write Failed\n", file_name);
100             break;
101         }
102         bzero(buffer,BUFFER_SIZE);   
103     }
104     printf("Recieve File:\t %s From Server[%s] Finished\n",file_name, argv[1]);
105    
106     close(fp);
107     //关闭socket
108     close(client_socket);
109     return 0;
110 }

 

posted on 2014-01-07 13:42  秦瑞It行程实录  阅读(1219)  评论(0编辑  收藏  举报
www.cnblogs.com/ruiyqinrui