Class Operations
Address Classes :
// It is an Base Class for all other Domain specific socket address classes which are
derived from it .
class SockAddr{
private:
int validity;
// To check for exceptions and errors.
// it is checked in every function to validate further actions.
SockAddr *SockAddrptr;
// Generic pointer to the base class which could to pointing to
// any of the derived class based on the parameters.
// It could be either Inet or unix.
public :
SockAddr();
// Default constructor .
SockAddr(int family,int port=0,char *host=Null);
// Based on the family parameter the pointer will be assigned the
derived class.
SockAddr(int family,char *host,char *serv);
// Based on the family parameter the pointer will be assigned the
derived class.
SockAddr &operator = (SockAddr newAddr);
// Operator overloaded to assign any new address directly to it.
SockAddr *operator void *();
// Operator overloaded to get this pointer.
struct SockAddr &GetAddr();
// To get the address stored in the SockAddrptr.
int valid();
// Function used to access valid variable to test for further actions.
virtual const int Getlen();
// The appropriate derived class function will be called .
virtual int Getfamily();
// The appropriate derived class function will be called .
struct hostent *gethostbyname(char *host);
struct hostent *gethostbyaddr(char *hostaddr,int len,int type);
struct servent *getservbyname(char *service,char *proto);
// The above methods are used to get the service or host structures given
any one parameter.
long gethostid();
// returns the IP address in integer form.
private:
virtual void update(SockAddr *newAddr){}
// the update method is used to update the values based on the new
address assigned to it .
};
// The SockAddr_Inet Is the derived from SockAddr class which contains
Inet specific methods.
class SockAddr_Inet :public SockAddr{
private:
struct sockaddr_in addrstruct;
int len;
int family;
public:
InetSockAddr(int port=0,char *host=Null);
InetSockAddr(char *host,char *serv=Null,char *proto=Null);
struct SockAddr *GetAddr();
const int Getlen();
private:
void Helperconstructer(int port,char *host);
// As both Constructors contain same code we use the helper constructor to
// simplify it.
int Getfamily();
void Update(SockAddr *NewAddr);
// The Update method in turn calls Effectchange method .
void Effectchange(InetSockAddr *newAddr);
};
// The SockAddr_Unix Is the derived from SockAddr class which contains
Unix specific methods.
class SockAddr_Unix :public SockAddr{
private:
struct sockaddr_un addrstruct;
int len;
int family;
public:
UnixSockAddr(char *path=Null);
struct SockAddr *GetAddr();
const int Getlen();
private:
void Helperconstructer(int port,char *host);
// As both Constructors contain same code we use the helper constructor to
// simplify it.
int Getfamily();
void Update(SockAddr *NewAddr);
// The Update method in turn calls Effectchange method .
void Effectchange(UnixSockAddr *newAddr);
};
Socket classes :
// It is an Base Class for all other modes of Communication classes like sockets
streams , fifos, IPC communication , TLI which are derived from it .
class IPC-SAP{
private :
int fd;
public:
int Getfd();
int Setfd();
};
// It is an Base Class for all other Domain specific socket classes which are
derived from it .
class Socket :public IPC-SAP{
private:
int ptrvalidity;
// To check for exceptions and errors.
int Sockfd;
protected :
Buffer *send_buff,*recv_buff;
int SendTimeout;
int RecvTimeout;
public :
Socket();
// default constructor.
Socket(int SockFamily,int SockType,int Protcol=0);
// constructor.
const int GetSockFd();
// returns the sockfd.
virtual int Isconnected();
virtual int GetFamily();
virtual void send(char *buff,int nbytes,SockAddr peer=Null,int flags);
virtual void recv(char *buff,int &nbytes,SockAddr &peer=Null,int flags=0);
virtual int operator << (const char *buff);
virtual int operator >> (char *buff);
virtual int readv(struct iovec iov[],int iovcount,int flags=0);
virtual int writev(struct iovec iov[],int iovcount,int flags);
virtual void bind(SockAddr peer);
void getpeername(SockAddr &peer);
void getsockname(SockAddr &peer);
void close();
void shutdown(int howto=NORECVSND);
void setopt(int opt,int level);
void unsetopt(int opt,int level);
void setbuffsz(const int sz);
void unsetbuffsz(int buff=SNDBUFF);
void flush_output();
void listen(int);
void set_sendtimeout(int);
void set_recvtimeout(int);
virtual int Accept(SockAddr &);
virtual int connect(SockAddr &);
private:
int valid();
// to check for validity.
};
// The derived class Socket_Inet specific to Inet family.
class Socket_Inet :public Socket{
private:
int family;
int type;
int proto;
int state;
// Connected or Not-Connected
int mode;
// Sync or async.
int sigstatus;
int valid;
int Sockfd;
public :
Socket_Inet(int Type ,int protocol);
Socket_Inet();
// Sets the Family, type proto and state parameters of the class.
int Isconnected();
int GetFamily();
void send(char *buff,int nbytes,SockAddr peer,int flags);
void recv(char *buff,int &nbytes,SockAddr &peer,int flags);
int operator << (const char *buff);
int operator >> (char *buff);
int readv(struct iovec iov[],int iovcount,int flags=0);
int writev(struct iovec iov[],int iovcount,int flags);
int Accept(SockAddr &);
int connect(SockAddr &);
void Bind(SockAddr &);
int getsockid();
};
// The derived class Socket_Unix specific to Unix family.
class Socket_Unix :public Socket{
private:
int family;
int type;
int proto;
int state;
int mode;
int sigstatus;
public :
Socket_unix(int Type,int protocol);
int Isconnected();
int GetFamily();
void send(char *buff,int nbytes,SockAddr peer,int flags);
void recv(char *buff,int &nbytes,SockAddr &peer,int flags);
int operator << (const char *buff);
int operator >> (char *buff);
int readv(struct iovec iov[],int iovcount,int flags=0);
int writev(struct iovec iov[],int iovcount,int flags);
int Accept(SockAddr &);
int connect(SockAddr &);
void Bind(SockAddr &);
int getsockid();
};
Back To Main Page