<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Plugs and Sockets Example</title> <link rel="stylesheet" href="style.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.73.2"> <link rel="start" href="index.html" title="Programming with gtkmm"> <link rel="up" href="ch20.html" title="Chapter 20. Plugs and Sockets"> <link rel="prev" href="ch20.html" title="Chapter 20. Plugs and Sockets"> <link rel="next" href="ch21.html" title="Chapter 21. Timeouts, I/O and Idle Functions"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <div class="navheader"> <table width="100%" summary="Navigation header"> <tr><th colspan="3" align="center">Plugs and Sockets Example</th></tr> <tr> <td width="20%" align="left"> <a accesskey="p" href="ch20.html"><img src="../icons/prev.png" alt="Prev"></a> </td> <th width="60%" align="center">Chapter 20. Plugs and Sockets</th> <td width="20%" align="right"> <a accesskey="n" href="ch21.html"><img src="../icons/next.png" alt="Next"></a> </td> </tr> </table> <hr> </div> <div class="sect1" lang="en"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="id2590474"></a>Plugs and Sockets Example</h2></div></div></div> <p> The following is a simple example of using sockets and plugs. The method of communication between processes is deliberately kept very simple: The <code class="classname">Plug</code> writes its ID out to a text file named <code class="filename">plug.id</code> and the process with the socket reads the ID from this files. In a real program, you may want to use a more sophisticated method of inter-process communication. </p> <p><a class="ulink" href="../../../examples/book/socket/" target="_top">Source Code</a></p> <p>File: <code class="filename">plug.cc</code> </p> <pre class="programlisting"> #include <iostream> #include <fstream> #include <gtkmm.h> #include <gtkmm/plug.h> #include <glib/gstdio.h> using namespace std; const char* id_filename = "plug.id"; void on_embed() { cout << "I've been embedded." << endl; } class MyPlug : public Gtk::Plug { public: MyPlug() : m_label("I am the plug") { set_size_request(150, 100); add(m_label); signal_embedded().connect(sigc::ptr_fun(on_embed)); show_all(); } private: Gtk::Label m_label; }; int main(int argc, char** argv) { Gtk::Main app(argc, argv); MyPlug plug; ofstream out(id_filename); out << plug.get_id(); out.close(); cout << "The window ID is: " << plug.get_id() << endl; app.run(plug); // remove the ID file when the program exits g_remove(id_filename); return 0; } </pre> <p>File: <code class="filename">socket.cc</code> </p> <pre class="programlisting"> #include <iostream> #include <fstream> #include <gtkmm.h> #include <gtkmm/socket.h> using namespace std; const char* id_filename = "plug.id"; void plug_added() { cout << "A plug was added" << endl; } bool plug_removed() { cout << "A Plug was removed" << endl; return true; } class MySocketWindow : public Gtk::Window { public: MySocketWindow() { ifstream infile(id_filename); if (infile) { Gtk::Socket* socket = Gtk::manage(new Gtk::Socket()); add(*socket); socket->signal_plug_added().connect(sigc::ptr_fun(plug_added)); socket->signal_plug_removed().connect(sigc::ptr_fun(plug_removed)); Gdk::NativeWindow plug_id; infile >> plug_id; infile.close(); socket->add_id(plug_id); } else { Gtk::Label* label = Gtk::manage( new Gtk::Label( "Plug id file not found.\n Make sure plug is running.")); add(*label); set_size_request(150, 50); } show_all(); } }; int main(int argc, char** argv) { Gtk::Main app(argc, argv); MySocketWindow win; app.run(win); return 0; } </pre> <p> This example creates two executable programs: <code class="filename">socket</code> and <code class="filename">plug</code>. The idea is that <code class="filename">socket</code> has an application window that will embed a widget from the <code class="filename">plug</code> program. The way this example is designed, <code class="filename">plug</code> must be running first before starting <code class="filename">socket</code>. To see the example in action, execute the following commands in order from within the example directory: </p> <p> Start the <code class="filename">plug</code> program and send it to the background (or just use a different terminal). </p> <pre class="screen">$ ./plug &</pre> <p> After which you should see something like the following: </p> <pre class="screen">The window ID is: 69206019</pre> <p>Then start the <code class="filename">socket</code> program:</p> <pre class="screen">$ ./socket</pre> <p> After starting <code class="filename">socket</code>, you should see the following output in the terminal: </p> <pre class="screen">I've been embedded. A plug was added</pre> <p> The first line of output is from <code class="filename">plug</code>, after it has been notified that it has been embedded inside of a <code class="classname">Socket</code>. The second line was emitted by <code class="filename">socket</code> in response to its <code class="function">plug_added</code> signal. If everything was done as described above, the <code class="filename">socket</code> window should look roughly like the following: </p> <div class="screenshot"><div><img src="../figures/socket.png"></div></div> <p> If for some reason the <code class="classname">Socket</code> couldn't attach the <code class="classname">Plug</code>, the window would look something like this: </p> <div class="screenshot"><div><img src="../figures/socket-fail.png"></div></div> </div> <div class="navfooter"> <hr> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"> <a accesskey="p" href="ch20.html"><img src="../icons/prev.png" alt="Prev"></a> </td> <td width="20%" align="center"><a accesskey="u" href="ch20.html"><img src="../icons/up.png" alt="Up"></a></td> <td width="40%" align="right"> <a accesskey="n" href="ch21.html"><img src="../icons/next.png" alt="Next"></a> </td> </tr> <tr> <td width="40%" align="left" valign="top">Chapter 20. Plugs and Sockets </td> <td width="20%" align="center"><a accesskey="h" href="index.html"><img src="../icons/home.png" alt="Home"></a></td> <td width="40%" align="right" valign="top"> Chapter 21. Timeouts, I/O and Idle Functions </td> </tr> </table> </div> </body> </html>