[glibmm] Added a simple example showing how to resolve an internet address from a hostname



commit e01812192846a4e4312271302b34e19e7d75fe0b
Author: Jonathon Jongsma <jonathon quotidian org>
Date:   Thu Jul 16 23:47:44 2009 -0500

    Added a simple example showing how to resolve an internet address from a hostname

 .gitignore                      |    1 +
 ChangeLog                       |    9 +++++++
 configure.ac                    |    1 +
 examples/Makefile.am            |    2 +-
 examples/network_io/Makefile.am |    8 ++++++
 examples/network_io/resolve.cc  |   51 +++++++++++++++++++++++++++++++++++++++
 6 files changed, 71 insertions(+), 1 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index 4cf122f..60f5c28 100644
--- a/.gitignore
+++ b/.gitignore
@@ -51,6 +51,7 @@ giommconfig.h
 /examples/thread/dispatcher2
 /examples/thread/thread
 /examples/thread/threadpool
+/examples/network_io/resolve
 
 # gio/
 /gio/giomm-2.4.pc
diff --git a/ChangeLog b/ChangeLog
index 59914b8..b7bf2dc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
 2009-07-16  Jonathon Jongsma  <jonathon jongsma collabora co uk>
 
+	* .gitignore:
+	* configure.ac:
+	* examples/Makefile.am:
+	* examples/network_io/Makefile.am:
+	* examples/network_io/resolve.cc: Added a simple example showing how
+	  to resolve an internet address from a hostname
+
+2009-07-16  Jonathon Jongsma  <jonathon jongsma collabora co uk>
+
 	* gio/src/resolver.ccg:
 	* gio/src/resolver.hg: add async versions of Resolver methods
 
diff --git a/configure.ac b/configure.ac
index 2773f60..a000415 100644
--- a/configure.ac
+++ b/configure.ac
@@ -327,6 +327,7 @@ AC_CONFIG_FILES([
     examples/iochannel_stream/Makefile
     examples/child_watch/Makefile
     examples/regex/Makefile
+    examples/network_io/Makefile
 
   scripts/Makefile
 
diff --git a/examples/Makefile.am b/examples/Makefile.am
index ccc9014..46fcd26 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -1,4 +1,4 @@
-example_dirs = child_watch compose iochannel_stream markup options properties regex thread keyfile
+example_dirs = child_watch compose iochannel_stream markup options properties regex thread keyfile network_io
 
 # These use gtkmm stuff:
 # thread
diff --git a/examples/network_io/Makefile.am b/examples/network_io/Makefile.am
new file mode 100644
index 0000000..af7b01f
--- /dev/null
+++ b/examples/network_io/Makefile.am
@@ -0,0 +1,8 @@
+include $(top_srcdir)/examples/Makefile.am_fragment
+
+EXTRA_DIST =
+
+#Build the executable, but don't install it.
+noinst_PROGRAMS = resolve
+resolve_SOURCES = resolve.cc
+
diff --git a/examples/network_io/resolve.cc b/examples/network_io/resolve.cc
new file mode 100644
index 0000000..fb89c94
--- /dev/null
+++ b/examples/network_io/resolve.cc
@@ -0,0 +1,51 @@
+/*******************************************************************************
+ *
+ *  Copyright (c) 2009 Jonathon Jongsma
+ *
+ *  This file is part of glibmm
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>
+ *
+ *******************************************************************************/
+#include <giomm.h>
+#include <list>
+#include <iostream>
+
+typedef std::list<Glib::RefPtr<Gio::InetAddress> > addr_list_t;
+int main(int argc, char** argv)
+{
+    if (argc <= 1) {
+        std::cerr << "Usage: " << argv[0] << " <hostname>" << std::endl;
+        return 1;
+    }
+
+    Gio::init ();
+
+    Glib::RefPtr<Gio::Resolver> resolver = Gio::Resolver::get_default ();
+    try {
+        // NOTE: in any real-world application you should probably use the
+        // _async() version.  here we use the sync version for simplicity
+        addr_list_t addresses =
+            resolver->lookup_by_name (argv[1]);
+
+        addr_list_t::iterator i, end = addresses.end ();
+        for (i = addresses.begin (); i != end; ++i)
+        {
+            std::cout << "Address Candidate: " << (*i)->to_string () << std::endl;
+        }
+    } catch (const Glib::Error& error)
+    {
+        std::cerr << "Unable to lookup hostname: " << error.what () << std::endl;
+    }
+}



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