[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8. sockstream Classes

sockstream classes are designed in such a way that they provide the same interface as their stream counterparts do. We have isockstream derived from istream and osockstream derived from ostream. We also have iosockstream which is derived from iostream.

Each domain also has its own set of stream classes. For example, unix domain has isockunix, osockunix, and iosockunix derived from isockstream, osockstream, and iosockstream respectively. Similarly, inet domain has isockinet, osockinet, and iosockinet.

8.1 iosockstreams  Generic IOStream classes for sockbuf buffers.
8.2 iosockinet Stream Classes  IOStream classes for inet domain of sockets.
8.3 iosockunix Classes  IOStream classes for unix domain of sockets.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.1 iosockstreams


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.1.1 isockstream Class

Since isockstream is publicly derived from istream, most of the public functions of istream are also available in isockstream.

isockstream redefines rdbuf() defined in its virtual base class ios. Since, ios::rdbuf() is not virtual, care must be taken to call the correct rdbuf() through a reference or a pointer to an object of class isockstream.

In what follows,

isockstream is(sb)
Constructs an isockstream object is with sb as its sockbuf.

isockstream is(sbp)
Constructs an isockstream object is with *sbp as its sockbuf.

sbp = is.rdbuf()
returns a pointer to the sockbuf of the isockstream object is.

isockstream::operator -> ()
returns a pointer to the isockstream's sockbuf so that the user can use isockstream object as a sockbuf object.

 
    is->connect (sa); // same as is.rdbuf()->connect (sa);


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.1.2 osockstream Class

Since osockstream is publicly derived from ostream, most of the public functions of ostream are also available in osockstream.

osockstream redefines rdbuf() defined in its virtual base class ios. Since, ios::rdbuf() is not virtual, care must be taken to call the correct rdbuf() through a reference or a pointer to an object of class osockstream.

In what follows,

osockstream os(sb)
Constructs an osockstream object os with sb as its sockbuf.

osockstream os(sbp)
Constructs an osockstream object os with *sbp as its sockbuf.

sbp = os.rdbuf()
returns a pointer to the sockbuf of the osockstream object os.

osockstream::operator -> ()
returns a pointer to the osockstream's sockbuf so that the user can use osockstream object as a sockbuf object.

 
    os->connect (sa); // same as os.rdbuf()->connect (sa);


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.1.3 iosockstream Class

Since iosockstream is publicly derived from iostream, most of the public functions of iostream are also available in iosockstream.

iosockstream redefines rdbuf() defined in its virtual base class ios. Since, ios::rdbuf() is not virtual, care must be taken to call the correct rdbuf() through a reference or a pointer to an object of class iosockstream.

In what follows,

iosockstream io(sb)
Constructs an iosockstream object io with sb as its sockbuf.

iosockstream io(sbp)
Constructs an iosockstream object io with *sbp as its sockbuf.

sbp = io.rdbuf()
returns a pointer to the sockbuf of the iosockstream object io.

iosockstream::operator -> ()
returns a pointer to the iosockstream's sockbuf so that the user can use iosockstream object as a sockbuf object.

 
    io->connect (sa); // same as io.rdbuf()->connect (sa);


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.2 iosockinet Stream Classes

We discus only isockinet class here. osockinet and iosockinet are similar and are left out. However, they are covered in the examples that follow.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.2.1 isockinet

isockinet is used to handle interprocess communication in inet domain. It is derived from isockstream class and it uses a sockinetbuf as its stream buffer. See section 8.1 iosockstreams, for more details on isockstream. See section 4. sockinetbuf Class, for information on sockinetbuf.

In what follows,

isockinet is (ty, proto)
constructs an isockinet object is whose sockinetbuf buffer is of the type ty and has the protocol number proto. The default protocol number is 0.

isockinet is (sb)
constructs a isockinet object is whose sockinetbuf is sb. sb must be in inet domain.

isockinet is (sinp)
constructs a isockinet object is whose sockinetbuf is sinp.

sinp = is.rdbuf ()
returns a pointer to the sockinetbuf of isockinet object is.

isockinet::operator ->
returns sockinetbuf of sockinet so that the sockinet object acts as a smart pointer to sockinetbuf.

 
        is->localhost (); // same as is.rdbuf ()->localhost ();


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.2.2 iosockinet examples

The first pair of examples demonstrates datagram socket connections in the inet domain. First, tdinread prints its local host and local port on stdout and waits for input in the connection. tdinwrite is started with the local host and local port of tdinread as arguments. It sends the string "How do ye do!" to tdinread which in turn reads the string and prints on its stdout.

 
// tdinread.cc
#include <sockinet.h>

int main ()
{
    char buf[256];
    isockinet is (sockbuf::sock_dgram);
    is->bind ();

    cout << is->localhost() << ' ' << is->localport() << endl;

    is.getline (buf);
    cout << buf << endl;

    return 0;
}

 
// tdinwrite.cc--tdinwrite hostname portno
#include <sockinet.h>
#include <stdlib.h>

int main (int ac, char** av)
{
    osockinet os (sockbuf::sock_dgram);
    os->connect (av[1], atoi(av[2]));
    os << "How do ye do!" << endl;
    return 0;
}

The next example communicates with an nntp server through a sockbuf::sock_stream socket connection in inet domain. After establishing a connection to the nntp server, it sends a "HELP" command and gets back the HELP message before sending the "QUIT" command.

 
// tnntp.cc
#include <sockinet.h>

int main ()
{
    char  buf[1024];
    iosockinet io (sockbuf::sock_stream);
    io->connect ("murdoch.acc.virginia.edu", "nntp", "tcp");
    io.getline (buf, 1024); cout << buf << endl;
    io << "HELP\r\n" << flush;
    io.getline (buf, 1024); cout << buf << endl;
    while (io.getline (buf, 1024))
        if (buf[0] == '.' && buf[1] == '\r') break;
        else if (buf[0] == '.' && buf[1] == '.') cout << buf+1 << endl;
        else cout << buf << endl;
    io << "QUIT\r\n" << flush;
    io.getline (buf, 1024); cout << buf << endl;
    return 0;
}


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.3 iosockunix Classes

We discuss only isockunix here. osockunix and iosockunix are similar.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.3.1 isockunix class

isockunix is used to handle interprocess communication in unix domain. It is derived from isockstream class and it uses a sockunixbuf as its stream buffer. See section 8.1 iosockstreams, for more details on isockstream. See section 6. sockunixbuf Class, for information on sockunixbuf.

In what follows,

isockunix is (ty, proto)
constructs an isockunix object is whose sockunixbuf buffer is of the type ty and has the protocol number proto. The default protocol number is 0.

isockunix is (sb)
constructs a isockunix object is whose sockunixbuf is sb. sb must be in unix domain.

isockunix is (sinp)
constructs a isockunix object is whose sockunixbuf is sinp.

sinp = is.rdbuf ()
returns a pointer to the sockunixbuf of isockunix object is.

isockunix::operator ->
returns sockunixbuf of sockunix so that the sockunix object acts as a smart pointer to sockunixbuf.

 
        is->localhost (); // same as is.rdbuf ()->localhost ();


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.3.2 iosockunix examples

tsunread listens for connections. When tsunwrite requests connection, tsunread accepts it and waits for input. tsunwrite sends the string "Hello!!!" to tsunread. tsunread reads the string sent by tsunwrite and prints on its stdout.

 
// tsunread.cc
#include <sockunix.h>
#include <unistd.h>

int main ()
{
    sockunixbuf sunb (sockbuf::sock_stream);
    sunb.bind ("/tmp/socket+-");
    sunb.listen (2);
    isockunix is (sunb.accept ());
    char buf[32];
    is >> buf; cout << buf << endl;
    unlink ("/tmp/socket+-");
    return 0;
}

 
// tsunwrite.cc
#include <sockunix.h>
int main ()
{
    osockunix os (sockbuf::sock_stream);
    os->connect ("/tmp/socket++");
    os << "Hello!!!\n" << flush;
    return 0;
}


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Herbert Straub on June, 15 2005 using texi2html