We discus only isockinet
class here. osockinet
and
iosockinet
are similar and are left out. However, they are
covered in the examples that follow.
isockinet
is used to handle interprocess communication in
inet domain. It is derived from isockstream
class and it
uses a sockinetbuf
as its stream buffer. See iosockstream, for
more details on isockstream
. See sockinetbuf Class, for
information on sockinetbuf
.
In what follows,
ty
is a sockbuf::type
and must be one of
sockbuf::sock_stream
, sockbuf::sock_dgram
,
sockbuf::sock_raw
, sockbuf::sock_rdm
, and
sockbuf::sock_seqpacket
proto
denotes the protocol number and is of type int
sb
is a sockbuf
object and must be in inet domain
sinp
is a pointer to an object of sockinetbuf
isockinet is (ty, proto)
isockinet
object is
whose sockinetbuf
buffer is of the type ty
and has the protocol number
proto
. The default protocol number is 0.
isockinet is (sinp)
isockinet
object is
whose sockinetbuf
is sinp
.
sinp = is.rdbuf ()
sockinetbuf
of isockinet
object
is
.
isockinet::operator ->
sockinetbuf
of sockinet
so that the sockinet
object acts as a smart pointer to sockinetbuf
.
is->localhost (); // same as is.rdbuf ()->localhost ();
The first pair of examples demonstrates datagram socket connections in the
inet domain. First, tdinread
prints its local host and
local port on stdout and waits for input in the connection.
tdinwrite
is started with the local host and local port of
tdinread
as arguments. It sends the string "How do ye do!" to
tdinread
which in turn reads the string and prints on its stdout.
// tdinread.cpp #include <sockinet.h> int main () { char buf[256]; bisockinet is (sockbuf::sock_dgram); is->bind (); cout << is->localhost() << ' ' << is->localport() << endl; is.getline (buf); cout << buf << endl; return 0; }
// tdinwrite.cpp--tdinwrite hostname portno #include <sockinet.h> #include <stdlib.h> int main (int ac, char** av) { bosockinet os (sockbuf::sock_dgram); os->connect (av[1], atoi(av[2])); os << "How do ye do!" << endl; return 0; }
The next example communicates with an nntp server through a
sockbuf::sock_stream
socket connection in inet domain.
After establishing a connection to the nntp server, it sends a "HELP"
command and gets back the HELP message before sending the "QUIT"
command.
// tnntp.cpp #include <sockinet.h> int main () { char buf[1024]; biosockinet io (sockbuf::sock_stream); io->connect ("murdoch.acc.virginia.edu", "nntp", "tcp"); io.getline (buf, 1024); cout << buf << endl; io << "HELP\r\n" << flush; io.getline (buf, 1024); cout << buf << endl; while (io.getline (buf, 1024)) if (buf[0] == '.' && buf[1] == '\r') break; else if (buf[0] == '.' && buf[1] == '.') cout << buf+1 << endl; else cout << buf << endl; io << "QUIT\r\n" << flush; io.getline (buf, 1024); cout << buf << endl; return 0; }