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

9.3: iosockunix Classes

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

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

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

#include <sockunix.h>

#include <unistd.h>



int main ()

{

    bsockunixbuf sunb (sockbuf::sock_stream);

    sunb.bind ("/tmp/socket+-");

    sunb.listen (2);

    bisockunix is ( sunb.accept () );

    char buf[32];

    is >> buf; cout << buf << endl;

    unlink ("/tmp/socket+-");

    return 0;

}


// tsunwrite.cpp

#include <sockunix.h>

int main ()

{

    bosockunix os (sockbuf::sock_stream);

    os->connect ("/tmp/socket++");

    os << "Hello!!!\n" << flush;

    return 0;

}