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
00036 #include "Socket.h"
00037 #include "SocketException.h"
00038
00039 void
00040 Socket::close()
00041 {
00042 if( _connected )
00043 {
00044 ::close( _socket ) ;
00045 _socket = 0 ;
00046 _connected = false ;
00047 _listening = false ;
00048 }
00049 }
00050
00051 void
00052 Socket::send( const string &str, int start, int end )
00053 {
00054 string send_str = str.substr( start, end ) ;
00055 int bytes_written = write( _socket, send_str.c_str(), send_str.length() ) ;
00056 if( bytes_written == -1 )
00057 {
00058 string err( "socket failure, writing on stream socket" ) ;
00059 const char* error_info = strerror( errno ) ;
00060 if( error_info )
00061 err += " " + (string)error_info ;
00062 throw SocketException( err, __FILE__, __LINE__ ) ;
00063 }
00064 }
00065
00066 int
00067 Socket::receive( char *inBuff, int inSize )
00068 {
00069 int bytesRead = 0 ;
00070 if( ( bytesRead = read( _socket, inBuff, inSize ) ) < 1 )
00071 {
00072 string err( "socket failure, reading on stream socket: " ) ;
00073 const char *error_info = strerror( errno ) ;
00074 if( error_info )
00075 err += " " + (string)error_info ;
00076 throw SocketException( err, __FILE__, __LINE__ ) ;
00077 }
00078 inBuff[bytesRead] = '\0' ;
00079 return bytesRead ;
00080 }
00081
00088 void
00089 Socket::dump( ostream &strm ) const
00090 {
00091 strm << BESIndent::LMarg << "Socket::dump - ("
00092 << (void *)this << ")" << endl ;
00093 BESIndent::Indent() ;
00094 strm << BESIndent::LMarg << "socket: " << _socket << endl ;
00095 strm << BESIndent::LMarg << "is connected? " << _connected << endl ;
00096 strm << BESIndent::LMarg << "is listening? " << _listening << endl ;
00097 strm << BESIndent::LMarg << "socket address set? " << _addr_set << endl ;
00098 if( _addr_set )
00099 {
00100 strm << BESIndent::LMarg << "socket address: " << (void *)&_from << endl;
00101 }
00102 BESIndent::UnIndent() ;
00103 }
00104