Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Handling Multiple TCP Connections in C++


May 1996/Handling Multiple TCP Connections in C++/Listing 1

Listing 1: TCP class hierarchy interface


class IPaddress
{
  friend class SocketAddr;
  
  unsigned long address;        // Address: 4 bytes in the network
                                // byte order
  IPaddress(const unsigned int netaddr) : address(netaddr) {}

public:
  IPaddress(void)   { address = INADDR_ANY; }   // Wildcard address
  IPaddress(const char * name);        // Involves the name resolution
  unsigned long net_addr(void) const { return address; }
  operator const char * (void) const; // get a STATIC symbolic
                                      // representation of an IP 
                                      // address
};

class SocketAddr : sockaddr_in
{
  friend class StreamSocket;
  SocketAddr(void) {}
public:
  SocketAddr(const IPaddress host, const short port_no);
  operator sockaddr * (void) const      { return (sockaddr *)this; }
  operator const char * (void) const;   // Give a STATIC string
                                        // representation
};

                            // This can be either listening (passive)
                            // socket or connective (active) socket.
                            // That's why we don't have a public
                            // constructor here
class StreamSocket
{
public:
  enum Status { Good, Nodata, Eof, Failure };

protected:
  const int socket_handle;             // Socket handle = file handle
  Status status;

  StreamSocket(void);

public:
  StreamSocket(const int _socket_handle);    // wrap a class around
  ~StreamSocket(void);
  SocketAddr get_my_name(void) const;        // Name of this socket
  SocketAddr get_peer_name(void) const;      // Name of the socket this
                                             // socket is connected to
  void print(const char * title) const;      // Print the socket status

  void set_blocking_io(const BOOL onoff);
  void enable_sigio(const BOOL onoff);

                                // Write to the socket, return status
  Status write(const void * buffer, const unsigned int len);
                                // Read from the socket returning the
                                // status and size of the block read
  struct ReadResult { unsigned int len; Status status; };
  ReadResult read(void * buffer, const unsigned int len);
};

class Listen : public StreamSocket
{
public:
  Listen(const SocketAddr bind_address);
  StreamSocket * accept(const int backlog=1);
};

                         // This is a socket to initiate an active
                         // TCP open, that is, a connection. The
                         // constructor attempts to connect to another
                         // (hopefully listening) socket on
                         // probably another computer/port
class ConnectingSocket : public StreamSocket
{
public:
  ConnectingSocket(const SocketAddr target_address);
};
/* End of File */

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.