用C语言socket编程写简单的server和client程序,目的为了测试一次socket连接中,数据从client端开始发送至server端接受完毕所耗的时间,以及找到原因和提升性能。这里贴出代码,make以后,直接运行server和client即可。
sever.c
/************************************ * For msmr * server_s.c * tesing the speed of socket * 2015-02-04 * author@tom ************************************/ #include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netinet/tcp.h> #define MAX_SIZE 300 int main(int argc, char *argv[]) { int size = 300; int server_sockfd; int client_sockfd; int len; struct sockaddr_in my_addr; struct sockaddr_in remote_addr; int sin_size; char buf[MAX_SIZE]; memset(&my_addr,0,sizeof(my_addr)); my_addr.sin_family=AF_INET; my_addr.sin_addr.s_addr = htonl(0x7f000001); // 127.0.0.1 my_addr.sin_port=htons(9023); if((server_sockfd=socket(PF_INET,SOCK_STREAM,0))<0) { perror("socket"); return 1; } if (bind(server_sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))<0) { perror("bind"); return 1; } listen(server_sockfd,5); // the length of listening queue is 5 sin_size=sizeof(struct sockaddr_in); if((client_sockfd=accept(server_sockfd,(struct sockaddr *)&remote_addr,&sin_size))<0) { perror("accept"); return 1; } // set TCP_NODELAY int enable = 1; if(setsockopt(client_sockfd, IPPROTO_TCP, TCP_NODELAY, (void*)&enable, sizeof(enable)) < 0) // TCP_QUICKACK printf("TCP_NODELAY SETTING ERROR!\n"); struct timeval accept_t; gettimeofday(&accept_t,NULL); printf("Warning: server accept timestamp %lu.%06lu\n", accept_t.tv_sec, accept_t.tv_usec); printf("accept client %s\n",inet_ntoa(remote_addr.sin_addr)); len=recv(client_sockfd,buf,MAX_SIZE,0); struct timeval start_t; gettimeofday(&start_t,NULL); printf("Warning: server finishes reading timestamp %lu.%06lu\n", start_t.tv_sec, start_t.tv_usec); close(client_sockfd); close(server_sockfd); return 0; }
client.c
/************************************ * For msmr * client_s.c * tesing the speed of socket * 2015-02-04 * author@tom ************************************/ #include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netinet/tcp.h> #define MAX_SIZE 300 int main(int argc, char *argv[]) { int client_sockfd; int len; struct sockaddr_in remote_addr; char buf[MAX_SIZE]; memset(&remote_addr,0,sizeof(remote_addr)); remote_addr.sin_family=AF_INET; remote_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); remote_addr.sin_port=htons(9023); if((client_sockfd=socket(PF_INET,SOCK_STREAM,0))<0) { perror("socket"); return 1; } if(connect(client_sockfd,(struct sockaddr *)&remote_addr,sizeof(struct sockaddr))<0) { perror("connect"); return 1; } printf("connected to server\n"); // set TCP_NODELAY int enable = 1; if(setsockopt(client_sockfd, IPPROTO_TCP, TCP_NODELAY, (void*)&enable, sizeof(enable)) < 0) //TCP_QUICKACK printf("TCP_NODELAY SETTING ERROR!\n"); printf("Enter string to send:"); while(scanf("%s",buf) <= 0){} struct timeval start_t; gettimeofday(&start_t,NULL); printf("Warning: Client timestamp %lu.%06lu\n", start_t.tv_sec, start_t.tv_usec); send(client_sockfd,buf,(int)strlen(buf),0); close(client_sockfd); return 0; }
Makefile
#! /bin/bash .PHONY:clean default: gcc -O3 server.c -o server -g gcc -O3 client.c -o client -g clean: rm server client
C Socket Programming server client
原文地址:http://blog.csdn.net/bluecloudmatrix/article/details/43490971