samle server and client command line opts added
authorMarcel Poul <marcel.poul@cern.ch>
Mon, 14 Nov 2011 16:13:59 +0000 (16:13 +0000)
committerMarcel Poul <marcel.poul@cern.ch>
Mon, 14 Nov 2011 16:13:59 +0000 (16:13 +0000)
emi.canl.canl-c/src/canl_sample_client.c
emi.canl.canl-c/src/canl_sample_server.c

index 349569e..540ac76 100644 (file)
@@ -1,15 +1,42 @@
 #include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
 #include "canl.h"
 
 #define BUF_LEN 1000
 
-int main()
+int main(int argc, char *argv[])
 {
     canl_ctx my_ctx;
     canl_io_handler my_io_h;
     int err = 0;
     char *err_msg = NULL;
     char buf[BUF_LEN];
+    char *p_server = NULL;
+    char *def_server = "www.linuxfoundation.org";
+    int opt, port = 80;
+
+    while ((opt = getopt(argc, argv, "hp:s:")) != -1) {
+        switch (opt) {
+            case 'h':
+                fprintf(stderr, "Usage: %s [-p port]" 
+                        "[-s server] [-h] \n", argv[0]);
+                break;
+            case 'p':
+                port = atoi(optarg);
+                break;
+            case 's':
+                p_server = optarg;
+                break;
+            default: /* '?' */
+                fprintf(stderr, "Usage: %s [-p port]" 
+                        "[-s server] [-h] \n", argv[0]);
+                exit(-1);
+        }
+    }
+
+    if (!p_server)
+        p_server = def_server;
 
     my_ctx = canl_create_ctx();
     if (!my_ctx){
@@ -23,7 +50,7 @@ int main()
         goto end;
     }
 
-    err = canl_io_connect(my_ctx, my_io_h, "www.seznam.cz", 80, 0, NULL, NULL);
+    err = canl_io_connect(my_ctx, my_io_h, p_server, port, 0, NULL, NULL);
     if (err) {
         printf("connection cannot be established\n");
         goto end;
index 96ff765..44b60ad 100644 (file)
@@ -1,13 +1,30 @@
 #include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
 #include "canl.h"
 
-int main()
+int main(int argc, char *argv[])
 {
     canl_ctx my_ctx;
     canl_io_handler my_io_h;
     canl_io_handler my_new_io_h;
     int err = 0;
     char *err_msg = NULL;
+    int opt, port = 4321;
+
+    while ((opt = getopt(argc, argv, "hp:")) != -1) {
+        switch (opt) {
+            case 'h':
+                fprintf(stderr, "Usage: %s [-p port] [-h] \n", argv[0]);
+                break;
+            case 'p':
+                port = atoi(optarg);
+                break;
+            default: /* '?' */
+                fprintf(stderr, "Usage: %s [-p port] [-h] \n", argv[0]);
+                exit(-1);
+        }
+    }
 
     my_ctx = canl_create_ctx();
     if (!my_ctx){
@@ -20,7 +37,7 @@ int main()
         //set_error("io handler cannot be created\n");
         goto end;
     }
-    
+
     my_new_io_h = canl_create_io_handler(my_ctx);
     if (!my_new_io_h) {
         //set_error("io handler cannot be created\n");
@@ -28,7 +45,7 @@ int main()
     }
 
     /* canl_create_io_handler has to be called for my_new_io_h and my_io_h*/
-    err = canl_io_accept(my_ctx, my_io_h, 4321, 0, NULL, NULL, &my_new_io_h);
+    err = canl_io_accept(my_ctx, my_io_h, port, 0, NULL, NULL, &my_new_io_h);
     if (err) {
         //set_error("cannot make a connection");
         goto end;