Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

Socket Class Reference

#include <Socket.hpp>

Inheritance diagram for Socket::

TcpSocket List of all members.

Public Methods

 Socket (sockaddr *sa=NULL, int domain=PF_INET, int type=SOCK_STREAM, int protocol=0) throw (int)
 Socket (Socket &original) throw (int)
 Socket (int set_socket, int domain=PF_INET, int type=SOCK_STREAM, int protocol=0) throw (int)
int bind (struct sockaddr *sa=NULL)
int connect (struct sockaddr *sa=NULL)
void close ()
void shutdownInput ()
void shutdownOutput ()
void shutdown (int how=2)
sockaddr * getSockAddr ()
sockaddr * getLocalSockAddr ()
int getSocket ()
int getDomain ()
int getType ()
int getProtocol ()
int setSockOpt (int level, int optname, void *optval, socklen_t optlen)
void * getSockOpt (int level, int optname, void *optval, socklen_t *optlen)
int read (void *buffer, size_t len, int flags=0)
int send (void *buffer, size_t len, int flags=0)

Protected Attributes

int s_domain
int s_type
int s_protocol
sockaddr * rsa
sockaddr * lsa
sockaddr remote_sa
sockaddr local_sa
int sck

Constructor & Destructor Documentation

Socket::Socket sockaddr *    sa = NULL,
int    domain = PF_INET,
int    type = SOCK_STREAM,
int    protocol = 0
throw (int)
 

Definition at line 15 of file Socket.cpp.

00020 {
00021   s_domain   = domain;
00022   s_type     = type;
00023   s_protocol = protocol;
00024 
00025   rsa = NULL;
00026   lsa = NULL;
00027 
00028   sck = socket( domain, type, protocol );
00029   
00030   if(sck == -1) 
00031   {
00032     throw errno;
00033   }
00034   else 
00035   {
00036     if(sa != NULL) 
00037     {
00038       local_sa.sa_family = sa->sa_family;
00039 
00040       // NOTICE: Can't you use memcpy() instead ?!
00041       for ( register int i = 0; i < 14; i++ ) 
00042       {
00043         local_sa.sa_data[i] = sa->sa_data[i];
00044       }
00045 
00046       lsa = &local_sa;
00047     }
00048     else 
00049     {
00050 /*    local_sa.sa_family = AF_INET;
00051       memset(&(local_sa.sa_data), '\0', 14);
00052       ((struct sockaddr_in*)local_sa).sin_port = htons(0);
00053       ((struct sockaddr_in)local_Sa).sin_addr.s_addr = htonl(INADDR_ANY);
00054       lsa = &local_sa; 
00055 */
00056     }
00057   }
00058 }

Socket::Socket Socket &    original throw (int)
 

Definition at line 60 of file Socket.cpp.

00061 {
00062   struct sockaddr* tmp;
00063   
00064   s_domain   = original.getDomain();
00065   s_type     = original.getType();
00066   s_protocol = original.getProtocol();
00067   
00068   sck = socket(s_domain,s_type,s_protocol);
00069   if(sck == -1) 
00070   {
00071     throw errno;
00072   }
00073   else 
00074   {
00075     tmp = original.getLocalSockAddr();
00076     local_sa.sa_family = tmp->sa_family;
00077     for (int i = 0;i<14;i++) local_sa.sa_data[i] = tmp->sa_data[i];
00078     tmp = original.getSockAddr();
00079     remote_sa.sa_family = tmp->sa_family;
00080     for (int i = 0;i<14;i++) remote_sa.sa_data[i] = tmp->sa_data[i];
00081     lsa = &local_sa;
00082     rsa = &remote_sa;
00083   }
00084 }

Socket::Socket int    set_socket,
int    domain = PF_INET,
int    type = SOCK_STREAM,
int    protocol = 0
throw (int)
 

Definition at line 86 of file Socket.cpp.

00091 {
00092   s_domain   = domain;
00093   s_type     = type;
00094   s_protocol = protocol;
00095 
00096   lsa = NULL;
00097   rsa = NULL;
00098   sck = set_socket;
00099   int i;
00100   socklen_t socksize = sizeof(struct sockaddr);
00101   
00102   i = getsockname(sck,&local_sa,&socksize);
00103   if (i == -1) 
00104   {
00105     throw errno;
00106   }
00107   else 
00108   {
00109     lsa = &local_sa;
00110     socksize = sizeof(struct sockaddr);
00111     i = getpeername(sck,&remote_sa,&socksize);
00112     if ((i == -1) && (errno != ENOTCONN)) 
00113     {
00114       throw errno;
00115     }
00116     else 
00117     {
00118       rsa = &remote_sa;
00119     }
00120   }
00121 }


Member Function Documentation

int Socket::bind struct sockaddr *    sa = NULL
 

Definition at line 128 of file Socket.cpp.

Referenced by TcpSocket::TcpSocket().

