# socket.h

    00001 #ifdef __GNUG__
    00002 #pragma interface
    00003 #endif
    00004
    00005 #ifndef _socket_h
    00006 #define _socket_h
    00007
    00008 #include <unistd.h>
    00009 #include <sys/types.h>
    00010
    00011 #include <string>
    00012
    00013 class TCPSocket {
    00014   protected:
    00015     TCPSocket();
    00016     virtual ~TCPSocket();
    00017
    00018     int socket_;
    00019     int port_;
    00020     bool initialized_;
    00021     bool bound_;
    00022
    00023   public:
    00024     void create();
    00025     void bind(u_int16_t port_begin, u_int16_t port_fence);
    00026     int port() { return port_; }
    00027     u_int32_t addr();
    00028
    00029     bool initialized() { return initialized_; }
    00030     bool bound() { return bound_; }
    00031
    00032     virtual void close();
    00033 };
    00034
    00035 class TCPIOSocket: public TCPSocket {
    00036   public:
    00037     int read(void *d, int n);
    00038     int write(const void *d, int n);
    00039     int read_int(int *d, int n) {return read((void*)d,n*sizeof(int));}
    00040     int write_int(const int *d, int n) {return write((void*)d,n*sizeof(int));}
    00041     int read_string(std::string &);
    00042     int write_string(const std::string &);
    00043     int read_int(int &);
    00044     int write_int(int);
    00045     int read_uint32(u_int32_t &);
    00046     int write_uint32(u_int32_t);
    00047 };
    00048
    00049 class TCPServerSocket: public TCPSocket {
    00050     friend class TCPServerConnection;
    00051   public:
    00052     void listen(int queue_length = 8);
    00053 };
    00054
    00055 class TCPServerConnection: public TCPIOSocket {
    00056   public:
    00057     void accept(const TCPServerSocket &);
    00058 };
    00059
    00060 class TCPClientConnection: public TCPIOSocket {
    00061     bool connected_;
    00062   public:
    00063     TCPClientConnection();
    00064     void close();
    00065     bool connected() { return connected_; }
    00066     void connect(const char *remote_hostname, u_int16_t remote_port);
    00067     void connect(u_int32_t remote_host, u_int16_t remote_port);
    00068 };
    00069
    00070 #endif