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

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


// exchanges char strings with tsunwrite.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;

        }



        bsockunixbuf  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);



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


// exchanges char strings with tsunread.cpp

#include <sockunix.h>



int main(int ac, char** av)

{

        if (ac < 2) {

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

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

                return 1;

        }



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

}