forest-net
an overlay networks for large-scale virtual worlds
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
forest::Np4d Class Reference

Network programming for dummies. More...

#include <Np4d.h>

Static Public Member Functions

static ipa_t ipAddress (const char *)
 Return the IP address for the string pointed to by ips. More...
 
static string ip2string (ipa_t)
 Create a string representation of an IP address. More...
 
static bool readIpAdr (istream &, ipa_t &)
 If next thing on the current line is an ip address, return it in ipa and return true. More...
 
static ipa_t getIpAdr (const char *)
 Get the default IP address of a specified host. More...
 
static ipa_t myIpAddress ()
 Get the default IP address of this host. More...
 
static void initSockAdr (ipa_t, ipp_t, sockaddr_in *)
 Initialize a socket address structure with a specified IP address and port number. More...
 
static void extractSockAdr (sockaddr_in *, ipa_t &, ipp_t &)
 Extract an IP address and port number from a socket address structure. More...
 
static ipp_t getSockPort (int)
 Get the local port number associated with a given socket. More...
 
static ipa_t getSockIp (int)
 Get the local IP address associated with a given socket. More...
 
static ipa_t getPeerIp (int)
 Get the IP address of the peer for a given socket. More...
 
static int datagramSocket ()
 Open a datagram socket. More...
 
static int streamSocket ()
 Open a stream socket. More...
 
static bool bind4d (int, ipa_t, ipp_t)
 Bind a socket to a specified address and port. More...
 
static bool listen4d (int)
 Listen for a stream connection. More...
 
static int accept4d (int)
 Accept the next waiting connection request. More...
 
static int accept4d (int, ipa_t &, ipp_t &)
 Accept the next waiting connection request. More...
 
static bool connect4d (int, ipa_t, ipp_t)
 Connect to a remote host. More...
 
static bool nonblock (int)
 Configure a socket to be nonblocking. More...
 
static int sendto4d (int, void *, int, ipa_t, ipp_t)
 Send a datagram to a remote host. More...
 
static int recv4d (int, void *, int)
 Receive a datagram from a remote host. More...
 
static int recvfrom4d (int, void *, int, ipa_t &, ipp_t &)
 Receive a datagram from a remote host. More...
 
static bool hasData (int)
 Test a socket to see if it has data to be read. More...
 
static int dataAvail (int)
 Determine the amount of data available for reading from socket. More...
 
static int spaceAvail (int)
 Determine the space available for writing on a socket. More...
 
static bool recvInt (int, uint32_t &)
 Read a 32 bit integer from a stream socket. More...
 
static bool sendInt (int, uint32_t)
 Send a 32 bit integer on a stream socket. More...
 
static bool recvIntBlock (int, uint32_t &)
 
static bool sendIntBlock (int, uint32_t)
 
static bool recvIntVec (int, uint32_t *, int)
 Receive a vector of 32 bit integers on a stream socket. More...
 
static bool sendIntVec (int, uint32_t *, int)
 Send a vector of 32 bit integers on a stream socket. More...
 
static int recvBuf (int, char *, int)
 Receive a "chunk" of data on a stream socket. More...
 
static int sendBuf (int, char *, int)
 
static int recvBufBlock (int, char *, int)
 
static int sendBufBlock (int, char *, int)
 
static int sendString (int, const string &)
 Send a string over a blocking stream socket. More...
 

Detailed Description

Network programming for dummies.

This class defines a library of routines for IPv4 network programming, that hides much of the ugliness of the standard system calls. The big advantage is that you can avoid socket address structures completely.

Many of the routines are just wrappers on the standard system calls with more intuitive interfaces.

Definition at line 34 of file Np4d.h.

Member Function Documentation

int forest::Np4d::accept4d ( int  sock)
static

Accept the next waiting connection request.

Uses accept system call but hides the ugliness.

Parameters
sockis socket number
Returns
the socket number of the new connection or -1 on failure

Definition at line 182 of file Np4d.cpp.

Here is the caller graph for this function:

int forest::Np4d::accept4d ( int  sock,
ipa_t &  ipa,
ipp_t &  ipp 
)
static

Accept the next waiting connection request.

Uses accept system call but hides the ugliness.

Parameters
sockis socket number
ipais a reference to an IP address; on return its value is the address of the remote host
ippis a reference to an IP port number; on return its value is the port number of the remote host
Returns
the socket number of the new connection or -1 on failure

Definition at line 193 of file Np4d.cpp.

Here is the call graph for this function:

bool forest::Np4d::bind4d ( int  sock,
ipa_t  ipa,
ipp_t  ipp 
)
static

Bind a socket to a specified address and port.

Uses bind system call but hides the ugliness.

Parameters
sockis socket number
ipais IP address to bind to
ippis IP port to bind to
Returns
true on success, false on failure

