📄 server.c
字号:
/* server.c This program is used for test net transfer speed for my at91rm9200 board. This program is runing in ARM , it send and receive data over earthnet 10/100M then print the transfer speed . It's a goold TCP SOCKET demo author: zou jian guo <zounix@126.com> Based on httpd.c in uCLinux kit create date: 2004-09-22 update date: 2004-11-02 *///define DEBUG#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <string.h>#include <sys/types.h>#include <sys/times.h>#include <sys/time.h>#include <sys/socket.h>#include <netinet/in.h>#include <errno.h>#include <sys/stat.h>#include <dirent.h>#include <signal.h>#include <unistd.h>#include <ctype.h>#include "pthread.h"//#define DEBUG#ifdef DEBUG#define DPRINTF(str...) printf (str)#else#define DPRINTF(str...) /* nothing */#endifint HandleConnect(int fd);void show_buf(char *buf,int len);char buf[4096];int fd;int MAX_LEN=4096;int QUIT=0;int KEY_QUIT=0;int TIMEOUT=3000;char referrer[128];int content_length;#define SERVER_PORT 80int fd_fifo;/////////////////////////////////////////////////////////////////////////int main(int argc, char **argv){ int listenfd,connfd; socklen_t len; volatile int true = 1; int count=0, send_len=0; struct sockaddr_in server_addr,client_addr; //char buf[1024]; if((listenfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) { perror("Unable to obtain network"); exit(1); } if((setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (void *)&true, sizeof(true))) == -1) { perror("setsockopt failed"); exit(1); } server_addr.sin_family = AF_INET; server_addr.sin_port = htons(SERVER_PORT); server_addr.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(listenfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) { perror("Unable to bind socket"); exit(1); } if(listen(listenfd, 8*3) == -1) { perror("Unable to listen"); exit(4); } printf("wait for connection.\n"); while (1) { len = sizeof(client_addr); if((connfd = accept(listenfd, (void *)&client_addr, &len)) == -1) { exit(5); close(connfd); } printf("get the client request\n"); for(;;){ if (send(connfd, buf, MAX_LEN, 0) == -1) break; count++; send_len +=MAX_LEN; if(count %100 ==0){ printf("%ld send data %8x MB\r\n",count,send_len>>10); } } close(connfd); //HandleConnect(connfd); }}/////////////////////////////////////////////////////////////////////////static double timeval_sub(struct timeval *x,struct timeval *y){ double result=(x->tv_sec - y->tv_sec)*1000000+(x->tv_usec - y->tv_usec); return result/1000000;}/////////////////////////////////////////////////////////////////////////int send_data(FILE *f){ struct timeval start,stop; double total_sec; unsigned int i=0,n,len=1024; unsigned int data_size=0; for(i=0;i<len; i++) buf[i]=i; gettimeofday(&start,NULL); while(1){ if(len<=0)break; n=fwrite(buf,1,len,f); data_size += n; //if(data_size >= 0x400000)break; }#if 1 gettimeofday(&stop,NULL); total_sec = timeval_sub(&stop,&start); printf("=======================================\r\n"); printf("send %dKB data OK!\n", data_size>>10); printf("Use time = %10.5f seconds!\n",total_sec); printf("SPEED = %10.4f KB/S\n",(data_size>>10)/total_sec); printf("=======================================\r\n");#endif }/////////////////////////////////////////////////////////////////////////int HandleConnect(int fd){ FILE *f; int fd_fifo; char buf[160]; char *answer; f = fdopen(fd,"a+"); if (!f) { printf("httpd: Unable to open httpd input fd, error %d\n", errno); alarm(TIMEOUT); close(fd); alarm(0); return 0; } setbuf(f, 0); printf("parse client request\n"); while ( fgets(buf, 150, f) && (strlen(buf) > 2)) { printf("do quest!\n"); alarm(TIMEOUT); #ifdef DEBUG printf("Got buf '%s'\n", buf); #endif if (!strncasecmp(buf, "##Start:", 8)) { printf("starting send data to client\n"); send_data(f); printf("send data completed!\n"); printf("======================================\n"); } alarm(0); if (ferror(f)) { fprintf(stderr, "Error continuing reading connection, error %d\n", errno); fclose(f); return 0; } alarm(TIMEOUT); fflush(f); fclose(f); alarm(0); } return 0;}/////////////////////////////////////////////////////////////////////////void show_buf(char *buf,int len){ int i,j=0; DPRINTF("buf is:\n"); for(i=0;i<len;i++){ if(i%16==0) printf("\n%4d:",j++); printf("%02x ",buf[i]); } printf("\n\r"); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -