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

# SocketPort(3cc)

The socket port is an internal class which is attached to and then serviced by a specific SocketService 'object'. base class for realtime and thread pool serviced protocols.

## NAME

SocketPort - The socket port is an internal class which is attached to and then serviced by a specific **SocketService** 'object'. base class for realtime and thread pool serviced protocols.

## SYNOPSIS

`#include <socket.h>`

Inherits **Socket**, and **TimerPort**.

### Public Methods

void **setTimer** (**timeout\_t** timeout=0)\
 *Derived setTimer to notify the service thread pool of change in expected timeout.* void **incTimer** (**timeout\_t** timeout)\
 *Derived incTimer to notify the service thread pool of a change in expected timeout.*

### Protected Methods

**SocketPort** (**SocketService** \*svc, **TCPSocket** &tcp)\
 *Construct an accepted TCP socket connection from a specific bound TCP server.* **SocketPort** (**SocketService** \*svc, const **InetAddress** &ia, **tpport\_t** port)\
 *Construct a bound UDP socket for use in deriving realtime UDP streaming protocols handled by thread pool objects.* **SocketPort** (**SocketService** \*svc, const **InetHostAddress** &ih, **tpport\_t** port)\
 *A non-blocking constructor for outbound tcp connections.* void **attach** (**SocketService** \*svc)\
 *Attach yourself to the service pool thread object.* virtual **~SocketPort** ()\
 *Disconnect the socket from the service thread pool and the remote connection.* void **setDetectPending** (bool)\
 *Used to indicate if the service thread should monitor pending data for us.* bool **getDetectPending** (void) const\
 *Get the current state of the DetectPending flag.* void **setDetectOutput** (bool)\
 *Used to indicate if output ready monitoring should be performed by the service thread.* bool **getDetectOutput** (void) const\
 *Get the current state of the DetectOutput flag.* virtual void **expired** (void)\
 *Called by the service thread pool when the objects timer has expired.* virtual void **pending** (void)\
 *Called by the service thread pool when input data is pending for this socket.* virtual void **output** (void)\
 *Called by the service thread pool when output data is pending for this socket.* virtual void **disconnect** (void)\
 *Called by the service thread pool when a disconnect has occured.* **Error** **connect** (const **InetAddress** &ia, **tpport\_t** port)\
 *Connect a* **Socket** *Port to a known peer host.* int **send** (const void \*buf, int len)\
 *Transmit 'send' data to a connected peer host.* int **receive** (void \*buf, size\_t len)\
 *Receive a message from any host.* int **peek** (void \*buf, size\_t len)\
 *Examine the content of the next packet.*

### Friends

class **SocketService**\

## DETAILED DESCRIPTION

The socket port is an internal class which is attached to and then serviced by a specific **SocketService** 'object'. base class for realtime and thread pool serviced protocols.

Derived versions of this class offer specific functionality for specific protocols. Both Common C++ supporting frameworks and application objects may be derived from related protocol specific base classes.

A special set of classes, 'SocketPort' and '**SocketService**', exist for building realtime streaming media servers on top of UDP and TCP protocols. The 'SocketPort' is used to hold a connected or associated TCP or UDP socket which is being 'streamed' and which offers callback methods that are invoked from a '**SocketService**' thread. **SocketService**'s can be pooled into logical thread pools that can service a group of SocketPorts. A millisecond accurate 'timer' is associated with each SocketPort and can be used to time synchronize SocketPort I/O operations.

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

## CONSTRUCTOR & DESTRUCTOR DOCUMENTATION

### SocketPort::SocketPort (SocketService \* svc, TCPSocket & tcp)` [protected]`

Construct an accepted TCP socket connection from a specific bound TCP server.

This is meant to derive advanced application specific TCP servers that can be thread pooled.

**Parameters:**

- *svc*

  *pool thread object.*

- *tcp*

  *socket object to accept.*

### SocketPort::SocketPort (SocketService \* svc, const InetAddress & ia, tpport\_t port)` [protected]`

*Construct a bound UDP socket for use in deriving realtime UDP streaming protocols handled by thread pool objects.*

**Parameters:**

- *svc*

  *pool thread object.*

- *ia*

  *address of interface to bind.*

- *port*

  *number to bind to.*

