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

6. sockunixbuf Class

sockunixbuf class is derived from sockbuf class declared in <sockstream.h> and hence, inherits most of the public member functions of sockbuf. See section 2. sockbuf Class, for more information on sockbuf.

6.1 Methods  Describes sockunixbuf member functions
6.2 unix Datagram Sockets  A pair of example programs demonstrating datagram connection in unix domain
6.3 unix Stream Sockets  A pair of example programs demonstrating stream connection in unix domain


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

6.1 Methods

In what follows,

sockunixbuf uns(ty, proto)
Constructs a sockunixbuf object uns with ty as its type and proto as its protocol number. proto defaults to 0.

sockunixbuf uns = su
Constructs a sockunixbuf object uns which uses the same socket as is used by su.

uns = su
sockunixbuf object uns closes its current socket if no other sockbuf is referring to it and uses the socket that sockbuf object su is using.

uns.open(ty, proto)
create a sockunixbuf object with ty as its type and proto as its protocol and assign the sockunixbuf object so created to *this. It returns this. proto defaults to 0.

uns.bind(path)
binds uns to the unix pathname path. It returns 0 on success and returns the errno on failure.

uns.connect(path)
connects uns to the unix pathname path. It returns 0 on success and returns the errno on failure.


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

6.2 unix Datagram Sockets

The following two programs illustrates how to use sockunixbuf class for datagram connection in unix domain. tdunread.cc also shows how to use isockunix class and tdunwrite.cc shows how to use osockunix class.

tdunread.cc

 
// reads data sent by tdunwrite.cc
#include <sockunix.h>
#include <unistd.h>
#include <errno.h>

int main(int ac, char** av)
{
        if (ac != 2) {
                cerr << "USAGE: " << av[0] << " socket_path_name\n";
                return 1;
        }
        
        // isockunix builds the sockunixbuf object
        isockunix 	su (sockbuf::sock_dgram);
    
        su->bind(av[1]);

        cout << "Socket name = " << av[1] << endl;

        if (chmod(av[1], 0777) == -1) {
                perror("chmod");
                return 1;
        }

        char buf[1024];
        int i;
        su >> i;
        cout << av[0] << ": " << i << " strings: ";
        while (i--) {
                su >> buf;
                cout  << buf << ' ';
        }
        cout << endl;
        
        unlink(av[1]);
        return 0;
}

tdunwrite.cc

 
// sends data to tdunread.cc
#include <sockunix.h>

int main(int ac, char** av)
{
        if (ac < 2) {
                cerr << "USAGE: " << av[0]
                     << " socket_path_name data...\n";
                return 1;
        }

        osockunix	su (sockbuf::sock_dgram);

	su->connect (av[1]);

        su << ac << ' ';
        while (*av) { su << av[i] << ' '; av++; }
        su << endl;

        return 0;
}


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

6.3 unix Stream Sockets

The following two programs illustrates how to use sockunixbuf class for stream connection in unix domain. It also shows how to use iosockunix class.

tsunread.cc

 
// exchanges char strings with tsunwrite.cc
#include <sockunix.h>
#include <unistd.h>
#include <errno.h>

int main(int ac, char** av)
{
        if (ac != 2) {
                cerr << "USAGE: " << av[0] << " socket_path_name\n";
                return 1;
        }
        
        sockunixbuf  su(sockbuf::sock_stream);
        su.bind(av [1]);

        cout << "Socket name = " << av[1] << endl;

        if (chmod(av[1], 0777) == -1) {
                perror("chmod");
                return 1;
        }

        su.listen(3);
        
        iosockunix ioput (su.accept ());
        char       buf[1024];
        
        ioput << av[0] << ' ' << av[1] << endl;
        while ( ioput >> buf ) cout << av[0] << ": " << buf << endl;
        unlink(av[1]);
        return 0;
}

tsunwrite.cc

 
// exchanges char strings with tsunread.cc
#include <sockunix.h>

int main(int ac, char** av)
{
        if (ac < 2) {
                cerr << "USAGE: " << av[0]
                     << " socket_path_name data...\n";
                return 1;
        }

        iosockunix oput (sockbuf::sock_stream);
        oput->connect (av [1]);

        char buf[128];

        oput >> buf;
        cout << buf << ' ';
        oput >> buf;
        cout << buf << endl;

        while (*av) oput << *av++ << ' ';
        oput << endl;
        
        return 0;
}


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

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