cleanup (-> old directory) & creation of 'new' src folder + TODO
[my-code/ivac.git] / old / stream.c
diff --git a/old/stream.c b/old/stream.c
new file mode 100644 (file)
index 0000000..07439a5
--- /dev/null
@@ -0,0 +1,119 @@
+/* stream.c - streaming server
+ *
+ * author: hackbard
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+/* memset */
+#include <string.h>
+
+/* socket and bind stuff */
+#include <sys/types.h>
+#include <sys/socket.h>
+
+/* sockkaddr_in */
+#include <netinet/in.h>
+
+/* inet_ntoa ... */
+#include <arpa/inet.h>
+
+/* errno stuff ... */
+#include <errno.h>
+
+/* read, close */
+#include <unistd.h>
+
+/* timing stuff */
+#include <sys/time.h>
+
+/* defines ... */
+#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_usec<time_start->tv_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, total_read=0, total_send=0;
+  struct timeval time_start;
+  int i=0;
+
+  if(argc!=2) {
+    printf("usage: %s <port>\n",argv[0]);
+    exit(1);
+  }
+
+  if((listen_fd=socket(AF_INET,SOCK_STREAM,0)) == -1) {
+    printf("can't open socket.\n");
+    exit(1);
+  }
+  memset(&local_addr,0,sizeof(local_addr));
+  local_addr.sin_family=AF_INET;
+  local_addr.sin_port=htons(atoi(argv[1]));
+  local_addr.sin_addr.s_addr=htonl(INADDR_ANY);
+
+  if(bind(listen_fd,(struct sockaddr *)&local_addr,sizeof(local_addr))==-1) {
+    printf("unable to bind on port %d.\n",atoi(argv[1]));
+    perror("bind");
+    exit(1);
+  }
+
+  if(listen(listen_fd,1)==-1) {
+    printf("error listening on port %d.\n",atoi(argv[1]));
+    exit(1);
+  }
+
+  remote_addr_len=sizeof(remote_addr); 
+  if((send_fd=accept(listen_fd,(struct sockaddr *)&remote_addr,
+  &remote_addr_len))!=-1) {
+    printf("accepting connection from %s port %d.\n",
+    inet_ntoa(remote_addr.sin_addr),
+    ntohs(remote_addr.sin_port));
+
+    /* 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",total_send,total_read);
+  }
+  return 0;
+}