diff resources/udp-server.c @ 24:ad5cd8dd99b3 bbb_network

UDP communication in place, pre-alpha
author Giulio Moro <giuliomoro@yahoo.it>
date Fri, 08 May 2015 11:12:13 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/resources/udp-server.c	Fri May 08 11:12:13 2015 +0100
@@ -0,0 +1,54 @@
+/* Creates a datagram server.  The port 
+   number is passed as an argument.  This
+   server runs forever */
+
+#include <sys/types.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <string.h>
+#include <netdb.h>
+#include <stdio.h>
+
+void error(const char *msg)
+{
+    perror(msg);
+    exit(0);
+}
+
+int main(int argc, char *argv[])
+{
+   int sock, length, n;
+   socklen_t fromlen;
+   struct sockaddr_in server;
+   struct sockaddr_in from;
+   char buf[1024];
+
+   if (argc < 2) {
+      fprintf(stderr, "ERROR, no port provided\n");
+      exit(0);
+   }
+   
+   sock=socket(AF_INET, SOCK_DGRAM, 0);
+   if (sock < 0) error("Opening socket");
+   length = sizeof(server);
+   bzero(&server,length);
+   server.sin_family=AF_INET;
+   server.sin_addr.s_addr=INADDR_ANY;
+   server.sin_port=htons(atoi(argv[1]));
+   if (bind(sock,(struct sockaddr *)&server,length)<0) 
+       error("binding");
+   fromlen = sizeof(struct sockaddr_in);
+   while (1) {
+       n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from,&fromlen);
+       if (n < 0) error("recvfrom");
+       write(1,"Received a datagram: ",21);
+       write(1,buf,n);
+       n = sendto(sock,"Got your message\n",17,
+                  0,(struct sockaddr *)&from,fromlen);
+       if (n  < 0) error("sendto");
+   }
+   return 0;
+ }
+