The first example program wrote to a portal, but didn't read any input. The next example expands on the first example by printing the first packet of output returned by the socket.
#include <rw/rstream.h> #include <rw/toolpro/sockport.h> #include <rw/toolpro/inetaddr.h> #include <rw/toolpro/winsock.h> 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; }
The only new lines of code are lines //1 and //2.
//1 | We obtain the next packet of information that arrives at the socket. There is no way to tell in advance how much data will be returned in the string. |
//2 | Print whatever data arrived. |
A problem with this new program is that it does not always handle all of the data sent by the server. More data could arrive after the first packet was printed, and we would never know it. To fix this, replace line //1 with this loop:
RWCString reply,packet; while ( !(packet=p.recv()).isNull() ) { reply += packet; }
The revised version continues to receive data on the portal until the recv() function returns the null string, which happens when the server terminates the connection.
The wwwget example program uses code similar to this example to fetch a page of the World Wide Web using the HTTP protocol. Check it out for a real-life example of how to use the RWSocketPortal class.
©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.