00129 {
00130   int ret;
00131   
00132   if (sa != NULL) 
00133   {
00134     local_sa.sa_family = sa->sa_family;
00135     for (int i = 0;i<14;i++) local_sa.sa_data[i] = sa->sa_data[i];
00136     rsa = &local_sa;
00137   }
00138     
00139   ret = ::bind(sck, lsa, sizeof(struct sockaddr));
00140   
00141   return ret;
00142 }

void Socket::close  
 

Definition at line 160 of file Socket.cpp.

Referenced by main().

00161 {
00162   ::close(sck);
00163 }

int Socket::connect struct sockaddr *    sa = NULL
 

Definition at line 144 of file Socket.cpp.

Referenced by TcpSocket::TcpSocket().

00145 {
00146   int ret;
00147   
00148   if (sa != NULL) 
00149   {
00150     remote_sa.sa_family = sa->sa_family;
00151     for (int i = 0;i<14;i++) remote_sa.sa_data[i] = sa->sa_data[i];
00152     rsa = &local_sa;
00153   }
00154   
00155   ret = ::connect(sck, rsa, sizeof(struct sockaddr));
00156 
00157   return ret;
00158 }

int Socket::getDomain  
 

Definition at line 199 of file Socket.cpp.

00200 {
00201   return s_domain;
00202 }

struct sockaddr * Socket::getLocalSockAddr  
 

Definition at line 187 of file Socket.cpp.

00188 {
00189   int size = sizeof(struct sockaddr);
00190   getsockname(sck, lsa, (socklen_t *) &size);
00191   return lsa;
00192 }

int Socket::getProtocol  
 

Definition at line 209 of file Socket.cpp.

00210 {
00211   return s_protocol;
00212 }

struct sockaddr * Socket::getSockAddr  
 

Definition at line 180 of file Socket.cpp.

00181 {
00182   int size = sizeof(struct sockaddr);
00183   getpeername(sck, rsa, (socklen_t *) &size);
00184   return rsa;
00185 }

void * Socket::getSockOpt int    level,
int    optname,
void *    optval,
socklen_t *    optlen
 

Definition at line 222 of file Socket.cpp.

Referenced by TcpSocket::getKeepAlive(), TcpSocket::getRecieveBufferSize(), TcpSocket::getSendBufferSize(), and TcpSocket::getTcpNoDelay().

00223 {
00224   int ret;
00225   ret = getsockopt(sck, level, optname, optval, optlen);
00226   if (ret != -1)
00227   {
00228     return optval;
00229   }
00230   else
00231   {
00232     return NULL;
00233   }
00234 }

int Socket::getSocket  
 

Definition at line 194 of file Socket.cpp.

00195 {
00196   return sck;
00197 }

int Socket::getType  
 

Definition at line 204 of file Socket.cpp.

00205 {
00206   return s_type;
00207 }

int Socket::read void *    buffer,
size_t    len,
int    flags = 0
 

Definition at line 236 of file Socket.cpp.

00237 {
00238    int l  = len;
00239    l = recv(sck,buffer, len, flags);
00240    return l;
00241 }

int Socket::send void *    buffer,
size_t    len,
int    flags = 0
 

Definition at line 244 of file Socket.cpp.

00245 {
00246    int l  = len;
00247    l = ::send(sck,buffer, len, flags);
00248    return l;
00249 }

int Socket::setSockOpt int    level,
int    optname,
void *    optval,
socklen_t    optlen
 

Definition at line 214 of file Socket.cpp.

Referenced by TcpSocket::getSoLinger(), TcpSocket::setKeepAlive(), TcpSocket::setRecieveBufferSize(), TcpSocket::setSendBufferSize(), TcpSocket::setSoLinger(), TcpSocket::setSoTimeout(), and TcpSocket::setTcpNoDelay().

00215 {
00216   int ret;
00217   ret = setsockopt(sck, level, optname, optval, optlen);
00218   return ret;
00219 }

void Socket::shutdown int    how = 2
 

Definition at line 165 of file Socket.cpp.

00166 {
00167   ::shutdown(sck, how);
00168 }

void Socket::shutdownInput  
 

Definition at line 170 of file Socket.cpp.

00171 {
00172   ::shutdown(sck, 0);
00173 }

void Socket::shutdownOutput  
 

Definition at line 175 of file Socket.cpp.

00176 {
00177   ::shutdown(sck, 1);
00178 }


Member Data Documentation

struct sockaddr Socket::local_sa [protected]
 

Definition at line 47 of file Socket.hpp.

struct sockaddr* Socket::lsa [protected]
 

Definition at line 45 of file Socket.hpp.

struct sockaddr Socket::remote_sa [protected]
 

Definition at line 46 of file Socket.hpp.

struct sockaddr* Socket::rsa [protected]
 

Definition at line 44 of file Socket.hpp.

int Socket::s_domain [protected]
 

Definition at line 43 of file Socket.hpp.

int Socket::s_protocol [protected]
 

Definition at line 43 of file Socket.hpp.

int Socket::s_type [protected]
 

Definition at line 43 of file Socket.hpp.

int Socket::sck [protected]
 

Definition at line 48 of file Socket.hpp.


The documentation for this class was generated from the following files:
Generated on Sun Oct 14 18:49:47 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001