]> hackdaworld.org Git - my-code/ivac.git/commitdiff
version 1 from stream.c and receive.c
authorhackbard <hackbard>
Wed, 16 Oct 2002 21:17:12 +0000 (21:17 +0000)
committerhackbard <hackbard>
Wed, 16 Oct 2002 21:17:12 +0000 (21:17 +0000)
receive.c [new file with mode: 0644]
stream.c
tutorials/hcat.c [deleted file]

diff --git a/receive.c b/receive.c
new file mode 100644 (file)
index 0000000..ba61d5d
--- /dev/null
+++ b/receive.c
@@ -0,0 +1,70 @@
+/* receive.c - receive from 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>
+
+int main(int argc, char *argv[]) {
+  int receive_fd;
+  struct sockaddr_in target_addr;
+  socklen_t target_addr_len;
+  int receive_bytes, write_bytes;
+
+  if(argc!=3) {
+    printf("usage: %s <port>\n",argv[0]);
+    exit(1);
+  }
+
+  if((receive_fd=socket(AF_INET,SOCK_STREAM,0)) == -1) {
+    printf("can't open socket.\n");
+    exit(1);
+  }
+  memset(&target_addr,0,sizeof(target_addr));
+  target_addr.sin_family=AF_INET;
+  target_addr.sin_port=htons(atoi(argv[2]));
+  target_addr.sin_addr.s_addr=inet_addr(argv[1]);
+
+  if(connect(receive_fd,(struct sockaddr *)&target_addr,sizeof(target_addr))==-1) {
+  printf("unable to connect.\n");
+  perror("connect");
+  exit(1);
+  }
+
+  printf("connected.\n");
+
+  receive_bytes=1;
+  while(receive_bytes>0) {
+       unsigned char buf[1000];
+       receive_bytes=recv(receive_fd,buf,sizeof(buf),0);
+        write_bytes=write(1,buf,sizeof(buf));
+  }
+
+  close(receive_fd);
+  printf("connection closed ...\n");
+  printf("%d from %d total bytes written.\n",write_bytes,receive_bytes);
+  
+  return 0;
+}
index 09916112b727109b70e1f68c354d82ccabb15bb5..da6cc47a1f0eb92ad5e110ca576c6156b7f0a8c4 100644 (file)
--- a/stream.c
+++ b/stream.c
 /* errno stuff ... */
 #include <errno.h>
 
+/* read, close */
+#include <unistd.h>
+
 int main(int argc, char *argv[]) {
   int listen_fd, send_fd;
-  struct sockaddr_in local_addr, *remote_addr;
+  struct sockaddr_in local_addr, remote_addr;
+  socklen_t remote_addr_len;
   int send_bytes, read_bytes;
 
   if(argc!=2) {
@@ -33,18 +37,17 @@ int main(int argc, char *argv[]) {
     exit(1);
   }
 
-  if(listen_fd=socket(AF_INET,SOCK_STREAM,0) == -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_port=htons(1025);
+  local_addr.sin_port=htons(atoi(argv[1]));
   local_addr.sin_addr.s_addr=htonl(INADDR_ANY);
-  memset((void *)&(local_addr.sin_zero),'\0',8);
 
-  if(bind(listen_fd,&local_addr,sizeof(struct sockaddr))==-1) {
+  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);
@@ -55,17 +58,18 @@ int main(int argc, char *argv[]) {
     exit(1);
   }
 
-  if(send_fd=accept(listen_fd,(struct sockaddr *)remote_addr,
-  sizeof(struct sockaddr_in))!=-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));
+    inet_ntoa(remote_addr.sin_addr),
+    ntohs(remote_addr.sin_port));
 
       /* send stuff .... */
       read_bytes=1;
       while(read_bytes>0) {
-       char buf[1000];
-       read_bytes=read(stdin,(void *)buf,sizeof(buf));
+       unsigned char buf[1000];
+       read_bytes=read(0,buf,sizeof(buf));
         send_bytes=send(send_fd,buf,sizeof(buf),0);
       }
 
diff --git a/tutorials/hcat.c b/tutorials/hcat.c
deleted file mode 100644 (file)
index da8c006..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <sys/ioctl.h>
-#include <sys/stat.h>
-#include <sys/wait.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <signal.h>
-#include <stdio.h>
-
-int main(int argc, char **argv) {
-  int listenfd, connfd,rres;
-  socklen_t clilen;
-  struct sockaddr_in cliaddr, servaddr;
-  unsigned char foo[1500-12-12];
-
-  listenfd = socket(AF_INET, SOCK_STREAM, 0);
-
-  memset(&servaddr, 0, sizeof(servaddr));
-  servaddr.sin_family = AF_INET;
-  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
-  servaddr.sin_port = htons(2350);
-
-  bind(listenfd, &servaddr, sizeof(servaddr));
-
-  listen(listenfd, 1);
-  
-  clilen = sizeof(cliaddr);
-  connfd = accept(listenfd, &cliaddr, &clilen);
-  fprintf(stderr,"connected\n");
-
-
-  rres=1;
-  while(rres>0) {
-    rres=read(connfd,foo,sizeof(foo));
-    fprintf(stderr,"%d bytes read\n",rres);
-    write(1,foo,rres);
-  }
-   
-  return 0;
-}