class TCPSocketWrapper
{
class TCPAcceptedSocket
{
// ...
};
public:
enum sockstate_type { CLOSED, LISTENING,
ACCEPTED, CONNECTED };
TCPSocketWrapper();
~TCPSocketWrapper();
// this is provided for syntax
// TCPSocketWrapper s2(s2.accept());
TCPSocketWrapper(const TCPAcceptedSocket &as);
// server methods
// binds and listens on a given port number
void listen(int port, int backlog = 100);
// accepts the new connection
// it requires the earlier call to listen
TCPAcceptedSocket accept();
// client methods
// creates the new connection
void connect(const char *address, int port);
// general methods
// get the current state of the socket wrapper
sockstate_type state() const { return sockstate; }
// get the network address
// and port number of this socket
const char * address() const;
int port() const;
// write data to the socket
void write(const void *buf, int buflen);
// read data from the socket
// returns the number of bytes read
int read(void *buf, int buflen);
// close socket
void close();
private:
// copy is not supported
TCPSocketWrapper(const TCPSocketWrapper&);
TCPSocketWrapper& operator=(const TCPSocketWrapper&);
};
End of Listing