...
authorhackbard <hackbard>
Wed, 16 Oct 2002 15:53:01 +0000 (15:53 +0000)
committerhackbard <hackbard>
Wed, 16 Oct 2002 15:53:01 +0000 (15:53 +0000)
hstream.c [new file with mode: 0644]

diff --git a/hstream.c b/hstream.c
new file mode 100644 (file)
index 0000000..201d6aa
--- /dev/null
+++ b/hstream.c
@@ -0,0 +1,80 @@
+/* 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>
+
+/* read, close ;) */
+#include <unistd.h>
+
+/* errno stuff ... */
+#include <errno.h>
+
+int main(int argc, char *argv[]) {
+  int listen_fd, send_fd;
+  socklen_t len;
+  struct sockaddr_in local_addr, remote_addr;
+  int send_bytes, read_bytes;
+
+  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);
+  //memset(&(local_addr.sin_zero),'\0',8);
+
+  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);
+  }
+  len=sizeof(remote_addr);
+  if( (send_fd=accept(listen_fd,(struct sockaddr *)&remote_addr,&len)) <0) {
+    printf("accepting connection from %s port %d.\n",
+    inet_ntoa(remote_addr.sin_addr),
+    ntohs(remote_addr.sin_port));
+
+      /* send stuff .... */
+      read_bytes=1;
+      while(read_bytes>0) {
+       unsigned char buf[1000];
+       read_bytes=read(0,buf,sizeof(buf));
+        send_bytes=send(send_fd,buf,sizeof(buf),0);
+      }
+
+      close(send_fd);
+      printf("connection closed ...\n");
+      printf("%d from %d total bytes sent.\n",send_bytes,read_bytes);
+  }
+  return 0;
+}