00001 00009 // Definition of the Socket class 00010 00011 #ifndef Socket_class 00012 #define Socket_class 00013 00014 00015 #include <sys/types.h> 00016 #include <sys/socket.h> 00017 #include <netinet/in.h> 00018 #include <netdb.h> 00019 #include <unistd.h> 00020 #include <string> 00021 #include <arpa/inet.h> 00022 00023 00024 const int MAXHOSTNAME = 1024; 00025 const int MAXCONNECTIONS = 5; 00026 00027 class Socket 00028 { 00029 public : 00030 void showErrors() {outputErrors = true;} 00031 void hideErrors() {outputErrors = false;} 00032 protected : 00033 bool outputErrors; 00034 public : 00035 // Server initialization 00036 bool Create(); 00037 bool Bind ( const int port ); 00038 bool Listen() ; 00039 bool Accept ( Socket & s ) const {return Accept(&s);} 00040 bool Accept ( Socket * s ) const; 00041 00042 // Client initialization 00043 bool Connect ( const char * host, const int port ); 00044 void SetNonBlocking ( const bool ); 00045 00046 00047 public: 00048 Socket(); 00049 Socket(const char * _host, int _port); 00050 Socket(int port); 00051 ~Socket(); 00052 00053 // Data Transimission 00054 size_t Send ( const unsigned char * s, size_t size ) ; 00055 size_t Receive ( unsigned char * s, size_t max_size ) ; 00056 bool WaitData(size_t millisec); 00057 00058 bool WaitBuffer(unsigned char * s, size_t size, 00059 size_t millisec) ; 00060 00061 bool SendAll(const unsigned char * s, size_t size, 00062 size_t millisec) ; 00063 00064 bool Close(); 00065 00066 00067 bool IsOpen() const { return m_sock != -1; } 00068 bool IsBroken() const {return broken_pipe;} 00069 00070 bool PrepareServer(); 00071 bool Open(Socket * newsocket); 00072 bool Open(); 00073 protected: 00074 bool accepting; 00075 bool broken_pipe; 00076 00077 int port; 00078 char * host; 00079 bool server; 00080 private: 00081 00082 fd_set rfs; 00083 int m_sock; 00084 sockaddr_in m_addr; 00085 00086 }; 00087 00088 00089 #endif