Using Streaming<ToolKit> with a TCP Socket |
The os_tcp_socket
class has an associated device adapter for use with Streaming<ToolKit>.
With an automatic conversion operator, you can supply an os_tcp_socket
whenever an os_adapter is expected. Refer to Streaming
The following example shows how to
create an os_bstream on a TCP socket for use in
sending ANSI/ISO string objects. The server process accepts, reads a chemical
symbol, looks up the element associated with the chemical, and writes the
chemical name to the socket.
// Example bstream server.
#include <iostream>
#include <map>
#include <string>
#include <ospace/network.h>
#include <ospace/stream.h>
#include <ospace/uss/std/string>
void
main()
{
os_network_toolkit init_network;
os_streaming_toolkit init_stream;
// Initialize STL map of elements.
map< string, string, less< string > > elements;
elements[ "Ca" ] = "Calcium";
elements[ "Au" ] = "Gold";
elements[ "K" ] = "Potassium";
os_tcp_connection_server server( os_socket_address( 3004 ) );
while ( true ) // loop forever
{
os_tcp_socket socket;
server.accept( socket ); // Accept client connection.
os_bstream stream( socket );
string key;
stream >> key; // Read string object.
stream << elements[ key ]; // Return associated term, if any.
}
}
< no output >
// Example bstream client.
#include <iostream>
#include <string>
#include <ospace/network.h>
#include <ospace/stream.h>
#include <ospace/uss/std/string>
void
main()
{
os_network_toolkit init_network;
os_streaming_toolkit init_stream;
// Create socket, then bind to port 3004 on my host.
os_tcp_socket socket;
socket.connect_to( os_socket_address( 3004 ) );
os_bstream stream( socket );
string symbol = "Au";
stream << symbol; // Send string object via socket.
string result;
stream >> result; // Read response from socket into string.
cout << symbol << " means " << result << endl;
}
Au means Gold
Copyright©1994-2026 Recursion
Software LLC
All Rights Reserved - For use by licensed users only.