buflen = 1024 bytes + added total and kb/s output for stream
[my-code/ivac.git] / stream.c
1 /* stream.c - streaming server
2  *
3  * author: hackbard
4  *
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 /* memset */
11 #include <string.h>
12
13 /* socket and bind stuff */
14 #include <sys/types.h>
15 #include <sys/socket.h>
16
17 /* sockkaddr_in */
18 #include <netinet/in.h>
19
20 /* inet_ntoa ... */
21 #include <arpa/inet.h>
22
23 /* errno stuff ... */
24 #include <errno.h>
25
26 /* read, close */
27 #include <unistd.h>
28
29 /* timing stuff */
30 #include <sys/time.h>
31
32 /* defines ... */
33 #define MAX_SIZE 1024
34 #define PRINT_RATE 100
35
36 int print_rate(struct timeval *time_start,int t) {
37   struct timeval now;
38   int sec_t,usec_t;
39   unsigned int delta_t;
40   unsigned int kbs_t;
41   int count;
42
43   gettimeofday(&now,NULL);
44   sec_t=now.tv_sec-time_start->tv_sec;
45   usec_t=(now.tv_usec<time_start->tv_usec)
46     ?1000000-time_start->tv_usec+now.tv_usec
47     :now.tv_usec-time_start->tv_usec;
48   delta_t=sec_t*1000000+usec_t;
49   kbs_t=(t/delta_t)*(1000000/1024);
50   count=printf("total: %d MByte - average: %d kB/s",t/(1024*1024),kbs_t);
51   while(count--) printf("\b");
52
53   return 1;
54 }
55
56 int main(int argc, char *argv[]) {
57   int listen_fd, send_fd;
58   struct sockaddr_in local_addr, remote_addr;
59   socklen_t remote_addr_len;
60   int send_bytes, read_bytes, total_read=0, total_send=0;
61   struct timeval time_start;
62   int i=0;
63
64   if(argc!=2) {
65     printf("usage: %s <port>\n",argv[0]);
66     exit(1);
67   }
68
69   if((listen_fd=socket(AF_INET,SOCK_STREAM,0)) == -1) {
70     printf("can't open socket.\n");
71     exit(1);
72   }
73  
74   memset(&local_addr,0,sizeof(local_addr));
75   local_addr.sin_family=AF_INET;
76   local_addr.sin_port=htons(atoi(argv[1]));
77   local_addr.sin_addr.s_addr=htonl(INADDR_ANY);
78
79   if(bind(listen_fd,(struct sockaddr *)&local_addr,sizeof(local_addr))==-1) {
80     printf("unable to bind on port %d.\n",atoi(argv[1]));
81     perror("bind");
82     exit(1);
83   }
84
85   if(listen(listen_fd,1)==-1) {
86     printf("error listening on port %d.\n",atoi(argv[1]));
87     exit(1);
88   }
89
90   remote_addr_len=sizeof(remote_addr); 
91   if((send_fd=accept(listen_fd,(struct sockaddr *)&remote_addr,
92   &remote_addr_len))!=-1) {
93     printf("accepting connection from %s port %d.\n",
94     inet_ntoa(remote_addr.sin_addr),
95     ntohs(remote_addr.sin_port));
96
97     /* time init */
98     gettimeofday(&time_start,NULL);
99
100     /* send stuff .... */
101     read_bytes=1;
102     while(read_bytes>0) {
103       unsigned char buf[MAX_SIZE];
104
105       read_bytes=read(0,buf,sizeof(buf));
106       total_read+=read_bytes;
107       send_bytes=send(send_fd,buf,read_bytes,0);
108       total_send+=send_bytes;
109       if(!((i++)%PRINT_RATE)) print_rate(&time_start,total_send);
110     }
111
112       close(send_fd);
113       close(listen_fd);
114       printf("connection closed ...\n");
115       printf("%d from %d total bytes sent.\n",total_send,total_read);
116   }
117   return 0;
118 }