]> hackdaworld.org Git - my-code/ivac.git/commitdiff
added udp_{send,rcv}.c
authorhackbard <hackbard>
Sun, 15 Jun 2003 14:37:41 +0000 (14:37 +0000)
committerhackbard <hackbard>
Sun, 15 Jun 2003 14:37:41 +0000 (14:37 +0000)
Makefile
udp_rcv.c [new file with mode: 0644]
udp_send.c [new file with mode: 0644]

index 432f72a3b9b1d643b05b8d6042ebda2937d2ebd8..e01e8a685c0b010fff61ced6262915283fd97962 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@ CFLAGS = -DDEBUG -O3 -Wall -I/usr/X11/include -I/usr/include/directfb -DVIDEO_DE
 LIBS = -L/usr/X11/lib -L/usr/lib/directfb-0.9.15 -lX11 -lXext -ldirectfb
 
 OBJS = minirgb.o webcam.o
-OBJS2 = stream receive datagram dgramrcv
+OBJS2 = stream receive datagram dgramrcv udp_rcv udp_send
 OBJS3 = dfb_api.o
 
 webcam: $(OBJS)
diff --git a/udp_rcv.c b/udp_rcv.c
new file mode 100644 (file)
index 0000000..1fa5fab
--- /dev/null
+++ b/udp_rcv.c
@@ -0,0 +1,76 @@
+/* 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>
+
+#define MAX_BUF 1472
+
+int main(int argc, char *argv[]) {
+  int receive_fd;
+  int opt;
+  struct sockaddr_in local_addr,target_addr;
+  int receive_bytes, write_bytes;
+  socklen_t target_addr_len;
+  unsigned char buf[MAX_BUF];
+
+  if(argc!=2) {
+    printf("usage: %s <port>\n",argv[0]);
+    exit(1);
+  }
+
+  if((receive_fd=socket(AF_INET,SOCK_DGRAM,0)) == -1) {
+    printf("can't open socket.\n");
+    exit(1);
+  }
+
+  opt=1;
+  setsockopt(receive_fd,SOL_SOCKET,SO_BROADCAST,&opt,sizeof(opt));
+
+  local_addr.sin_family=AF_INET;
+  local_addr.sin_port=htons(atoi(argv[1]));
+  local_addr.sin_addr.s_addr=htonl(INADDR_ANY);
+  bind(receive_fd,(const struct sockaddr *)&local_addr,sizeof(local_addr));
+  memset(&target_addr,0,sizeof(target_addr));
+ /*
+  target_addr.sin_family=AF_INET;
+  target_addr.sin_port=htons(atoi(argv[1]));
+  target_addr.sin_addr.s_addr=inet_addr(argv[1]);
+  target_addr_len=sizeof(target_addr);
+ */
+
+  receive_bytes=1;
+  while(receive_bytes>0) {
+       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;
+}
diff --git a/udp_send.c b/udp_send.c
new file mode 100644 (file)
index 0000000..b81eb53
--- /dev/null
@@ -0,0 +1,90 @@
+/* datagram.c - datagram sockets 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>
+
+/* open */
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#define MAX_BUF 1472
+
+int main(int argc, char *argv[]) {
+  int send_fd,broadcast_on;
+  struct sockaddr_in local_addr, remote_addr;
+  socklen_t remote_addr_len,optlen;
+  int send_bytes, read_bytes;
+  unsigned char buf[MAX_BUF];
+
+  if(argc!=3) {
+    printf("usage: %s <target-ip/broadcast-addr> <port>\n",argv[0]);
+    exit(1);
+  }
+
+  if((send_fd=socket(AF_INET,SOCK_DGRAM,0)) == -1) {
+    printf("can't open socket.\n");
+    exit(1);
+  }
+
+  broadcast_on=1;
+  optlen=sizeof(broadcast_on);
+  if((setsockopt(send_fd,SOL_SOCKET,SO_BROADCAST,&broadcast_on,optlen))==-1)
+    perror("setsockopt");
+  
+  memset(&local_addr,0,sizeof(local_addr));
+  local_addr.sin_family=AF_INET;
+  local_addr.sin_port=htons(atoi(argv[2]));
+  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[2]));
+    perror("bind");
+    exit(1);
+  }
+
+  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]);
+
+  /* send stuff .... */
+  read_bytes=1;
+  while(read_bytes>0) {
+    read_bytes=read(0,buf,sizeof(buf));
+    send_bytes=sendto(send_fd,buf,read_bytes,0,(struct sockaddr *)&remote_addr,remote_addr_len);
+#ifdef DEBUG
+    perror("sendto");
+#endif
+  }
+
+  close(send_fd);
+  printf("connection closed ...\n");
+  printf("%d from %d total bytes sent.\n",send_bytes,read_bytes);
+
+  return 0;
+}