---
name: "TCPStream"
section: "3cc"
platform: "linux"
package: "libcommoncpp2-dev"
version: "1.0.13-5"
release: "Debian 3.1 (Sarge)"
platforms: ["linux"]
url: "https://new.carta.tech/TCPStream"
---

# TCPStream(3cc)

TCP streams are used to represent TCP client connections to a server by TCP protocol servers for accepting client connections. streamable TCP socket connection.

## NAME

TCPStream - TCP streams are used to represent TCP client connections to a server by TCP protocol servers for accepting client connections. streamable TCP socket connection.

## SYNOPSIS

`#include <socket.h>`

Inherits **Socket**.

Inherited by **TCPSession**, **tcpstream**, and **URLStream**.

### Public Methods

**TCPStream** (**TCPSocket** &server, int **size**=512, bool throwflag=true, **timeout\_t** **timeout**=0)\
 *Create a TCP stream by accepting a connection from a bound TCP socket acting as a server.* **TCPStream** (const **InetHostAddress** &host, **tpport\_t** port, int **size**=512, bool throwflag=true, **timeout\_t** **timeout**=0)\
 *Create a TCP stream by connecting to a TCP socket (on a remote machine).* void **setTimeout** (**timeout\_t** to)\
 *Set the I/O operation timeout for socket I/O operations.* **TCPStream** (const TCPStream &source)\
 *A copy constructor creates a new stream buffer.* virtual **~TCPStream** ()\
 *Flush and empty all buffers, and then remove the allocated buffers.* int **sync** (void)\
 *Flushes the stream input and output buffers, writes pending output.* bool **isPending** (**Pending** pend, **timeout\_t** **timeout**=TIMEOUT\_INF)\
 *Get the status of pending stream data.* int **peek** (void \*buf, size\_t len)\
 *Examine contents of next waiting packet.* int **getBufferSize** (void) const\
 *Return the size of the current stream buffering used.*

### Protected Methods

**TCPStream** (bool throwflag=true)\
 *The constructor required for 'tcpstream', a more C++ style version of the TCPStream class.* void **allocate** (int **size**)\
 *Used to allocate the buffer space needed for iostream operations.* void **endStream** (void)\
 *Used to terminate the buffer space and cleanup the socket connection.* int **underflow** ()\
 *This streambuf method is used to load the input buffer through the established tcp socket connection.* int **uflow** ()\
 *This streambuf method is used for doing unbuffered reads through the establish tcp socket connection when in interactive mode.* int **overflow** (int ch)\
 *This streambuf method is used to write the output buffer through the established tcp connection.* void **connect** (const **InetHostAddress** &host, **tpport\_t** port, int **size**)\
 *Create a TCP stream by connecting to a TCP socket (on a remote machine).* std::iostream \* **tcp** (void)\
 *Used in derived classes to refer to the current object via it's iostream.*

### Protected Attributes

**timeout\_t** **timeout**\
 int **bufsize**\
 char \* **gbuf**\
 char \* **pbuf**\

### Friends

TCPStream & **crlf** (TCPStream &)\
 TCPStream & **lfcr** (TCPStream &)\

## DETAILED DESCRIPTION

TCP streams are used to represent TCP client connections to a server by TCP protocol servers for accepting client connections. streamable TCP socket connection.

The TCP stream is a C++ 'stream' class, and can accept streaming of data to and from other C++ objects using the \<\< and \>\> operators.

TCPStream itself can be formed either by connecting to a bound network address of a TCP server, or can be created when 'accepting' a network connection from a TCP server.

**Author:**  David Sugar \<dyfet@ostel.com\>

## CONSTRUCTOR & DESTRUCTOR DOCUMENTATION

### TCPStream::TCPStream (bool throwflag = true)` [protected]`

The constructor required for 'tcpstream', a more C++ style version of the TCPStream class.

### TCPStream::TCPStream (TCPSocket & server, int size = 512, bool throwflag = true, timeout\_t timeout = 0)

Create a TCP stream by accepting a connection from a bound TCP socket acting as a server.

This performs an 'accept' call.

**Parameters:**

- *server*

  *socket listening*

- *size*

  *of streaming input and output buffers.*

- *throwflag*

  *flag to throw errors.*

- *timeout*

  *for all operations.*

