[gnio] Add command line argument to set port



commit 70f644828a086f2ef5ba843dfce4d0a56b0febb9
Author: Alexander Larsson <alexl redhat com>
Date:   Wed May 6 11:04:50 2009 +0200

    Add command line argument to set port
    
    This lets you pick what port to run this on. It also prints the port
    in use when starting.
---
 examples/server.c |   24 ++++++++++++++++++++++--
 1 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/examples/server.c b/examples/server.c
index 4fe0399..597b581 100644
--- a/examples/server.c
+++ b/examples/server.c
@@ -3,6 +3,14 @@
 
 #define MESSAGE "Welcome to the echo service!\n"
 
+int port = 7777;
+static GOptionEntry cmd_entries[] = {
+  {"port", 'p', 0, G_OPTION_ARG_INT, &port,
+   "Local port to bind to", NULL},
+  {NULL}
+};
+
+
 static gboolean
 handler (GThreadedSocketService *service,
          GSocketConnection      *connection,
@@ -31,21 +39,33 @@ handler (GThreadedSocketService *service,
 }
 
 int
-main (void)
+main (int argc, char *argv[])
 {
   GSocketService *service;
+  GOptionContext *context;
+  GError *error = NULL;
 
   g_type_init ();
   g_thread_init (NULL);
 
+  context = g_option_context_new (" - Test GSocket server stuff");
+  g_option_context_add_main_entries (context, cmd_entries, NULL);
+  if (!g_option_context_parse (context, &argc, &argv, &error))
+    {
+      g_printerr ("%s: %s\n", argv[0], error->message);
+      return 1;
+    }
+
   service = g_threaded_socket_service_new ();
 
   {
-    GSocketListener *tcp = g_tcp_listener_new (2223);
+    GSocketListener *tcp = g_tcp_listener_new (port);
     g_socket_service_add_listener (service, tcp);
     g_object_unref (tcp);
   }
 
+  g_print ("Echo service listening on port %d\n", port);
+
   g_signal_connect (service, "run", G_CALLBACK (handler), NULL);
 
   g_main_loop_run (g_main_loop_new (NULL, FALSE));



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]