1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
<html>
<head>
<title>firstworks Programming with Rudiments using the Base Classes</title>
<link href="../css/styles.css" rel="stylesheet">
</head>
<body>
<span class="heading">Programming with Rudiments using the Base Classes</span><br><br>
<ul>
<li><a href="#daemon">Using the daemonprocess Class</a></li>
<li><a href="#file">Using the file Class</a></li>
<li><a href="#device">Using the device Class</a></li>
<li><a href="#serialport">Using the serialport Class</a></li>
<li><a href="#shmfile">Using the shmfile Class</a></li>
<li><a href="#server">Using the Server Classes</a></li>
<li><a href="#client">Using the Client Classes</a></li>
<li><a href="#complex">Using the Complex Initialization methods of the Server Classes</a></li>
<li><a href="#listener">Using the listener Class</a></li>
</ul>
<a name="daemon"></a>
<span class="heading">Using the daemonprocess Class</span><br><br>
<p>Here is some sample code for a daemon process that writes "hello" every 2
seconds. Most daemons don't write anything to the console, but this one does
for purposes of demonstration.</p>
@daemonprocess.C.html@
<a name="file">
<span class="heading">Using the file Class</span><br><br>
<p>...</p>
@file.C.html@
<a name="device">
<span class="heading">Using the device Class</span><br><br>
<p>...</p>
@device.C.html@
<a name="serialport">
<span class="heading">Using the serialport Class</span><br><br>
<p>...</p>
@serialport.C.html@
<a name="shmfile">
<span class="heading">Using the shmfile Class</span><br><br>
<p>...</p>
@shmfile.C.html@
<a name="server"></a>
<span class="heading">Using the Server Classes</span><br><br>
<p>Daemons are commonly used to serve data to clients on the same machine or
over a network. Below is an example combining the daemon and listener classes.
This server listens on unix and inet sockets for a client connection, receives
a string from the client and writes the same string back to the client.</p>
@server.C.html@
<p>Notice that this server listens on both inet and unix ports. Inet ports
allow clients and servers to talk across a network. Unix ports allow clients
and servers on the same machine to talk through a pipe. Though clients and
servers on the same machine could talk over inet ports, unix ports are much
faster and use fewer system resources.</p>
<a name="client"></a>
<span class="heading">Using the Client Classes</span><br><br>
<p>Here's the code for a client that can talk to the server above. This client
sends a string to the server, reads what the server sends back and prints it
out. It does this once over an inet port and once over a unix port.</p>
@inetclientsocket.C.html@
@unixclientsocket.C.html@
<a name="complex"></a>
<span class="heading">Using the Complex Initialization methods of the
Server Classes</span><br><br>
<p>Setting up a server to listen on a socket is actually a multi-step process.
The listen() methods simplify this process but some applications may
require a more flexible interface. If you need to set socket options or
perform additional actions between the steps of socket initialization, you can
use the Complex Inititalization methods of the server classes.</p>
<p>Below is an alternative implementation of the myserver constructor in
which some socket options are set.</p>
<pre>
myserver::myserver() : daemonprocess(), listener() {
<font color="#0000ff">// run as a different user/group</font>
runAsUser(<font color="#ff00ff">"nobody"</font>);
runAsGroup(<font color="#ff00ff">"nobody"</font>);
<font color="#0000ff">// detach from the controlling tty</font>
detach();
<font color="#0000ff">// initialize the ports</font>
inetsocket.initialize(<font color="#ff00ff">NULL</font>,<font color="#ff00ff">8040</font>);
unixsocket.initialize(<font color="#ff00ff">"/tmp/mysocket"</font>,S_IRUSR|S_IWUSR);
<font color="#0000ff">// set some socket options</font>
inetsocket.lingerOnClose(<font color="#ff00ff">10</font>);
inetsocket.reuseAddresses();
unixsocket.lingerOnClose(<font color="#ff00ff">10</font>);
<font color="#0000ff">// bind to the ports</font>
inetsocket.bind();
unixsocket.bind();
<font color="#0000ff">// listen on the ports</font>
inetsocket.listen(<font color="#ff00ff">15</font>);
unixsocket.listen(<font color="#ff00ff">15</font>);
<font color="#0000ff">// add sockets to the pool</font>
addFileDescriptor(&inetsocket);
addFileDescriptor(&unixsocket);
}
</pre>
<a name="listener">
<span class="heading">Using the listener Class</span><br><br>
<p>...</p>
@listener.C.html@
</body>
</html>
|