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

4. sockinetbuf Class

sockinetbuf class is derived from sockbuf class and inherits most of the public functions of sockbuf. See section 2. sockbuf Class, for more information on sockbuf. In addition, it provides methods for getting sockinetaddr of local and peer connections. See section 5. sockinetaddr Class, for more information on sockinetaddr.

4.1 Methods  Describes sockinetbuf member functions.
4.2 inet Datagram Sockets  A pair of example programs demonstrating datagram connection in inet domain.
4.3 inet Stream Sockets  A pair of example programs demonstrating stream connection in inet domain.


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

4.1 Methods

In what follows,

sockinetbuf ins(ty, proto)
Constructs a sockinetbuf object ins whose socket communication type is ty and protocol is proto. proto defaults to 0.

sockinetbuf ins(si)
Constructs a sockinetbuf object ins which uses the same socket as si uses.

ins = si
performs the same function as sockbuf::operator=. See section 2. sockbuf Class, for more details.

ins.open(ty, proto)
create a new sockinetbuf whose type and protocol are ty and proto respectively and assign it to ins.

sockinetaddr sina = ins.localaddr()
returns the local inet address of the sockinetbuf object ins. The call will make sense only after a call to either sockbuf::bind or sockbuf::connect.

sockinetaddr sina = ins.peeraddr()
returns the peer inet address of the sockinetbuf object ins. The call will make sense only after a call to sockbuf::connect.

const char* hn = ins.localhost()
returns the local inet thostname of the sockinetbuf object ins. The call will make sense only after a call to either sockbuf::bind or sockbuf::connect.

const char* hn = ins.peerhost()
returns the peer inet thostname of the sockinetbuf object ins. The call will make sense only after a call to sockbuf::connect.

int pn = ins.localport()
returns the local inet port number of the sockinetbuf object ins in host byte order. The call will make sense only after a call to either sockbuf::bind or sockbuf::connect.

int pn = ins.peerport()
returns the peer inet port number of the sockinetbuf object ins in local host byte order. The call will make sense only after a call to sockbuf::connect.

ins.bind ()
binds ins to the default address INADDR_ANY and the default port. It returns 0 on success and returns the errno on failure.

ins.bind (adr, portno)
binds ins to the address adr and the port portno. It returns 0 on success and returns the errno on failure.

ins.bind (adr, serv, proto)
binds ins to the address, adr and the port corresponding to the service serv and the protocol proto>. It returns 0 on success and returns the errno on failure.

ins.bind (thostname, portno)
binds ins to the address corresponding to the hostname thostname and the port portno. It returns 0 on success and returns the errno on failure.

ins.bind (thostname, serv, proto)
binds ins to the address corresponding to the hostname thostname and the port corresponding to the service serv and the protocol proto>. It returns 0 on success and returns the errno on failure.

ins.connect (adr, portno)
connects ins to the address adr and the port portno. It returns 0 on success and returns the errno on failure.

ins.connect (adr, serv, proto)
connects ins to the address, adr and the port corresponding to the service serv and the protocol proto>. It returns 0 on success and returns the errno on failure.

ins.connect (thostname, portno)
connects ins to the address corresponding to the hostname thostname and the port portno. It returns 0 on success and returns the errno on failure.

ins.connect (thostname, serv, proto)
connects ins to the address corresponding to the hostname thostname and the port corresponding to the service serv and the protocol proto>. It returns 0 on success and returns the errno on failure.


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

4.2 inet Datagram Sockets

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

tdinread.cc

 
// reads data sent by tdinwrite.cc
#include <sockinet.h>

int main(int ac, char** av)
{
        isockinet  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.cc

 
// sends data to tdinread.cc
#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;
        }

        osockinet 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;        
}


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

4.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.cc

 
// receives strings from tsinwrite.cc and sends the strlen
// of each string back to tsinwrite.cc
#include        <sockinet.h>

int main()
{
        sockinetbuf     si(sockbuf::sock_stream);
        si.bind();

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

        iosockinet s (si.accept());
        char          buf[1024];

        while (s >> buf) {
                cout << buf << ' ';
                s << ::strlen(buf) << endl;
        }
        cout << endl;
        
        return 0;
}

tsinwrite.cc

 
// sends strings to tsinread.cc 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)
{
        iosockinet 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.cc
        si.shutdown(sockbuf::shut_write);

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

        return 0;
}


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

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