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

5.3: inet Stream Sockets

The following two programs illustrates the use of sockinetbuf class for stream connection in inet domain. It also shows how to use iosockinet class.

tsinread.cpp


// receives strings from tsinwrite.cpp and sends the strlen

// of each string back to tsinwrite.cpp

#include        <sockinet.h>



int main()

{

        bsockinetbuf     si(sockbuf::sock_stream);

        si.bind();



        cout << si.localhost() << ' ' << si.localport() << endl;

        si.listen();



        biosockinet s ( si.accept() );

        char          buf[1024];



        while (s >> buf) {

                cout << buf << ' ';

                s << ::strlen(buf) << endl;

        }

        cout << endl;



        return 0;

}

tsinwrite.cpp


// sends strings to tsinread.cpp and gets back their length

// usage: tsinwrite hostname portno

//        see the output of tsinread for what hostname and portno to use



#include        <sockinet.h>

#include        <stdlib.h>



int main(int ac, char** av)

{

        biosockinet sio (sockbuf::sock_stream);

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



        sio << "Hello! This is a test\n" << flush;



        // terminate the while loop in tsinread.cpp

        si.shutdown(sockbuf::shut_write);



        int len;

        while (s >> len) cout << len << ' ';

        cout << endl;



        return 0;

}