cpp utils files, network and a smal util class
This commit is contained in:
parent
ea3dffdb4a
commit
62f50a2f79
13 changed files with 610 additions and 0 deletions
69
net/socket.cpp
Normal file
69
net/socket.cpp
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
#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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue