summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorJonathon Jongsma <jonathon@quotidian.org>2009-07-16 23:47:44 -0500
committerJonathon Jongsma <jonathon@quotidian.org>2009-07-16 23:47:44 -0500
commite01812192846a4e4312271302b34e19e7d75fe0b (patch)
treec3f634de39450fa605bb9e455c3bb0307c769b11 /examples
parent8b70ef30a84f46e1e5dc68221f7a7c68747b6dc6 (diff)
downloadglibmm-e01812192846a4e4312271302b34e19e7d75fe0b.tar.gz
Added a simple example showing how to resolve an internet address from a hostname
Diffstat (limited to 'examples')
-rw-r--r--examples/Makefile.am2
-rw-r--r--examples/network_io/Makefile.am8
-rw-r--r--examples/network_io/resolve.cc51
3 files changed, 60 insertions, 1 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am
index ccc90149..46fcd26b 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 00000000..af7b01f7
--- /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 00000000..fb89c940
--- /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;
+ }
+}