Programming with Rudiments using the Base Classes

- Using the daemonprocess Class
- Using the inetserversocket Class
- Using the inetclientsocket Class
- Using the unixserversocket Class
- Using the unixclientsocket Class
- Using the modemserver Class
- Using the modemclient Class
- Using the Complex Initialization Methods of the Server Classes
- Using the clientserverfactory Class
- Using the listener Class

Using the daemonprocess Class

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.

// Copyright (c) 2012 David Muse
// See the file COPYING for more information
#ifdef RUDIMENTS_NAMESPACE
using namespace rudiments;
#endif
int main(int argc, const char **argv) {
}
Using the inetserversocket Class

Daemons are commonly used to serve data to clients over a network. This can be done using the inetserversocket class. The example below listens on an inet socket for a client connection, receives a string from the client and writes the same string back to the client.

// Copyright (c) 2012 David Muse
// See the file COPYING for more information
#ifdef RUDIMENTS_NAMESPACE
using namespace rudiments;
#endif
int main(int argc, const char **argv) {
}
Using the inetclientsocket Class

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.

// Copyright (c) 2012 David Muse
// See the file COPYING for more information
#ifdef RUDIMENTS_NAMESPACE
using namespace rudiments;
#endif
int main(int argc, const char **argv) {
}

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.

Using the unixserversocket Class

Daemons are commonly used to serve data to clients over a network but they can also be used to serve data to processes running on the local machine. This can be done using the inetserversocket class but if your platform supports unix sockets, then it can be done much faster and more efficiently using the unixserversocket class. The example below listens on an unix socket for a client connection, receives a string from the client and writes the same string back to the client.

// Copyright (c) 2012 David Muse
// See the file COPYING for more information
#ifdef RUDIMENTS_NAMESPACE
using namespace rudiments;
#endif
int main(int argc, const char **argv) {
}
Using the unixclientsocket Class

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.

// Copyright (c) 2012 David Muse
// See the file COPYING for more information
#ifdef RUDIMENTS_NAMESPACE
using namespace rudiments;
#endif
int main(int argc, const char **argv) {
}
Using the modemserver Class

Even in this day and age, it isn't uncommon for embedded systems to communicate with the outside world over dial-up lines. To fill this need, rudiments provides implementations of the client and server abstractions for modems as well. The code below listens for a client connection over a dial-up line.

// Copyright (c) 2012 David Muse
// See the file COPYING for more information
#ifdef RUDIMENTS_NAMESPACE
using namespace rudiments;
#endif
int main(int argc, const char **argv) {
}
Using the modemclient Class

Here's the code for a client that can talk to the modem server above. This client sends a string to the server, reads what the server sends back and prints it out.

// Copyright (c) 2012 David Muse
// See the file COPYING for more information
#ifdef RUDIMENTS_NAMESPACE
using namespace rudiments;
#endif
int main(int argc, const char **argv) {
}
Using the Complex Initialization methods of the Server Classes

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.

Below is an alternative implementation of the inet server in which some socket options are set.

// Copyright (c) 2012 David Muse
// See the file COPYING for more information
#ifdef RUDIMENTS_NAMESPACE
using namespace rudiments;
#endif
int main(int argc, const char **argv) {
}
Using the clientserverfactory Class

Sometimes you know that you need a client or server at compile time, but won't know until runtime what kind of client or server that you need. The clientserverfactory class can help in this regard.

// Copyright (c) 2012 David Muse
// See the file COPYING for more information
#ifdef RUDIMENTS_NAMESPACE
using namespace rudiments;
#endif
int main(int argc, const char **argv) {
}
Using the listener Class

Servers commonly need to listen on multiple sockets for client connections. They can listen on multiple inet sockets, multiple unix sockets, or a combination. Technically, they could listen on any set of file descriptors, whether they are all sockets or not. The listener class can be used to make this possible.

// Copyright (c) 2012 David Muse
// See the file COPYING for more information
#ifdef RUDIMENTS_NAMESPACE
using namespace rudiments;
#endif
int main(int argc, const char **argv) {
}