Home Information Classes Download Usage Mail List Requirements Links Tutorial

# Socket Class Reference

STK TCP socket client/server class. More...

`#include <Socket.h>`

Inheritance diagram for Socket:

List of all members.

## Public Member Functions

| Socket (int port=2006)
| Default constructor which creates a local socket server on port 2006 (or the specified port number).
| Socket (int port, const char *hostname)
| Class constructor which creates a socket client connection to the specified host and port.
| ~Socket ()
| The class destructor closes the socket instance, breaking any existing connections.
int | connect (int port, const char *hostname="localhost")
| Connect a socket client to the specified host and port and returns the resulting socket descriptor.
void | close (void)
| Close this socket.
int | socket (void) const
| Return the server/client socket descriptor.
int | port (void) const
| Return the server/client port number.
int | accept (void)
| If this is a socket server, extract the first pending connection request from the queue and create a new connection, returning the descriptor for the accepted socket.
int | writeBuffer (const void *buffer, long bufferSize, int flags=0)
| Write a buffer over the socket connection. Returns the number of bytes written or -1 if an error occurs.
int | readBuffer (void *buffer, long bufferSize, int flags=0)
| Read a buffer from the socket connection, up to length bufferSize. Returns the number of bytes read or -1 if an error occurs.

## Static Public Member Functions

void | setBlocking (int socket, bool enable)
| If enable = false, the socket is set to non-blocking mode. When first created, sockets are by default in blocking mode.
void | close (int socket)
| Close the socket with the given descriptor.
bool | isValid (int socket)
| Returns TRUE is the socket descriptor is valid.
int | writeBuffer (int socket, const void *buffer, long bufferSize, int flags)
| Write a buffer via the specified socket. Returns the number of bytes written or -1 if an error occurs.
int | readBuffer (int socket, void *buffer, long bufferSize, int flags)
| Read a buffer via the specified socket. Returns the number of bytes read or -1 if an error occurs.

## Detailed Description

STK TCP socket client/server class.

This class provides a uniform cross-platform TCP socket client or socket server interface. Methods are provided for reading or writing data buffers to/from connections. This class also provides a number of static functions for use with external socket descriptors.

The user is responsible for checking the values returned by the read/write methods. Values less than or equal to zero indicate a closed or lost connection or the occurence of an error.

by Perry R. Cook and Gary P. Scavone, 1995 - 2002.

Definition at line 26 of file Socket.h.

## Constructor & Destructor Documentation

Socket::Socket | ( | int | port = 2006 | ) |

| Default constructor which creates a local socket server on port 2006 (or the specified port number). An StkError will be thrown if a socket error occurs during instantiation.

Socket::Socket | ( | int | port,
|  | const char * | hostname
| ) |

| Class constructor which creates a socket client connection to the specified host and port. An StkError will be thrown if a socket error occurs during instantiation.

## Member Function Documentation

int Socket::connect | ( | int | port,
|  | const char * | hostname = "localhost"
| ) |

Connect a socket client to the specified host and port and returns the resulting socket descriptor.

This method is valid for socket clients only. If it is called for a socket server, -1 is returned. If the socket client is already connected, that connection is terminated and a new connection is attempted. Server connections are made using the accept() method. An StkError will be thrown if a socket error occurs during instantiation.

See also:
  accept

int Socket::accept | ( | void |  | ) |

| If this is a socket server, extract the first pending connection request from the queue and create a new connection, returning the descriptor for the accepted socket. If no connection requests are pending and the socket has not been set non-blocking, this function will block until a connection is present. If an error occurs or this is a socket client, -1 is returned.

The documentation for this class was generated from the following file:

- Socket.h

The Synthesis ToolKit in C++ (STK)
©1995-2004 Perry R. Cook and Gary P. Scavone. All Rights Reserved.