sptk2 logo
SPTK Home Page

sptk::CSocket Class Reference
[Utility Classes]

Generic socket. More...

#include <CSocket.h>

Inheritance diagram for sptk::CSocket:

sptk::CFTPSocket sptk::CHttpConnect sptk::CImapConnect sptk::CSmtpConnect

List of all members.

Public Types

enum  CSocketOpenMode { SOM_CREATE, SOM_CONNECT, SOM_BIND }
 A mode to open a socket, one of. More...

Public Member Functions

 CSocket (int32_t domain=AF_INET, int32_t type=SOCK_STREAM, int32_t protocol=0)
 Constructor.
virtual ~CSocket ()
 Destructor.
int handle () const
 Returns socket handle.
void attach (SOCKET socketHandle)
 Attaches socket handle.
void host (std::string hostName)
 Sets the host name.
std::string host () const
 Returns the host name.
void port (int32_t portNumber)
 Sets the port number.
int32_t port () const
 Returns the current port number.
void open_addr (CSocketOpenMode openMode=SOM_CREATE, sockaddr_in *addr=0L)
 Opens the socket connection by address.
virtual void open (std::string hostName="", int32_t port=0, CSocketOpenMode openMode=SOM_CONNECT)
 Opens the client socket connection by host and port.
void listen (int32_t portNumber=0)
 Opens the server socket connection on port (binds/listens).
void accept (int &clientSocketFD, struct sockaddr_in &clientInfo)
 In server mode, waits for the incoming connection.
virtual void close ()
 Closes the socket connection.
bool active () const
 Returns the current socket state.
int32_t control (int flag, uint32_t *check)
 Calls Unix fcntl() or Windows ioctlsocket().
void setOption (int level, int option, int value) throw (sptk::CException)
 Sets socket option value Throws an error if not succeeded.
void getOption (int level, int option, int &value) throw (sptk::CException)
 Gets socket option value.
int32_t recv (void *buffer, uint32_t size)
 Reads data from the socket in regular or TLS mode.
int32_t send (const void *buffer, uint32_t len)
 Reads data from the socket in regular or TLS mode.
uint32_t readLine (char *buffer, uint32_t size)
 Reads one line (terminated with CRLF) from the socket into existing memory buffer.
uint32_t readLine (CBuffer &buffer)
 Reads one line (terminated with CRLF) from the socket into existing memory buffer.
uint32_t readLine (std::string &s)
 Reads one line (terminated with CRLF) from the socket into string.
uint32_t read (char *buffer, uint32_t size, sockaddr_in *from=NULL)
 Reads data from the socket.
uint32_t read (CBuffer &buffer, sockaddr_in *from=NULL)
 Reads data from the socket into memory buffer.
uint32_t write (const char *buffer, uint32_t size, const sockaddr_in *peer=0)
 Writes data to the socket.
uint32_t write (const CBuffer &buffer)
 Writes data to the socket.
bool readyToRead (uint32_t waitmsec)
 Reports true if socket is ready for reading from it.
bool readyToWrite ()
 Reports true if socket is ready for writing to it.
CSocketoperator<< (const std::string &)
 Stream std::string input.
CSocketoperator>> (std::string &)
 Stream std::string output.

Protected Member Functions

char getChar ()
 Reads a single char from the socket.

Protected Attributes

SOCKET m_sockfd
 Socket internal (OS) handle.
int32_t m_domain
 Socket domain type.
int32_t m_type
 Socket type.
int32_t m_protocol
 Socket protocol.
std::string m_host
 Host name.
int32_t m_port
 Port number.
fd_set m_inputs
 The set of socket descriptors for reading.
fd_set m_outputs
 The set of socket descriptors for writing.
CSocketReader m_reader
 Socket buffered reader.
CBuffer m_stringBuffer
 Buffer to read a line.


Detailed Description

Generic socket.

Allows to establish a network connection to the host by name and port address


Member Enumeration Documentation

A mode to open a socket, one of.

Enumerator:
SOM_CREATE  Only create (Typical UDP connectionless socket).
SOM_CONNECT  Connect.
SOM_BIND  Bind (listen).


Constructor & Destructor Documentation

sptk::CSocket::CSocket ( int32_t  domain = AF_INET,
int32_t  type = SOCK_STREAM,
int32_t  protocol = 0 
)

Constructor.

Parameters:
domain int32_t, socket domain type
type int32_t, socket type
protocol int32_t, protocol type


Member Function Documentation

void sptk::CSocket::accept ( int &  clientSocketFD,
struct sockaddr_in &  clientInfo 
)

In server mode, waits for the incoming connection.

When incoming connection is made, exits returning the connection info

Parameters:
clientSocketFD int&, connected client socket FD
clientInfo sockaddr_in&, connected client info

