guit  0.1
 All Classes Functions Variables Typedefs Enumerations Friends
Public Types | Public Member Functions | List of all members
GNatSocket Class Reference

TCP/IP or UDP/Datagram IPv4 socket. More...

Public Types

enum  Errors
 Socket errors. More...
 

Public Member Functions

 GNatSocket (int type=SOCK_STREAM)
 Creates a new Socket. More...
 
 GNatSocket (int type, SOCKET sockfd)
 Creates a Socket from an existing socket file descriptor.
 
 ~GNatSocket ()
 Destructor (closes the socket).
 
int connect (const std::string &host, int port)
 Connects the socket to an address. More...
 
int bind (int port)
 Assigns the socket to localhost. More...
 
int bind (const std::string &host, int port)
 Assigns the socket to an IP address. More...
 
int close ()
 Closes the socket.
 
bool isClosed () const
 Returns true if the socket has been closed.
 
SOCKET descriptor ()
 Returns the descriptor of the socket.
 
void shutdownInput ()
 Disables further receive operations.
 
void shutdownOutput ()
 Disables further send operations.
 
SOCKSIZE send (const SOCKDATA *buf, size_t len, int flags=0)
 Send sdata to a connected (TCP/IP) socket. More...
 
SOCKSIZE receive (SOCKDATA *buf, size_t len, int flags=0)
 Receives data from a connected (TCP/IP) socket. More...
 
SOCKSIZE sendTo (void const *buf, size_t len, int flags, SOCKADDR const *to, socklen_t addrlen)
 Sends data to a datagram socket.
 
SOCKSIZE receiveFrom (void *buf, size_t len, int flags, SOCKADDR *from, socklen_t *addrlen)
 Receives data from datagram socket.
 
int setReceiveBufferSize (int size)
 Set the size of the TCP/IP input buffer.
 
int setReuseAddress (bool)
 Enable/disable the SO_REUSEADDR socket option.
 
int setSendBufferSize (int size)
 Set the size of the TCP/IP output buffer.
 
int setSoLinger (bool, int linger)
 Enable/disable SO_LINGER with the specified linger time in seconds.
 
int setSoTimeout (int timeout)
 Enable/disable SO_TIMEOUT with the specified timeout (in milliseconds).
 
int setTcpNoDelay (bool)
 Enable/disable TCP_NODELAY (turns on/off TCP coalescence).
 
int getReceiveBufferSize () const
 Return the size of the TCP/IP input buffer.
 
bool getReuseAddress () const
 Return SO_REUSEADDR state.
 
int getSendBufferSize () const
 Return the size of the TCP/IP output buffer.
 
bool getSoLinger (int &linger) const
 Return SO_LINGER state and the specified linger time in seconds.
 
int getSoTimeout () const
 Return SO_TIMEOUT value.
 
bool getTcpNoDelay () const
 Return TCP_NODELAY state.
 

Static Public Member Functions

static void startup ()
 initialisation and cleanup of sockets on Widows. More...
 

Detailed Description

TCP/IP or UDP/Datagram IPv4 socket.

AF_INET connections following the IPv4 Internet protocol are supported.

Note
  • ServerSocket should be used on the server side.
  • SIGPIPE signals are ignored when using Linux, BSD or MACOSX.
  • TCP/IP sockets do not preserve record boundaries but SocketBuffer solves this problem.

Member Enumeration Documentation

Socket errors.

  • Failed (-1): could not connect, could not bind, etc.
  • InvalidSocket (-2): invalid socket or wrong socket type
  • UnknownHost (-3): could not reach host

Constructor & Destructor Documentation

GNatSocket::GNatSocket ( int  type = SOCK_STREAM)

Creates a new Socket.

Creates a AF_INET socket using the IPv4 Internet protocol. Type can be:

  • SOCK_STREAM (the default) for TCP/IP connected stream sockets
  • SOCK_DGRAM for UDP/datagram sockets (available only or Unix/Linux)

Member Function Documentation

void GNatSocket::startup ( )
static

initialisation and cleanup of sockets on Widows.

Note
startup is automatically called when a Socket or a ServerSocket is created
int GNatSocket::connect ( const std::string &  host,
int  port 
)

Connects the socket to an address.

Typically used for connecting TCP/IP clients to a ServerSocket. On Unix/Linux host can be a hostname, on Windows it can only be an IP address.

Returns
0 on success or a negative value on error which is one of Socket::Errors
int GNatSocket::bind ( int  port)

Assigns the socket to localhost.

Returns
0 on success or a negative value on error, see Socket::Errors
int GNatSocket::bind ( const std::string &  host,
int  port 
)

Assigns the socket to an IP address.

On Unix/Linux host can be a hostname, on Windows it can only be an IP address.

Returns
0 on success or a negative value on error, see Socket::Errors
SOCKSIZE GNatSocket::send ( const SOCKDATA *  buf,
size_t  len,
int  flags = 0 
)
inline

Send sdata to a connected (TCP/IP) socket.

Sends the first len bytes in buf.

Returns
the number of bytes that were sent, or 0 or shutdownInput() was called on the other side, or Socket::Failed (-1) if an error occured.
Note
TCP/IP sockets do not preserve record boundaries, see SocketBuffer.
SOCKSIZE GNatSocket::receive ( SOCKDATA *  buf,
size_t  len,
int  flags = 0 
)
inline

Receives data from a connected (TCP/IP) socket.

Reads at most len bytes fand stores them in buf. By default, this function blocks the caller until thre is availbale data.

Returns
the number of bytes that were received, or 0 or shutdownOutput() was called on the other side, or Socket::Failed (-1) if an error occured.

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