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

Preserves record boundaries when exchanging messages between connected TCP/IP sockets. More...

Public Member Functions

SOCKSIZE readLine (std::string &message)
 Read a message from a connected socket. More...
 
SOCKSIZE writeLine (const std::string &message)
 Send a message to a connected socket. More...
 
SOCKSIZE read (char *buffer, size_t len)
 Reads exactly len bytes from the socket, blocks otherwise. More...
 
SOCKSIZE write (const char *str, size_t len)
 Writes len bytes to the socket. More...
 
GNatSocketsocket ()
 Returns the associated socket.
 
 GNatSocketBuffer (GNatSocket *, size_t inputSize=8192, size_t ouputSize=8192)
 Constructor. More...
 
void setReadSeparator (int separ)
 Returns/changes the separator used by readLine(). More...
 
void setWriteSeparator (int separ)
 Returns/changes the separator used by writeLine(). More...
 

Detailed Description

Preserves record boundaries when exchanging messages between connected TCP/IP sockets.

Ensures that one call to readLine() corresponds to one and exactly one call to writeLine() on the other side. By default, writeLine() adds
at the end of each message and readLine() searches for
, or
so that it can retreive the entire record. Beware messages should thus not contain these charecters.

int main() {
Socket sock;
SocketBuffer sockbuf(sock);
int status = sock.connect("localhost", 3331);
if (status < 0) {
cerr << "Could not connect" << endl;
return 1;
}
while (cin) {
string request, response;
cout << "Request: ";
getline(cin, request);
if (sockbuf.writeLine(request) < 0) {
cerr << "Could not send message" << endl;
return 2;
}
if (sockbuf.readLine(response) < 0) {
cerr << "Couldn't receive message" << endl;
return 3;
}
}
return 0;
}

Constructor & Destructor Documentation

GNatSocketBuffer::GNatSocketBuffer ( GNatSocket sock,
size_t  inputSize = 8192,
size_t  ouputSize = 8192 
)

Constructor.

socket must be a connected TCP/IP Socket. It should not be deleted as long as the SocketBuffer is used. inputSize and ouputSize are the sizes of the buffers that are used internally for exchanging data.

Member Function Documentation

SOCKSIZE GNatSocketBuffer::readLine ( std::string &  message)

Read a message from a connected socket.

readLine() receives one (and only one) message sent by writeLine() on the other side, ie, a call to writeLine() corresponds to one and exactly one call to readLine() on the other side. The received data is stored in message. This method blocks until the message is fully received.

Returns
The number of bytes that were received or one of the following values:
  • 0: shutdownOutput() was called on the other side
  • Socket::Failed (-1): a connection error occured
  • Socket::InvalidSocket (-2): the socket is invalid.
Note
the separator (eg
) is counted in the value returned by readLine().
SOCKSIZE GNatSocketBuffer::writeLine ( const std::string &  message)

Send a message to a connected socket.

writeLine() sends a message that will be received by a single call of readLine() on the other side,

Returns
see readLine()
Note
if message contains one or several occurences of the separator, readLine() will be called as many times on the other side.
SOCKSIZE GNatSocketBuffer::read ( char *  buffer,
size_t  len 
)

Reads exactly len bytes from the socket, blocks otherwise.

Returns
see readLine()
SOCKSIZE GNatSocketBuffer::write ( const char *  str,
size_t  len 
)

Writes len bytes to the socket.

Returns
see readLine()
void GNatSocketBuffer::setReadSeparator ( int  separ)

Returns/changes the separator used by readLine().

setReadSeparator() changes the symbol used by readLine() to separate successive messages:

  • if separ < 0 (the default) readLine() searches for \n, \r or \n\r.
  • if separ >= 0, readLine() searches for this character to separate messages,
void GNatSocketBuffer::setWriteSeparator ( int  separ)

Returns/changes the separator used by writeLine().

setWriteSeparator() changes the character(s) used by writeLine() to separate successive messages:

  • if separ < 0 (the default) writeLine() inserts \n\r between successive lines.
  • if separ >= 0, writeLine() inserts separ between successive lines,

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