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.
#include <daemonprocess.h> #include <iostream.h> #include <unistd.h> #include <stdlib.h> class mydaemon: public daemonprocess { public: void init(); void loop(); }; void mydaemon::init() { runAsUser("nobody"); runAsGroup("nobody"); detach(); } void mydaemon::loop() { for (;;) { cout << "hello" << endl; sleep(2); } } mydaemon *md; void shutDown() { delete md; exit(0); } main() { md=new mydaemon; md->handleShutDown((void *)shutDown); md->init(); md->loop(); }Using the Server Class
Daemons are commonly used to serve data to clients over a network. Below is an example combining the daemon and server classes. This server receives a string from the client and writes the same string back to the client.
#include <daemonprocess.h> #include <server.h> #include <unistd.h> #include <stdlib.h> #include <sys/stat.h> class myserver: public daemonprocess, public server { public: void init(); void loop(); void session(); }; void myserver::init() { // run as a different user/group runAsUser("nobody"); runAsGroup("nobody"); // detach from the controlling tty detach(); // open, bind and listen on an inet port and unix socket openSockets(8040,"/tmp/mysocket",S_IRUSR|S_IWUSR,15); } void myserver::loop() { for (;;) { // accept connections, with no timeout acceptConnection(-1,-1); // fork to handle the new connection if (!fork()) { // close the duplicated server socket, // engage in a session with the client, // close the client socket and exit closeServerSocket(); session(); closeClientSocket(); _exit(0); } // close the client socket, but leave the server // socket open to accept more connections closeClientSocket(); } } void myserver::session() { // read a string from the client short bytes; readFromClient(&bytes); char *string=new char[bytes+1]; readFromClient(string,bytes); // write the same string back to the client writeToClient(bytes); writeToClient(string,bytes); // clean up delete[] string; } myserver *ms; void shutDown() { delete ms; exit(0); } main() { ms=new myserver; ms->handleShutDown((void *)shutDown); ms->init(); ms->loop(); }
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 Client ClassHere'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.
#include <client.h> #include <iostream.h> class myclient: public client { public: void inetSession(char *host, int port, char *message); void unixSession(char *socket, char *message); private: void session(char *message); }; void myclient::inetSession(char *host, int port, char *message) { openInetConnection(host,port,0,1); session(message); } void myclient::unixSession(char *socket, char *message) { openUnixConnection(socket,0,1); session(message); } void myclient::session(char *message) { // write a message to the server writeToServer((short)strlen(message)); writeToServer(message); // read a message back from the server short bytes; readFromServer(&bytes); char *string=new char[bytes+1]; readFromServer(string,bytes); // write out the message we got from the server cout << string << endl; // clean up delete[] string; } main() { // declare an instance of our client myclient mc; // connect to a known inet host/port and unix socket and // send a message over each connection mc.inetSession("localhost",8040,"message over inet port"); mc.unixSession("/tmp/mysocket","message over unix port"); }