bool sptk::CSocket::active (  )  const [inline]

Returns the current socket state.

Returns:
true if socket is opened

References m_sockfd.

Referenced by sptk::CFTPConnect::active().

void sptk::CSocket::attach ( SOCKET  socketHandle  ) 

Attaches socket handle.

Parameters:
socketHandle SOCKET, existing socket handle

void sptk::CSocket::getOption ( int  level,
int  option,
int &  value 
) throw (sptk::CException)

Gets socket option value.

Throws an error if not succeeded

void sptk::CSocket::host ( std::string  hostName  ) 

Sets the host name.

Parameters:
hostName std::string, the host name

Referenced by sptk::CImapDS::host().

void sptk::CSocket::listen ( int32_t  portNumber = 0  ) 

Opens the server socket connection on port (binds/listens).

Parameters:
portNumber int32_t, the port number

virtual void sptk::CSocket::open ( std::string  hostName = "",
int32_t  port = 0,
CSocketOpenMode  openMode = SOM_CONNECT 
) [virtual]

Opens the client socket connection by host and port.

Parameters:
hostName std::string, the host name
port int32_t, the port number
openMode CSocketOpenMode, socket open mode

void sptk::CSocket::open_addr ( CSocketOpenMode  openMode = SOM_CREATE,
sockaddr_in *  addr = 0L 
)

Opens the socket connection by address.

Parameters:
openMode CSocketOpenMode, SOM_CREATE for UDP socket, SOM_BIND for the server socket, and SOM_CONNECT for the client socket
addr sockaddr_in*, defines socket address/port information

int32_t sptk::CSocket::port (  )  const [inline]

Returns the current port number.

Returns:
port number

References m_port.

void sptk::CSocket::port ( int32_t  portNumber  ) 

Sets the port number.

Parameters:
portNumber int32_t, the port number

uint32_t sptk::CSocket::read ( CBuffer buffer,
sockaddr_in *  from = NULL 
)

Reads data from the socket into memory buffer.

Parameters:
buffer const CBuffer&, the memory buffer
from sockaddr_in*, an optional structure for source address
Returns:
the number of bytes read from the socket

uint32_t sptk::CSocket::read ( char *  buffer,
uint32_t  size,
sockaddr_in *  from = NULL 
)

Reads data from the socket.

Parameters:
buffer char *, the memory buffer
size uint32_t, the memory buffer size
from sockaddr_in*, an optional structure for source address
Returns:
the number of bytes read from the socket

uint32_t sptk::CSocket::readLine ( std::string &  s  ) 

Reads one line (terminated with CRLF) from the socket into string.

Parameters:
s std::string&, the destination string
Returns:
the number of bytes read from the socket

uint32_t sptk::CSocket::readLine ( CBuffer buffer  ) 

Reads one line (terminated with CRLF) from the socket into existing memory buffer.

The memory buffer is extended automatically to fit the string.

Parameters:
buffer CBuffer&, the destination buffer
Returns:
the number of bytes read from the socket

uint32_t sptk::CSocket::readLine ( char *  buffer,
uint32_t  size 
)

Reads one line (terminated with CRLF) from the socket into existing memory buffer.

The output string should fit the buffer or it will be returned incomplete.

Parameters:
buffer char *, the destination buffer
size uint32_t, the destination buffer size
Returns:
the number of bytes read from the socket

bool sptk::CSocket::readyToRead ( uint32_t  waitmsec  ) 

Reports true if socket is ready for reading from it.

Parameters:
waitmsec uint32_t, read timeout in msec

int32_t sptk::CSocket::recv ( void *  buffer,
uint32_t  size 
)

Reads data from the socket in regular or TLS mode.

Parameters:
buffer void *, the destination buffer
size uint32_t, the destination buffer size
Returns:
the number of bytes read from the socket

int32_t sptk::CSocket::send ( const void *  buffer,
uint32_t  len 
)

Reads data from the socket in regular or TLS mode.

Parameters:
buffer const void *, the send buffer
size uint32_t, the send data length
Returns:
the number of bytes sent the socket

uint32_t sptk::CSocket::write ( const CBuffer buffer  ) 

Writes data to the socket.

Parameters:
buffer const CBuffer&, the memory buffer
Returns:
the number of bytes written to the socket

uint32_t sptk::CSocket::write ( const char *  buffer,
uint32_t  size,
const sockaddr_in *  peer = 0 
)

Writes data to the socket.

Parameters:
buffer const char *, the memory buffer
size uint32_t, the memory buffer size
peer const sockaddr_in*, optional peer information
Returns:
the number of bytes written to the socket


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

Mon Mar 5 11:56:07 2012: SPTK 3.5.7