ref: http://www.cnblogs.com/xudong-bupt/archive/2013/12/29/3483059.html
http://blog.youkuaiyun.com/love_gaohz/article/details/43700411
http://blog.youkuaiyun.com/wesleyluo/article/details/6149071
server.cpp


#include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <string.h> #include <stdlib.h> #include <fcntl.h> #include <sys/shm.h> #include <sys/time.h> #define MYPORT 5566 #define QUEUE 20 #define BUFFER_SIZE 1024 int timeval_subtract(struct timeval* result, struct timeval* x, struct timeval* y) { int nsec; if ( x->tv_sec>y->tv_sec ) return -1; if ( (x->tv_sec==y->tv_sec) && (x->tv_usec>y->tv_usec) ) return -1; result->tv_sec = ( y->tv_sec-x->tv_sec ); result->tv_usec = ( y->tv_usec-x->tv_usec ); if (result->tv_usec<0) { result->tv_sec--; result->tv_usec+=1000000; } return 0; } int main() { ///定义sockfd int server_sockfd = socket(AF_INET,SOCK_STREAM, 0); ///定义sockaddr_in struct sockaddr_in server_sockaddr; server_sockaddr.sin_family = AF_INET; server_sockaddr.sin_port = htons(MYPORT); server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY); ///bind,成功返回0,出错返回-1 if(bind(server_sockfd,(struct sockaddr *)&server_sockaddr,sizeof(server_sockaddr))==-1) { perror("bind"); exit(1); } ///listen,成功返回0,出错返回-1 if(listen(server_sockfd,QUEUE) == -1) { perror("listen"); exit(1); } ///客户端套接字 char buffer[BUFFER_SIZE]; struct sockaddr_in client_addr; socklen_t length = sizeof(client_addr); ///成功返回非负描述字,出错返回-1 int conn = accept(server_sockfd, (struct sockaddr*)&client_addr, &length); if(conn<0) { perror("connect"); exit(1); } #if 1 int flags; if ((flags = fcntl(conn, F_GETFL, NULL)) < 0) { return -1; } if (fcntl(conn, F_SETFL, flags | O_NONBLOCK) == -1) { return -1; } #endif #if 1 int nRecvBufLen = 0; //设置为10M setsockopt( conn, SOL_SOCKET, SO_RCVBUF, ( const char* )&nRecvBufLen, sizeof( int ) ); #endif struct timeval start,stop,diff; int i = 0; while(1) { if (i == 0) { gettimeofday(&start,0); printf( "CAPABILITY_1_SECS_%010u_USEC_%010u\n", start.tv_sec, start.tv_usec ); } int len = recv(conn, buffer, sizeof(buffer),0); i++; if (i == 10000) { gettimeofday(&stop,0); printf( "CAPABILITY_2_SECS_%010u_USEC_%010u\n", stop.tv_sec, stop.tv_usec ); timeval_subtract(&diff,&start,&stop); printf( "CAPABILITY_3_SECS_%010u_USEC_%010u\n", diff.tv_sec, diff.tv_usec ); } } close(conn); close(server_sockfd); return 0; }
client.cpp


#include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <string.h> #include <stdlib.h> #include <fcntl.h> #include <sys/shm.h> #include <sys/time.h> #include <time.h> #define MYPORT 5566 #define BUFFER_SIZE 1024 int timeval_subtract(struct timeval* result, struct timeval* x, struct timeval* y) { int nsec; if ( x->tv_sec>y->tv_sec ) return -1; if ( (x->tv_sec==y->tv_sec) && (x->tv_usec>y->tv_usec) ) return -1; result->tv_sec = ( y->tv_sec-x->tv_sec ); result->tv_usec = ( y->tv_usec-x->tv_usec ); if (result->tv_usec<0) { result->tv_sec--; result->tv_usec+=1000000; } return 0; } int main() { ///定义sockfd int sock_cli = socket(AF_INET,SOCK_STREAM, 0); ///定义sockaddr_in struct sockaddr_in servaddr; memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(MYPORT); ///服务器端口 servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); ///服务器ip ///连接服务器,成功返回0,错误返回-1 if (connect(sock_cli, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { perror("connect"); exit(1); } #if 1 int flags; if ((flags = fcntl(sock_cli, F_GETFL, NULL)) < 0) { return -1; } if (fcntl(sock_cli, F_SETFL, flags | O_NONBLOCK) == -1) { return -1; } #endif #if 1 int nSendBufLen = 0; //设置为10M setsockopt( sock_cli, SOL_SOCKET, SO_SNDBUF, ( const char* )&nSendBufLen, sizeof( int ) ); #endif char sendbuf[BUFFER_SIZE]; strcpy(sendbuf, "hello server"); struct timeval start,stop,diff; int i = 0; while (1) { if (i == 0) { gettimeofday(&start,0); printf( "CAPABILITY_1_SECS_%010u_USEC_%010u\n", start.tv_sec, start.tv_usec ); } send(sock_cli, sendbuf, strlen(sendbuf),0); ///发送 i++; if (i == 10000) { gettimeofday(&stop,0); printf( "CAPABILITY_2_SECS_%010u_USEC_%010u\n", stop.tv_sec, stop.tv_usec ); timeval_subtract(&diff,&start,&stop); printf( "CAPABILITY_3_SECS_%010u_USEC_%010u\n", diff.tv_sec, diff.tv_usec ); } } close(sock_cli); return 0; }