00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <unistd.h>
00034 #include <errno.h>
00035 #include <sys/types.h>
00036 #include <sys/socket.h>
00037 #include <arpa/inet.h>
00038
00039 #include "Socket.h"
00040 #include "SocketException.h"
00041
00042 Socket::Socket( int socket, struct sockaddr *addr )
00043 : _socket( socket ),
00044 _connected( true ),
00045 _listening( false ),
00046 _addr_set( true )
00047 {
00048 char ip[46];
00049 unsigned int port;
00050
00051 switch (addr->sa_family) {
00052 case AF_INET:
00053 inet_ntop (AF_INET, &(((struct sockaddr_in *)addr)->sin_addr), ip, sizeof (ip));
00054 port = ntohs (((struct sockaddr_in *)addr)->sin_port);
00055 break;
00056 case AF_INET6:
00057 inet_ntop (AF_INET6, &(((struct sockaddr_in6 *)addr)->sin6_addr), ip, sizeof (ip));
00058 port = ntohs (((struct sockaddr_in6 *)addr)->sin6_port);
00059 break;
00060 default:
00061 snprintf (ip, sizeof (ip), "UNKNOWN FAMILY: %d", addr->sa_family);
00062 port = 0;
00063 break;
00064 }
00065 _port = port ;
00066 _ip = ip ;
00067 }
00068
00069 void
00070 Socket::close()
00071 {
00072 if( _connected )
00073 {
00074 ::close( _socket ) ;
00075 _socket = 0 ;
00076 _connected = false ;
00077 _listening = false ;
00078 }
00079 }
00080
00081 void
00082 Socket::send( const string &str, int start, int end )
00083 {
00084 string send_str = str.substr( start, end ) ;
00085 int bytes_written = write( _socket, send_str.c_str(), send_str.length() ) ;
00086 if( bytes_written == -1 )
00087 {
00088 string err( "socket failure, writing on stream socket" ) ;
00089 const char* error_info = strerror( errno ) ;
00090 if( error_info )
00091 err += " " + (string)error_info ;
00092 throw SocketException( err, __FILE__, __LINE__ ) ;
00093 }
00094 }
00095
00096 int
00097 Socket::receive( char *inBuff, int inSize )
00098 {
00099 int bytesRead = 0 ;
00100 if( ( bytesRead = read( _socket, inBuff, inSize ) ) < 1 )
00101 {
00102 string err( "socket failure, reading on stream socket: " ) ;
00103 const char *error_info = strerror( errno ) ;
00104 if( error_info )
00105 err += " " + (string)error_info ;
00106 throw SocketException( err, __FILE__, __LINE__ ) ;
00107 }
00108 inBuff[bytesRead] = '\0' ;
00109 return bytesRead ;
00110 }
00111
00118 void
00119 Socket::dump( ostream &strm ) const
00120 {
00121 strm << BESIndent::LMarg << "Socket::dump - ("
00122 << (void *)this << ")" << endl ;
00123 BESIndent::Indent() ;
00124 strm << BESIndent::LMarg << "socket: " << _socket << endl ;
00125 strm << BESIndent::LMarg << "is connected? " << _connected << endl ;
00126 strm << BESIndent::LMarg << "is listening? " << _listening << endl ;
00127 strm << BESIndent::LMarg << "socket address set? " << _addr_set << endl ;
00128 if( _addr_set )
00129 {
00130 strm << BESIndent::LMarg << "socket port: " << _port << endl;
00131 strm << BESIndent::LMarg << "socket ip: " << _ip << endl;
00132 }
00133 BESIndent::UnIndent() ;
00134 }
00135