Text Stream States |
The next example illustrates the
detection of EOF using os_tstream . Note that due
to buffering in ANSI/ISO IOStreams, you must query the stream not the underlying
device.
// Example state4 server.
#include <iostream>
#include <ospace/network.h>
#include <ospace/stream.h>
void
main()
{
os_network_toolkit init_network;
os_streaming_toolkit init_streaming;
// Create connection server with well-known port on local host.
os_tcp_connection_server server( os_socket_address( 3007 ) );
os_tcp_socket socket;
server.accept( socket ); // Accept incoming connection.
os_tstream stream( socket );
cout << "stream.eof() = " << stream.eof() << endl;
int x;
stream >> x;
cout << "stream.eof() = " << stream.eof() << endl;
int y;
stream >> y;
cout << "stream.eof() = " << stream.eof() << endl;
}
stream.eof() = 0
stream.eof() = 0
stream.eof() = 1
// Example state4 client.
#include <iostream>
#include <ospace/network.h>
#include <ospace/stream.h>
void
main()
{
os_network_toolkit init_network;
os_streaming_toolkit init_streaming;
// Create with unique system assigned port number.
os_tcp_socket socket;
// Connect to server on my host.
socket.connect_to( os_socket_address( 3007 ) );
os_tstream stream( socket );
stream << 1 << endl;
}
< no output >
For os_tstream
to behave as expected in the ANSI IOStream world, exceptions are not
propagated from the underlying device. However, you can retrieve the event
information by using the device's last_exception()
method. You can clear the last error information by
using clear_exception() . The
next example illustrates the relationship between the state of os_tstream
and the state of the underlying device. The example also shows that exception
information is not always available.
#include <iostream>
#include <ospace/network.h>
#include <ospace/stream.h>
void
main()
{
os_network_toolkit init_network;
os_streaming_toolkit init_streaming;
os_tcp_socket socket;
os_tstream stream( socket );
cout << "stream.good() = " << stream.good() << endl;
cout << "socket.good() = " << socket.good() << endl;
// Socket problem, exception generated.
socket.close();
int x;
stream >> x;
cout << "stream.good() = " << stream.good() << endl;
cout << "socket.good() = " << socket.good() << endl;
os_tcp_socket_device* device =
(os_tcp_socket_device*) stream.device();
if ( device->last_exception() )
{
cout << "device->last_exception() = " << endl;
cout << '\t'<< *device->last_exception() << endl;
device->clear_exception();
}
}
stream.good() = 1
socket.good() = 1
stream.good() = 0
socket.good() = 0
device->last_exception() =
read_failed: error code = 9 [connsock.cpp:217]
Copyright©1994-2026 Recursion
Software LLC
All Rights Reserved - For use by licensed users only.