summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJaromil <jaromil@dyne.org>2008-06-30 10:12:22 (GMT)
committer Jaromil <jaromil@dyne.org>2008-09-09 13:51:07 (GMT)
commitee4ebf12ec863bfbb31d62383058cc97e5f8ad35 (patch)
treeae320f87e6bd7680347836f787adb38cad9605d1
parentcfa8deed064b4a615c661fcd6ce8702c59a076be (diff)
test programs for udp server
(cherry picked from commit 19d858713e7ff2b8ac0bdada679c9667c00625f0)
-rw-r--r--Makefile8
-rw-r--r--udpbroadcast.cpp66
-rw-r--r--udptest.cpp27
3 files changed, 100 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index e35e7b2..86d556e 100644
--- a/Makefile
+++ b/Makefile
@@ -23,11 +23,17 @@ LIBS = -lpthread
IVYSYNC_OBJ = decoder.o thread.o linklist.o utils.o cmdline.o gui.o udpliteserver.o
-all: ivysync
+all: ivysync udptest udpbroadcast
ivysync: $(IVYSYNC_OBJ)
$(CPP) $(CPPFLAGS) -o ivysync $(IVYSYNC_OBJ) $(LIBS) $(GTKLIBS)
+udptest: udpliteserver.o utils.o udptest.o thread.o
+ $(CPP) $(CPPFLAGS) -o udptest udpliteserver.o utils.o udptest.o thread.o -lpthread
+
+udpbroadcast: udpbroadcast.o
+ $(CPP) $(CPPFLAGS) -o udpbroadcast udpbroadcast.o
+
#make clean
clean:
rm -rf *.o *~ ivysync
diff --git a/udpbroadcast.cpp b/udpbroadcast.cpp
new file mode 100644
index 0000000..742d46b
--- /dev/null
+++ b/udpbroadcast.cpp
@@ -0,0 +1,66 @@
+/*
+** broadcaster.c -- a datagram "client" like talker.c, except
+** this one can broadcast
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+
+#define SERVERPORT 15500 // the port users will be connecting to
+
+int main(int argc, char *argv[])
+{
+ int sockfd;
+ struct sockaddr_in their_addr; // connector's address information
+ struct hostent *he;
+ int numbytes;
+ int broadcast = 1;
+ //char broadcast = '1'; // if that doesn't work, try this
+
+ if (argc != 3) {
+ fprintf(stderr,"usage: broadcaster hostname message\n");
+ exit(1);
+ }
+
+ if ((he=gethostbyname(argv[1])) == NULL) { // get the host info
+ perror("gethostbyname");
+ exit(1);
+ }
+
+ if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
+ perror("socket");
+ exit(1);
+ }
+
+ // this call is the difference between this program and talker.c:
+ if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &broadcast,
+ sizeof broadcast) == -1) {
+ perror("setsockopt (SO_BROADCAST)");
+ exit(1);
+ }
+
+ their_addr.sin_family = AF_INET; // host byte order
+ their_addr.sin_port = htons(SERVERPORT); // short, network byte order
+ their_addr.sin_addr = *((struct in_addr *)he->h_addr);
+ memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);
+
+ if ((numbytes=sendto(sockfd, argv[2], strlen(argv[2]), 0,
+ (struct sockaddr *)&their_addr, sizeof their_addr)) == -1) {
+ perror("sendto");
+ exit(1);
+ }
+
+ printf("sent %d bytes to %s\n", numbytes, inet_ntoa(their_addr.sin_addr));
+
+ close(sockfd);
+
+ return 0;
+}
diff --git a/udptest.cpp b/udptest.cpp
new file mode 100644
index 0000000..61a09b4
--- /dev/null
+++ b/udptest.cpp
@@ -0,0 +1,27 @@
+
+
+#include <stdio.h>
+
+#include <stdlib.h> /* exit() */
+#include <unistd.h> /* close() */
+#include <errno.h> /* strerror() */
+#include <string.h> /* memset() */
+
+#include <utils.h>
+#include <udpliteserver.h>
+
+// UDP lite server
+UdpLiteSrv *udpsrv;
+
+bool synctest;
+
+int main(int argc, char **argv) {
+
+ udpsrv = new UdpLiteSrv();
+ udpsrv->init(&synctest);
+ // udpsrv->launch();
+ udpsrv->run();
+
+
+ return 1;
+}