// Copyright (c) 1999-2000 David Muse // See the COPYING file for more information. #ifndef SERVER_H #define SERVER_H #include <strstream.h> #include <sys/types.h> #include <sys/un.h> #include <netinet/in.h> #include <netdb.h> // The server class allows you to write programs that can talk to client // programs over TCP connections. // // It provides connection management and data transfer methods but // does not imply any particular client/server protocol. // // The class supports TCP connections over Inet and Unix domain sockets. // Inet sockets allow clients and servers on different machines to talk over a // network. Unix sockets allow clients and servers on the same machine to // talk. Inet sockets can be used by clients and servers on the same machine // but Unix sockets generally perform better. class server { public: server(); virtual ~server(); int listenOnInetPort(unsigned short int port, int backlog); // Listen on inet port: "port" and allow // "backlog" connections to pile up before // refusing them. // Entering a value of 0 for the "port" causes // the server to listen on an arbitrary port. // The getInetServerPort() method can be // used later to discover this port. int listenOnUnixPort(char *port, mode_t permissions, int backlog); // Listen on unix port: "port" and allow // "backlog" connections to pile up before // refusing them. Set the permissions on // "port" to "permissions". int listenOnPorts(unsigned short int inetport, char *unixport, mode_t permissions, int backlog); // listen on inet port: "inetport" and // unix port: "unixport" and allow // "backlog" connections to pile up before // refusing them. Set the permissions on // "unixport" to "permissions". int waitForClientConnection(int timeoutsec, int timeoutusec); // Wait for connections on any ports opened // by the calls above for "timeoutsec" seconds // and "timeoutusec" milliseconds before // falling through. // // Entering -1 for either parameter causes the // method to wait indefinitely. // // Entering 0 for both parameters causes the // method to fall through immediately unless a // client is already trying to connect. // // Returns 1 for a successful connection and 0 // otherwise. void lingerOnClose(int timeout); // Instructs the connection to the client to stay open // for "timeout" seconds even after disconnectClient() // is called if the client hasn't received all of the // data yet. void dontLingerOnClose(); // Instructs the connection to the client close // immediately when disconnectClient() is called // even if the client hasn't received all of the // data yet. // These methods write data to the server. On success, the // number of bytes written is returned. On failure, a -1 is // returned. int writeToClient(unsigned short number) const; int writeToClient(unsigned long number) const; int writeToClient(float number) const; int writeToClient(double number) const; int writeToClient(char character) const; int writeToClient(char *string) const; int writeToClient(char *string, size_t size) const; int writeToClient(void *buffer, size_t size) const; // These methods read data from the server. If no data is // available, they will wait until the server sends data or // closes the connection. On success, the number of bytes // read is returned. On failure, a -1 is returned. int readFromClient(unsigned short *buffer) const; int readFromClient(unsigned long *buffer) const; int readFromClient(float *buffer) const; int readFromClient(double *buffer) const; int readFromClient(char *buffer, size_t size) const; int readFromClient(void *buffer, size_t size) const; int readFromClient(strstream *buffer, char *terminator); int disconnectClient(); // disconnects any connected client int stopListening(); // disconnects the server process from any ports // it may be listening on unsigned short int getInetServerPort(); // returns the inet port number that // the server is listening on char *getUnixServerPort(); // returns the filename of the unix // port that the server is listening on int getServerSocket(); // returns the internal file descriptor // of the server socket int getClientSocket(); // returns the internal file descriptor // of the client socket protected: #include <private/server.h> }; #endif