### SocketPort::SocketPort (SocketService \* svc, const InetHostAddress & ih, tpport\_t port)` [protected]`

*A non-blocking constructor for outbound tcp connections.*

*To detect when the connection is established, overload* **SocketPort::output()***.* **SocketPort::output()** *get's called by the* **SocketService** *when the connection is ready,* **SocketPort::disconnect()** *when the connect failed. at the moment you should set the socket state to 'CONNECTED' when* **SocketPort::output()** *get's called for the first time.*

**Parameters:**

- *svc*

  *pool thread object.*

- *ih*

  *addess to connect to.*

- *port*

  *number to connect to.*

### virtual SocketPort::~SocketPort ()` [protected, virtual]`

*Disconnect the socket from the service thread pool and the remote connection.*

## MEMBER FUNCTION DOCUMENTATION

### void SocketPort::attach (SocketService \* svc)` [protected]`

*Attach yourself to the service pool thread object.*

*The later version.*

**Parameters:**

- *svc*

  *pool thread object*

### Error SocketPort::connect (const InetAddress & ia, tpport\_t port)` [protected]`

*Connect a* **Socket** *Port to a known peer host.*

*This is normally used with the UDP constructor. This is also performed as a non-blocking operation under Posix systems to prevent delays in a callback handler.*

**Returns:**  *0 if successful.*

**Parameters:**

- *ia*

  *address of remote host or subnet.*

- *port*

  *number of remote peer(s).*

### virtual void SocketPort::disconnect (void)` [inline, protected, virtual]`

*Called by the service thread pool when a disconnect has occured.*

### virtual void SocketPort::expired (void)` [inline, protected, virtual]`

*Called by the service thread pool when the objects timer has expired.*

*Used for timed events.*

### bool SocketPort::getDetectOutput (void) const` [inline, protected]`

*Get the current state of the DetectOutput flag.*

### bool SocketPort::getDetectPending (void) const` [inline, protected]`

*Get the current state of the DetectPending flag.*

### void SocketPort::incTimer (timeout\_t timeout)

*Derived incTimer to notify the service thread pool of a change in expected timeout.*

*This allows* **SocketService** *to reschedule all timers. Otherwise same as* **TimerPort***.*

**Parameters:**

- *timeout*

  *in milliseconds.*

*Reimplemented from* **TimerPort***.*

### virtual void SocketPort::output (void)` [inline, protected, virtual]`

*Called by the service thread pool when output data is pending for this socket.*

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

*Examine the content of the next packet.*

*This can be used to build 'smart' line buffering for derived TCP classes.*

**Parameters:**

- *buf*

  *pointer to packet buffer to examine.*

- *len*

  *of packet buffer to examine.*

**Returns:**  *number of bytes actually available.*

### virtual void SocketPort::pending (void)` [inline, protected, virtual]`

*Called by the service thread pool when input data is pending for this socket.*

### int SocketPort::receive (void \* buf, size\_t len)` [inline, protected]`

*Receive a message from any host.*

*This is used in derived classes to build protocols.*

**Parameters:**

- *buf*

  *pointer to packet buffer to receive.*

- *len*

  *of packet buffer to receive.*

**Returns:**  *number of bytes received.*

### int SocketPort::send (const void \* buf, int len)` [inline, protected]`

*Transmit 'send' data to a connected peer host.*

*This is not public by default since an overriding protocol is likely to be used in a derived class.*

**Returns:**  *number of bytes sent.*

**Parameters:**

- *buf*

  *address of buffer to send.*

- *len*

  *of bytes to send.*

### void SocketPort::setDetectOutput (bool)` [protected]`

*Used to indicate if output ready monitoring should be performed by the service thread.*

### void SocketPort::setDetectPending (bool)` [protected]`

*Used to indicate if the service thread should monitor pending data for us.*

### void SocketPort::setTimer (timeout\_t timeout = 0)

*Derived setTimer to notify the service thread pool of change in expected timeout.*

*This allows* **SocketService** *to  reschedule all timers. Otherwise same as* **TimerPort***.*

**Parameters:**

- *timeout*

  *in milliseconds.*

*Reimplemented from* **TimerPort***.*

## FRIENDS AND RELATED FUNCTION DOCUMENTATION

### friend class SocketService` [friend]`

## AUTHOR

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