Definition at line 161 of file Np4d.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

bool forest::Np4d::connect4d ( int  sock,
ipa_t  ipa,
ipp_t  ipp 
)
static

Connect to a remote host.

Uses connect system call but hides the ugliness.

Parameters
sockis socket number
ipais IP address of remote host
ippis IP port or the remote host
Returns
true on success, false on failure

Definition at line 208 of file Np4d.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

int forest::Np4d::dataAvail ( int  sock)
static

Determine the amount of data available for reading from socket.

Parameters
isthe socket number
Returns
the number of bytes available for reading or -1 on an error

Definition at line 272 of file Np4d.cpp.

Here is the caller graph for this function:

int forest::Np4d::datagramSocket ( )
static

Open a datagram socket.

Returns
socket number or 0 on failure

Definition at line 143 of file Np4d.cpp.

Here is the caller graph for this function:

void forest::Np4d::extractSockAdr ( sockaddr_in *  sap,
ipa_t &  ipa,
ipp_t &  ipp 
)
static

Extract an IP address and port number from a socket address structure.

Parameters
sapis pointer to the IP address structure
ipais a reference to an IP address
ippis a reference to a port number

Definition at line 90 of file Np4d.cpp.

Here is the caller graph for this function:

ipa_t forest::Np4d::getIpAdr ( const char *  hostName)
static

Get the default IP address of a specified host.

Parameters
hostNameis the anme of the host
Returns
an IP address in host byte order, or 0 on failure

Definition at line 57 of file Np4d.cpp.

Here is the caller graph for this function:

ipa_t forest::Np4d::getPeerIp ( int  sock)
static

Get the IP address of the peer for a given socket.

Parameters
sockis the number of a connected TCP socket
Returns
the address of the peer, or 0 on failure

Definition at line 121 of file Np4d.cpp.

ipa_t forest::Np4d::getSockIp ( int  sock)
static

Get the local IP address associated with a given socket.

Parameters
sockis the number of an open socket
Returns
the address of the socket, or 0 on failure

Definition at line 110 of file Np4d.cpp.

Here is the caller graph for this function:

ipp_t forest::Np4d::getSockPort ( int  sock)
static

Get the local port number associated with a given socket.

Parameters
sockis the number of an open socket
Returns
the port number of the socket, or 0 on failure

Definition at line 99 of file Np4d.cpp.

Here is the caller graph for this function:

bool forest::Np4d::hasData ( int  sock)
static

Test a socket to see if it has data to be read.

Uses the poll system call.

Parameters
sockis the socket to be tested
Returns
true if the socket has data to be read, else false

Definition at line 262 of file Np4d.cpp.

void forest::Np4d::initSockAdr ( ipa_t  ipa,
ipp_t  port,
sockaddr_in *  sap 
)
static

Initialize a socket address structure with a specified IP address and port number.

Parameters
ipais an IP address
ippis a port number
sapis pointer to the IP address structure

Definition at line 78 of file Np4d.cpp.

Here is the caller graph for this function:

string forest::Np4d::ip2string ( ipa_t  ipa)
static

Create a string representation of an IP address.

Parameters
ipais the IP address in host byte order
Returns
the string representing ipa

Definition at line 27 of file Np4d.cpp.

Here is the caller graph for this function:

ipa_t forest::Np4d::ipAddress ( const char *  ips)
static

Return the IP address for the string pointed to by ips.

Return 0 if string does not represent a valid IP address

Definition at line 17 of file Np4d.cpp.

Here is the caller graph for this function:

bool forest::Np4d::listen4d ( int  sock)
static

Listen for a stream connection.

Uses the listen system call but hides details.

Parameters
sockis socket number
Returns
true on success, false on failure

Definition at line 175 of file Np4d.cpp.

Here is the caller graph for this function:

ipa_t forest::Np4d::myIpAddress ( )
static

Get the default IP address of this host.

Returns
the IP address in host byte order, or 0 on failure

Definition at line 66 of file Np4d.cpp.

Here is the call graph for this function:

bool forest::Np4d::nonblock ( int  sock)
static

Configure a socket to be nonblocking.

Parameters
sockis the socket number
Returns
true on success, false on failure

Definition at line 132 of file Np4d.cpp.

Here is the caller graph for this function:

bool forest::Np4d::readIpAdr ( istream &  in,
ipa_t &  ipa 
)
static

If next thing on the current line is an ip address, return it in ipa and return true.

Otherwise, return false. The expected format is dotted-decimal. The address is returned in host byte order.

Definition at line 40 of file Np4d.cpp.

Here is the caller graph for this function:

int forest::Np4d::recv4d ( int  sock,
void *  buf,
int  leng 
)
static

Receive a datagram from a remote host.

Uses recv system call.

