Boost logo

Boost :

From: Jani Kajala (jani_at_[hidden])
Date: 2001-12-19 05:56:39


Some comments:

Below the socket is the stream. Better approach would be to separate the two, so
that low-level byte in/out stream would be requested from the socket and then
there would be separate ("filter") class which would provide more high level io.
If this is considered too much burden, then the low-level io functionality could
be in socket classes and separate high-level class would use the socket classes
to provide high level io. In any case, dependencies to iostreams would be
avoided.

Network address (say, inet_address) should also be a separate class. This is
also makes sense in context that it has its own operations (gethostbyname etc).

Regards,
Jani Kajala
http://www.helsinki.fi/~kajala

----- Original Message -----
From: "Rob Tougher" <rtougher_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Wednesday, December 19, 2001 12:11 AM
Subject: [boost] sockets library

>

--------------------------------------------------------------------------------

> //
> // Definitions of socket classes for boost
> //
>
> #include <string>
> #include <sstream>
>
> #ifndef boost_sockets
> #define boost_sockets
>
>
>
> //
> // Creates a client socket
> //
> class client_socket : private socket
> {
> public:
>
> client_socket ( std::string host, int port );
> virtual ~client_socket(){};
>
> const client_socket& operator << ( const std::string& ) const;
> const client_socket& operator >> ( std::string& ) const;
> const client_socket& operator << ( const marshallable& ) const;
> const client_socket& operator >> ( marshallable& ) const;
> };
>
>
>
>
> //
> // Creates a server socket
> //
> class server_socket : private socket
> {
> public:
>
> server_socket ( int port );
> server_socket (){};
> virtual ~server_socket();
>
> const server_socket& operator << ( const std::string& ) const;
> const server_socket& operator >> ( std::string& ) const;
> const server_socket& operator << ( const marshallable& ) const;
> const server_socket& operator >> ( marshallable& ) const;
>
> void accept ( server_socket& );
>
> };
>
>
>
>
> //
> // Inherit from marshallable and implement
> // marshal and unmarshal to allow your class
> // to be sent across a socket.
> //
> class marshallable
> {
> public:
> marshallable(){};
> ~marshallable(){};
>
> virtual void marshal ( std::ostringstream& );
> virtual void unmarshal ( std::istringstream& );
> };
>
>
>
>
> //
> // exception class
> //
> class socket_exception
> {
> public:
> socket_exception ( std;:string );
> ~socket_exception();
> };
>
>
>
> //
> // Most of the implementation is in here. We
> // can have a different class for each platform.
> //
> class socket
> {
>
> public:
>
> socket();
> virtual ~socket();
>
> // Server initialization
> bool create();
> bool bind ( const int port );
> bool listen() const;
> bool accept ( Socket& ) const;
>
> // Client initialization
> bool connect ( const std::string host, const int port );
>
> // Data Transimission
> bool send ( const std::string ) const;
> int recv ( std::string& ) const;
>
> bool is_valid() const { return m_sock != -1; }
>
> private:
>
> int m_sock;
> sockaddr_in m_addr;
>
> };
>
>
>
> #endif
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk