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

5.2: inet Datagram Sockets

The following two programs illustrates how to use sockinetbuf class for datagram connection in inet domain. tdinread.cpp also shows how to use isockinet class and tdinwrite.cpp shows how to use osockinet class.

tdinread.cpp


// reads data sent by tdinwrite.cpp

#include <sockinet.h>



int main(int ac, char** av)

{

        bisockinet  is (sockbuf::sock_dgram);

        is->bind();



        cout << "localhost = " << so.localhost() << endl

             << "localport = " << so.localport() << endl;



        char         buf[256];

        int          n;



        is >> n;

        cout << av[0] << ": ";

        while(n--) {

                is >> buf;

                cout << buf << ' ';

        }

        cout << endl;



        return 0;

}

tdinwrite.cpp


// sends data to tdinread.cpp

#include <sockinetbuf.h>

#include <stdlib.h>



int main(int ac, char** av)

{

        if (ac < 3) {

                cerr << "USAGE: " << av[0] << " thostname port-number "

                     << "data ... " << endl;

                return 1;

        }



        bosockinet os (sockbuf::sock_dgram);

        os->connect (av[1], atoi(av[2]));



        cout << "local: " << so.localport() << ' '

                          << so.localhost() << endl

             << "peer:  " << so.peerport() << ' '

                          << so.peerhost() << endl;



        os << ac-3; av += 3;

        while(*av) os << *av++ << ' ';

        os << endl;



        return 0;

}