CSL  6.0
TCPSocket Class Reference

#include <PracticalSocket.h>

+ Inheritance diagram for TCPSocket:

Public Member Functions

 TCPSocket () throw (SocketException)
 
 TCPSocket (const string &foreignAddress, unsigned short foreignPort) throw (SocketException)
 
void connect (const string &foreignAddress, unsigned short foreignPort) throw (SocketException)
 
void send (const void *buffer, int bufferLen) throw (SocketException)
 
int recv (void *buffer, int bufferLen) throw (SocketException)
 
string getForeignAddress () throw (SocketException)
 
unsigned short getForeignPort () throw (SocketException)
 
string getLocalAddress () throw (SocketException)
 
unsigned short getLocalPort () throw (SocketException)
 
void setLocalPort (unsigned short localPort) throw (SocketException)
 
void setLocalAddressAndPort (const string &localAddress, unsigned short localPort=0) throw (SocketException)
 

Static Public Member Functions

static void cleanUp () throw (SocketException)
 
static unsigned short resolveService (const string &service, const string &protocol="tcp")
 

Protected Attributes

int sockDesc
 

Private Member Functions

 TCPSocket (int newConnSD)
 

Friends

class TCPServerSocket
 

Detailed Description

TCP socket for communication with other TCP sockets

Definition at line 189 of file PracticalSocket.h.

Constructor & Destructor Documentation

TCPSocket::TCPSocket ( )
throw (SocketException
)

Construct a TCP socket with no connection

Exceptions
SocketExceptionthrown if unable to create TCP socket

Definition at line 238 of file PracticalSocket.cpp.

TCPSocket::TCPSocket ( const string &  foreignAddress,
unsigned short  foreignPort 
)
throw (SocketException
)

Construct a TCP socket with a connection to the given foreign address and port

Parameters
foreignAddressforeign address (IP address or name)
foreignPortforeign port
Exceptions
SocketExceptionthrown if unable to create TCP socket

Definition at line 243 of file PracticalSocket.cpp.

TCPSocket::TCPSocket ( int  newConnSD)
private

Definition at line 248 of file PracticalSocket.cpp.

Member Function Documentation

void CommunicatingSocket::connect ( const string &  foreignAddress,
unsigned short  foreignPort 
)
throw (SocketException
)
inherited

Attempt to establish a socket connection with the given foreign address and port

Parameters
foreignAddressforeign address (IP address or name)
foreignPortforeign port
Returns
true if connection successfully established
Exceptions
SocketExceptionthrown if unable to establish connection

Definition at line 186 of file PracticalSocket.cpp.

References fillAddr().

Referenced by UDPSocket::disconnect().

void CommunicatingSocket::send ( const void *  buffer,
int  bufferLen 
)
throw (SocketException
)
inherited

Write the given buffer to this socket. Call connect() before calling send()

Parameters
bufferbuffer to be written
bufferLennumber of bytes from buffer to be written
Returns
true if the send is successful
Exceptions
SocketExceptionthrown if unable to send data

Definition at line 198 of file PracticalSocket.cpp.

int CommunicatingSocket::recv ( void *  buffer,
int  bufferLen 
)
throw (SocketException
)
inherited

Read into the given buffer up to bufferLen bytes data from this socket. Call connect() before calling recv()

Parameters
bufferbuffer to receive the data
bufferLenmaximum number of bytes to read into buffer
Returns
number of bytes read, 0 for EOF, and -1 for error
Exceptions
SocketExceptionthrown if unable to receive data

Definition at line 205 of file PracticalSocket.cpp.

string CommunicatingSocket::getForeignAddress ( )
throw (SocketException
)
inherited

Get the foreign address. Call connect() before calling recv()

Returns
foreign address
Exceptions
SocketExceptionthrown if unable to fetch foreign address

Definition at line 215 of file PracticalSocket.cpp.

References Socket::sockDesc.

unsigned short CommunicatingSocket::getForeignPort ( )
throw (SocketException
)
inherited

Get the foreign port. Call connect() before calling recv()

Returns
foreign port
Exceptions
SocketExceptionthrown if unable to fetch foreign port

Definition at line 226 of file PracticalSocket.cpp.

References Socket::sockDesc.

string Socket::getLocalAddress ( )
throw (SocketException
)
inherited

Get the local address

Returns
local address of socket
Exceptions
SocketExceptionthrown if fetch fails

Definition at line 115 of file PracticalSocket.cpp.

unsigned short Socket::getLocalPort ( )
throw (SocketException
)
inherited

Get the local port

Returns
local port of socket
Exceptions
SocketExceptionthrown if fetch fails

Definition at line 125 of file PracticalSocket.cpp.

void Socket::setLocalPort ( unsigned short  localPort)
throw (SocketException
)
inherited

Set the local port to the specified port and the local address to any interface

Parameters
localPortlocal port
Exceptions
SocketExceptionthrown if setting local port fails

Definition at line 135 of file PracticalSocket.cpp.

void Socket::setLocalAddressAndPort ( const string &  localAddress,
unsigned short  localPort = 0 
)
throw (SocketException
)
inherited

Set the local port to the specified port and the local address to the specified address. If you omit the port, a random port will be selected.

Parameters
localAddresslocal address
localPortlocal port
Exceptions
SocketExceptionthrown if setting local port or address fails

Definition at line 148 of file PracticalSocket.cpp.

References fillAddr().

void Socket::cleanUp ( )
throw (SocketException
)
staticinherited

If WinSock, unload the WinSock DLLs; otherwise do nothing. We ignore this in our sample client code but include it in the library for completeness. If you are running on Windows and you are concerned about DLL resource consumption, call this after you are done with all Socket instances. If you execute this on Windows while some instance of Socket exists, you are toast. For portability of client code, this is an empty function on non-Windows platforms so you can always include it.

Parameters
bufferbuffer to receive the data
bufferLenmaximum number of bytes to read into buffer
Returns
number of bytes read, 0 for EOF, and -1 for error
Exceptions
SocketExceptionthrown WinSock clean up fails

Definition at line 159 of file PracticalSocket.cpp.

unsigned short Socket::resolveService ( const string &  service,
const string &  protocol = "tcp" 
)
staticinherited

Resolve the specified service for the specified protocol to the corresponding port number in host byte order

Parameters
serviceservice to resolve (e.g., "http")
protocolprotocol of service to resolve. Default is "tcp".

Definition at line 167 of file PracticalSocket.cpp.

Friends And Related Function Documentation

friend class TCPServerSocket
friend

Definition at line 209 of file PracticalSocket.h.

Member Data Documentation


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