]> hackdaworld.org Git - my-code/ivac.git/commitdiff
fixed send(from) recv(from) bug, added dgramrcv.c
authorhackbard <hackbard>
Fri, 18 Oct 2002 14:08:04 +0000 (14:08 +0000)
committerhackbard <hackbard>
Fri, 18 Oct 2002 14:08:04 +0000 (14:08 +0000)
Makefile
datagram.c
dgramrcv.c [new file with mode: 0644]
receive.c
stream.c

index 2b51ba5495f13a6b22e437e7d3e7a2a069cfdd49..8076296e97b5ac3f9137e9ca26e4d2d661797baf 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ INCLUDEDIR = /usr/include
 
 CFLAGS = -O3 -Wall
 
-OBJS = stream receive datagram
+OBJS = stream receive datagram dgramrcv
 
 all: $(OBJS)
 
index 83c4bc8d13674e8768c8a0709a3113b262d2bb61..72179b719b1d4ef707b8f0c44ecd3d348bbb87d6 100644 (file)
 /* read, close */
 #include <unistd.h>
 
+/* open */
+#include <sys/stat.h>
+#include <fcntl.h>
+
 int main(int argc, char *argv[]) {
-  int send_fd;
+  int send_fd,cmd_fd;
   struct sockaddr_in local_addr, remote_addr;
-  socklen_t remote_addr_len;
+  socklen_t remote_addr_len,optlen;
   int send_bytes, read_bytes;
 
-  if(argc!=3) {
-    printf("usage: %s <target-ip/broadcast-addr> <port>\n",argv[0]);
+  if(argc!=4) {
+    printf("usage: %s \"cmd command\" <target-ip/broadcast-addr> <port>\n",argv[0]);
     exit(1);
   }
 
@@ -42,15 +46,16 @@ int main(int argc, char *argv[]) {
     exit(1);
   }
 
-  // setsockopt(send_fd,SOL_SOCKET,SO_BROADCAST,
+  optlen=1;
+  setsockopt(send_fd,SOL_SOCKET,SO_BROADCAST,NULL,optlen);
  
   memset(&local_addr,0,sizeof(local_addr));
   local_addr.sin_family=AF_INET;
-  local_addr.sin_port=htons(atoi(argv[2]));
+  local_addr.sin_port=htons(atoi(argv[3]));
   local_addr.sin_addr.s_addr=htonl(INADDR_ANY);
 
   if(bind(send_fd,(struct sockaddr *)&local_addr,sizeof(local_addr))==-1) {
-    printf("unable to bind on port %d.\n",atoi(argv[1]));
+    printf("unable to bind on port %d.\n",atoi(argv[3]));
     perror("bind");
     exit(1);
   }
@@ -58,15 +63,20 @@ int main(int argc, char *argv[]) {
   remote_addr_len=sizeof(remote_addr); 
   memset(&remote_addr,0,sizeof(remote_addr));
   remote_addr.sin_family=AF_INET;
-  remote_addr.sin_port=htons(atoi(argv[2]));
-  remote_addr.sin_addr.s_addr=inet_addr(argv[1]);
+  remote_addr.sin_port=htons(atoi(argv[3]));
+  remote_addr.sin_addr.s_addr=inet_addr(argv[2]);
+
+  if((cmd_fd=open(argv[1],O_RDONLY))<0) {
+    printf("unable to open file descriptor for %s.\n",argv[1]);
+    exit(1);
+  }
 
   /* send stuff .... */
   read_bytes=1;
   while(read_bytes>0) {
     unsigned char buf[1000];
-    read_bytes=read(0,buf,sizeof(buf));
-    send_bytes=sendto(send_fd,buf,sizeof(buf),0,(struct sockaddr *)&remote_addr,remote_addr_len);
+    read_bytes=read(cmd_fd,buf,sizeof(buf));
+    send_bytes=sendto(send_fd,buf,read_bytes,0,(struct sockaddr *)&remote_addr,remote_addr_len);
   }
 
   close(send_fd);
diff --git a/dgramrcv.c b/dgramrcv.c
new file mode 100644 (file)
index 0000000..060fd4a
--- /dev/null
@@ -0,0 +1,66 @@
+/* dgramrcv.c - receive from udp 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;
+  int receive_bytes, write_bytes;
+  socklen_t target_addr_len,optlen;
+
+  if(argc!=3) {
+    printf("usage: %s <target-ip> <port>\n",argv[0]);
+    exit(1);
+  }
+
+  if((receive_fd=socket(AF_INET,SOCK_DGRAM,0)) == -1) {
+    printf("can't open socket.\n");
+    exit(1);
+  }
+
+  optlen=1;
+  setsockopt(receive_fd,SOL_SOCKET,SO_BROADCAST,NULL,optlen);
+  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]);
+  target_addr_len=sizeof(target_addr);
+
+  receive_bytes=1;
+  while(receive_bytes>0) {
+       unsigned char buf[1000];
+       receive_bytes=recvfrom(receive_fd,buf,sizeof(buf),0,(struct sockaddr *)&target_addr,&target_addr_len);
+        write_bytes=write(1,buf,receive_bytes);
+  }
+
+  close(receive_fd);
+  printf("connection closed ...\n");
+  printf("%d from %d total bytes written.\n",write_bytes,receive_bytes);
+  
+  return 0;
+}
index 36f8ffd770de4a8f33a9ac85b57ceddceb1fa34f..65b9b95fde7352eec4661bd1cdf4e74e821fcfdd 100644 (file)
--- a/receive.c
+++ b/receive.c
@@ -32,7 +32,7 @@ int main(int argc, char *argv[]) {
   int receive_bytes, write_bytes;
 
   if(argc!=3) {
-    printf("usage: %s <port>\n",argv[0]);
+    printf("usage: %s <target-ip> <port>\n",argv[0]);
     exit(1);
   }
 
@@ -58,7 +58,7 @@ int main(int argc, char *argv[]) {
   while(receive_bytes>0) {
        unsigned char buf[1000];
        receive_bytes=recv(receive_fd,buf,sizeof(buf),0);
-        write_bytes=write(1,buf,sizeof(buf));
+        write_bytes=write(1,buf,receive_bytes);
   }
 
   close(receive_fd);
index da6cc47a1f0eb92ad5e110ca576c6156b7f0a8c4..834bf4784dfce47f30c9ae858388729b8f862513 100644 (file)
--- a/stream.c
+++ b/stream.c
@@ -70,7 +70,7 @@ int main(int argc, char *argv[]) {
       while(read_bytes>0) {
        unsigned char buf[1000];
        read_bytes=read(0,buf,sizeof(buf));
-        send_bytes=send(send_fd,buf,sizeof(buf),0);
+        send_bytes=send(send_fd,buf,read_bytes,0);
       }
 
       close(send_fd);