zutil-cpp/net/socket.cpp

69 lines
1.5 KiB
C++

#include "socket.h"
using namespace zutil;
Socket::Socket(){
streambuf = NULL;
in = NULL;
out = NULL;
}
Socket::Socket( string host, int port ){
streambuf = NULL;
in = NULL;
out = NULL;
if ( !SocketIO::create() ){
throw SocketException ( "Could not create client socket." );
}
if ( !SocketIO::connect ( host, port ) ){
throw SocketException ( "Could not bind to port." );
}
}
Socket::~Socket(){
delete in;
delete out;
delete streambuf;
}
istream& Socket::get_istream(){
if(streambuf == NULL)
streambuf = new SocketStreamBuffer( *this );
if(in == NULL)
in = new istream( streambuf );
return *in;
}
ostream& Socket::get_ostream(){
if(streambuf == NULL)
streambuf = new SocketStreamBuffer( *this );
if(out == NULL)
out = new ostream( streambuf );
return *out;
}
const Socket& Socket::operator<<( const string &s ) const{
if ( !SocketIO::write( s ) ){
throw SocketException ( "Could not write string to socket." );
}
return *this;
}
const Socket& Socket::operator<<( const char c ) const{
if ( !SocketIO::write( c ) ){
throw SocketException ( "Could not write char to socket." );
}
return *this;
}
const Socket& Socket::operator>>( string &s ) const{
if ( !SocketIO::read( s ) ){
throw SocketException ( "Could not read string from socket." );
}
return *this;
}
const Socket& Socket::operator>>( char &c ) const{
if ( !(c = SocketIO::read( )) ){
throw SocketException ( "Could not read char from socket." );
}
return *this;
}