From d333f09513d243b67eb0ab5d5272461184f1e5a8 Mon Sep 17 00:00:00 2001 From: hackbard Date: Mon, 2 Feb 2004 02:24:04 +0000 Subject: [PATCH] buflen = 1024 bytes + added total and kb/s output for stream --- receive.c | 2 +- stream.c | 53 +++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/receive.c b/receive.c index 5c6e7dd..9fe86e2 100644 --- a/receive.c +++ b/receive.c @@ -27,7 +27,7 @@ #include /* defines ... */ -#define MAX_SIZE 1000 +#define MAX_SIZE 1024 int main(int argc, char *argv[]) { int receive_fd; diff --git a/stream.c b/stream.c index b97a670..aa8c52a 100644 --- a/stream.c +++ b/stream.c @@ -26,14 +26,40 @@ /* read, close */ #include +/* timing stuff */ +#include + /* defines ... */ -#define MAX_SIZE 1000 +#define MAX_SIZE 1024 +#define PRINT_RATE 100 + +int print_rate(struct timeval *time_start,int t) { + struct timeval now; + int sec_t,usec_t; + unsigned int delta_t; + unsigned int kbs_t; + int count; + + gettimeofday(&now,NULL); + sec_t=now.tv_sec-time_start->tv_sec; + usec_t=(now.tv_usectv_usec) + ?1000000-time_start->tv_usec+now.tv_usec + :now.tv_usec-time_start->tv_usec; + delta_t=sec_t*1000000+usec_t; + kbs_t=(t/delta_t)*(1000000/1024); + count=printf("total: %d MByte - average: %d kB/s",t/(1024*1024),kbs_t); + while(count--) printf("\b"); + + return 1; +} int main(int argc, char *argv[]) { int listen_fd, send_fd; struct sockaddr_in local_addr, remote_addr; socklen_t remote_addr_len; - int send_bytes, read_bytes; + int send_bytes, read_bytes, total_read=0, total_send=0; + struct timeval time_start; + int i=0; if(argc!=2) { printf("usage: %s \n",argv[0]); @@ -68,18 +94,25 @@ int main(int argc, char *argv[]) { inet_ntoa(remote_addr.sin_addr), ntohs(remote_addr.sin_port)); - /* send stuff .... */ - read_bytes=1; - while(read_bytes>0) { - unsigned char buf[MAX_SIZE]; - read_bytes=read(0,buf,sizeof(buf)); - send_bytes=send(send_fd,buf,read_bytes,0); - } + /* time init */ + gettimeofday(&time_start,NULL); + + /* send stuff .... */ + read_bytes=1; + while(read_bytes>0) { + unsigned char buf[MAX_SIZE]; + + read_bytes=read(0,buf,sizeof(buf)); + total_read+=read_bytes; + send_bytes=send(send_fd,buf,read_bytes,0); + total_send+=send_bytes; + if(!((i++)%PRINT_RATE)) print_rate(&time_start,total_send); + } close(send_fd); close(listen_fd); printf("connection closed ...\n"); - printf("%d from %d total bytes sent.\n",send_bytes,read_bytes); + printf("%d from %d total bytes sent.\n",total_send,total_read); } return 0; } -- 2.20.1