PREV UP NEXT C++ socket classes for OS/2

9.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.

9.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 iosockstream, for more details on isockstream. See 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 (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 ();

9.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.cpp

#include <sockinet.h>



int main ()

{

    char buf[256];

    bisockinet is (sockbuf::sock_dgram);

    is->bind ();



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



    is.getline (buf);

    cout << buf << endl;



    return 0;

}


// tdinwrite.cpp--tdinwrite hostname portno

#include <sockinet.h>

#include <stdlib.h>



int main (int ac, char** av)

{

    bosockinet 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.cpp

#include <sockinet.h>



int main ()

{

    char  buf[1024];

    biosockinet 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;

}