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

7.2: unix Datagram Sockets

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

tdunread.cpp


// reads data sent by tdunwrite.cpp

#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

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


// sends data to tdunread.cpp

#include <sockunix.h>



int main(int ac, char** av)

{

        if (ac < 2) {

                cerr << "USAGE: " << av[0]

                     << " socket_path_name data...\n";

                return 1;

        }



        bosockunix	su (sockbuf::sock_dgram);



	su->connect (av[1]);



        su << ac << ' ';

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

        su << endl;



        return 0;

}