]> jspc29.x-matter.uni-frankfurt.de Git - trbnettools.git/commitdiff
TRB3 simulator added
authorhadaq <hadaq>
Wed, 18 Jul 2012 22:13:51 +0000 (22:13 +0000)
committerhadaq <hadaq>
Wed, 18 Jul 2012 22:13:51 +0000 (22:13 +0000)
libtrbnet/trb3sim.c [new file with mode: 0644]

diff --git a/libtrbnet/trb3sim.c b/libtrbnet/trb3sim.c
new file mode 100644 (file)
index 0000000..371d110
--- /dev/null
@@ -0,0 +1,76 @@
+/* upd-server.c
+ *
+ * Copyright (c) 2000 Sean Walton and Macmillan Publishers.  Use may be in
+ * whole or in part in accordance to the General Public License (GPL).
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*****************************************************************************/
+/*** upd-server.c                                                          ***/
+/***                                                                       ***/
+/*** Create a datagram server that waits for client messages (which it     ***/
+/*** echoes back).                                                         ***/
+/*****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <resolv.h>
+#include <string.h>
+
+#define DEFAULT_PORT   25000
+
+int main(int count, char *strings[])
+{
+  int sd;
+  int port = DEFAULT_PORT;
+  struct sockaddr_in addr;
+  uint16_t buffer[32768];
+
+  if (count != 2)
+    printf("usage: %s <port>\n...Using default port (%d).\n", strings[0],
+           port);
+  else
+    port = atoi(strings[1]);
+  
+  sd = socket(PF_INET, SOCK_DGRAM, 0);
+  memset(&addr, 0, sizeof(addr));
+  addr.sin_family = AF_INET;
+  addr.sin_port = htons(port);
+  addr.sin_addr.s_addr = INADDR_ANY;
+  if (bind(sd, (struct sockaddr *)&addr, sizeof(addr)) != 0)
+    perror("bind");
+  while (1) {
+    int bytes; 
+    socklen_t addr_len = sizeof(addr);
+    int i;
+    
+    bytes =
+      recvfrom(sd, (void*)buffer, sizeof(buffer), 0, (struct sockaddr *)&addr,
+               &addr_len);
+    printf("msg from %s:%d (%d bytes)\n", inet_ntoa(addr.sin_addr),
+           ntohs(addr.sin_port), bytes);
+    for (i = 0; i < bytes / 2; i++) {
+      fprintf(stderr, "%d  0x%04x\n", i, buffer[i]);
+    }
+       sleep(6);
+    sendto(sd, buffer, bytes, 0, (struct sockaddr *)&addr, sizeof(addr));
+       fprintf(stderr, "sendto done\n");
+  }
+  close(sd);
+}