Using RWSocket
The Berkeley sockets adapter is represented by the
RWSocket class. Each socket API function has a corresponding
RWSocket member function. In addition,
RWSocket member functions include:
• Strong typing, which lets you catch errors at compile time
• Default arguments, which make common usage easier
• Errors that are indicated by using exceptions—you do not have to check returns codes
• Simplified pass and return of socket addresses
• Encapsulation and overloading, which coalesce multiple socket calls into one call
For example, you can set up a server with a single call to listen()—you do not have to call socket() and bind() first.
Example 13 connects to a socket and sends it a line of output, without error handling.
Example 13 – Connecting to a socket
#include <rw/rstream.h>
#include <rw/network/RWSocket.h> //1
#include <rw/network/RWInetAddr.h> //2
#include <rw/network/RWWinSockInfo.h> //3
int main()
{
RWWinSockInfo info; //4
RWSocket p; //5
p.connect( RWInetAddr(3010,"net.roguewave.com") ); //6
p.sendAtLeast("Hello out there!"); //7
p.closesocket(); //8
return 0;
}