Reading From a Portal
Example 3 expands on
Example 2 by printing the first packet of output returned by the socket.
Example 3 – Reading from a portal
#include <rw/rstream.h>
#include <rw/network/RWSocketPortal.h>
#include <rw/network/RWInetAddr.h>
#include <rw/network/RWWinSockInfo.h>
int main()
{
RWWinSockInfo info;
try {
RWSocketPortal p( RWInetAddr(3010,"net.roguewave.com") );
p.sendAtLeast("Hello out there!");
RWCString reply = p.recv(); //1
cout << reply; //2
}
catch (const RWxmsg& x) {
cerr << "Problem! " << x.why() << endl;
}
return 0;
}
This example does not always handle all data sent by the server. More data could arrive after the first packet is printed. To handle more data, replace line //1 with this loop:
RWCString reply,packet;
while ( !(packet=p.recv()).isNull() ) {
reply += packet;
}
This loop enables the code to receive data on the portal until the recv() function returns the null string, which happens when the server terminates the connection.
See the
wwwget sample program for an example of how to use the
RWSocketPortal class. It uses code similar to this example to fetch a page of the World Wide Web using the HTTP protocol.