### TCPStream::TCPStream (const InetHostAddress & host, tpport\_t port, int size = 512, bool throwflag = true, timeout\_t timeout = 0)

*Create a TCP stream by connecting to a TCP socket (on a remote machine).*

**Parameters:**

- *host*

  *address of remote TCP server.*

- *port*

  *number to connect.*

- *size*

  *of streaming input and output buffers.*

- *throwflag*

  *flag to throw errors.*

- *timeout*

  *for all operations.*

### TCPStream::TCPStream (const TCPStream & source)

*A copy constructor creates a new stream buffer.*

**Parameters:**

- *source*

  *of copy.*

### virtual TCPStream::~TCPStream ()` [inline, virtual]`

*Flush and empty all buffers, and then remove the allocated buffers.*

## MEMBER FUNCTION DOCUMENTATION

### void TCPStream::allocate (int size)` [protected]`

*Used to allocate the buffer space needed for iostream operations.*

*This function is called by the constructor.*

**Parameters:**

- *size*

  *of stream buffers from constructor.*

### void TCPStream::connect (const InetHostAddress & host, tpport\_t port, int size)` [protected]`

*Create a TCP stream by connecting to a TCP socket (on a remote machine).*

**Parameters:**

- *host*

  *address of remote TCP server.*

- *port*

  *number to connect.*

- *size*

  *of streaming input and output buffers.*

### void TCPStream::endStream (void)` [protected]`

*Used to terminate the buffer space and cleanup the socket connection.*

*This fucntion is called by the destructor.*

### int TCPStream::getBufferSize (void) const` [inline]`

*Return the size of the current stream buffering used.*

**Returns:**  *size of stream buffers.*

### bool TCPStream::isPending (Pending pend, timeout\_t timeout = TIMEOUT\_INF)` [virtual]`

*Get the status of pending stream data.*

*This can be used to examine if input or output is waiting, or if an error or disconnect has occured on the stream. If a read buffer contains data then input is ready and if write buffer contains data it is first flushed and then checked.*

*Reimplemented from* **Socket***.*

### int TCPStream::overflow (int ch)` [protected]`

*This streambuf method is used to write the output buffer through the established tcp connection.*

**Parameters:**

- *ch*

  *char to push through.*

**Returns:**  *char pushed through.*

### int TCPStream::peek (void \* buf, size\_t len)` [inline]`

*Examine contents of next waiting packet.*

**Parameters:**

- *buf*

  *pointer to packet buffer for contents.*

- *len*

  *of packet buffer.*

**Returns:**  *number of bytes examined.*

### void TCPStream::setTimeout (timeout\_t to)` [inline]`

*Set the I/O operation timeout for socket I/O operations.*

**Parameters:**

- *to*

  *timeout to set.*

*Reimplemented in* **URLStream***.*

### int TCPStream::sync (void)

*Flushes the stream input and output buffers, writes pending output.*

**Returns:**  *0 on success.*

### std::iostream\* TCPStream::tcp (void)` [inline, protected]`

*Used in derived classes to refer to the current object via it's iostream.*

*For example, to send a set of characters in a derived method, one might use \****tcp()** *\<\< 'test'.*

**Returns:**  *stream pointer of this object.*

### int TCPStream::uflow ()` [protected]`

*This streambuf method is used for doing unbuffered reads through the establish tcp socket connection when in interactive mode.*

*Also this method will handle proper use of buffers if not in interative mode.*

**Returns:**  *char from tcp socket connection, EOF if not connected.*

### int TCPStream::underflow ()` [protected]`

*This streambuf method is used to load the input buffer through the established tcp socket connection.*

**Returns:**  *char from get buffer, EOF if not connected.*

*Reimplemented in* **URLStream***.*

## FRIENDS AND RELATED FUNCTION DOCUMENTATION

### TCPStream& crlf (TCPStream &)` [friend]`

### TCPStream& lfcr (TCPStream &)` [friend]`

## MEMBER DATA DOCUMENTATION

### int TCPStream::bufsize` [protected]`

### char\* TCPStream::gbuf` [protected]`

### char \* TCPStream::pbuf` [protected]`

### timeout\_t TCPStream::timeout` [protected]`

## AUTHOR

*Generated automatically by Doxygen for CommonC++ from the source code.*