Parameters
sockis socket number
bufis pointer to the buffer containing the packet payload
lengis the maximum number of bytes to be stored in buf
Returns
the number of bytes received, or -1 on failure

Definition at line 234 of file Np4d.cpp.

Here is the caller graph for this function:

int forest::Np4d::recvBuf ( int  sock,
char *  buf,
int  buflen 
)
static

Receive a "chunk" of data on a stream socket.

This method is intended for use on nonblocking sockets. It expects the host on the other end of the socket to send a 32 bit integer, which represents the size of the next chunk of data to expect, followed by the chunk of data. The method reads the size information, then reads the specified amount of data into the provided buffer. For example, to send 20 bytes of data, the remote host sends the 32 bit integer 20, followed by the 20 bytes of data, for a total of 24 bytes.

Parameters
sockis the number of the socket
isa pointer to a buffer
buflenspecifies the size of the buffer
Returns
the number of bytes actually read on success; 0 if the underlying recv call returned 0, or -1 on an error or if the next complete chunk is not available

If the chunk size specified in the input stream exceeds buflen, the method will only read and return buflen bytes.

Definition at line 416 of file Np4d.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

int forest::Np4d::recvfrom4d ( int  sock,
void *  buf,
int  leng,
ipa_t &  ipa,
ipp_t &  ipp 
)
static

Receive a datagram from a remote host.

Uses recvfrom system call but hides the ugliness.

Parameters
sockis socket number
bufis pointer to the buffer containing the packet payload
lengis the maximum number of bytes to be stored in buf
ipais a reference to an IP address; on return, it contains the address of the remote host
ippis a reference to an IP port; on return, it contains the port number of the remote host
Returns
the number of bytes received, or -1 on failure

Definition at line 249 of file Np4d.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

bool forest::Np4d::recvInt ( int  sock,
uint32_t &  val 
)
static

Read a 32 bit integer from a stream socket.

Uses recv() system call but hides the ugliness. Intended for use with nonblocking socket. Will fail if the socket buffer has less than 4 bytes available to read.

Parameters
sockis socket number
valis a reference argument; on return it will have the value read
Returns
true on success, false on failure

Definition at line 310 of file Np4d.cpp.

Here is the call graph for this function:

bool forest::Np4d::recvIntVec ( int  ,
uint32_t *  ,
int   
)
static

Receive a vector of 32 bit integers on a stream socket.

Parameters
sockis socket number
vecis an array in which values are to be stored
lengthis the number of elements in vec
Returns
true if all expected values are received, else false

After a successful return, vec will contain the received integers on host byte order. After an unsuccessful return, the value of vec is undefined.

Definition at line 368 of file Np4d.cpp.

Here is the call graph for this function:

bool forest::Np4d::sendInt ( int  sock,
uint32_t  val 
)
static

Send a 32 bit integer on a stream socket.

Uses send() system call but hides the ugliness.

Parameters
sockis socket number
valis value to be sent
Returns
true on success, false on failure

Definition at line 342 of file Np4d.cpp.

Here is the call graph for this function:

bool forest::Np4d::sendIntVec ( int  ,
uint32_t *  ,
int   
)
static

Send a vector of 32 bit integers on a stream socket.

Uses send() system call but hides the ugliness.

Parameters
sockis socket number
vecis array of values to be sent
lengthis number of elements in vec
Returns
true on success, false on failure

Definition at line 386 of file Np4d.cpp.

Here is the call graph for this function:

int forest::Np4d::sendString ( int  sock,
const string &  s 
)
static

Send a string over a blocking stream socket.

Parameters
sockis an open, blocking stream socket
sis the string to be sent
Returns
the number of bytes sent from the string on success, -1 on failure

Definition at line 482 of file Np4d.cpp.

Here is the caller graph for this function:

int forest::Np4d::sendto4d ( int  sock,
void *  buf,
int  leng,
ipa_t  ipa,
ipp_t  ipp 
)
static

Send a datagram to a remote host.

Uses sendto system call but hides the ugliness.

Parameters
sockis socket number
bufis pointer to the buffer containing the packet payload
lengis the number of bytes to be sent
ipais IP address of remote host
ippis IP port or the remote host
Returns
number of bytes sent, or -1 on failure

Definition at line 222 of file Np4d.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

int forest::Np4d::spaceAvail ( int  sock)
static

Determine the space available for writing on a socket.

Parameters
isthe socket number
Returns
the number of bytes that can be written before the socket buffer becomes full, or -1 on error

Definition at line 288 of file Np4d.cpp.

Here is the caller graph for this function:

int forest::Np4d::streamSocket ( )
static

Open a stream socket.

Returns
socket number or 0 on failure

Definition at line 150 of file Np4d.cpp.

Here is the caller graph for this function:


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