| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
sockstream classes are designed in such a way that they provide the same
interface as their stream counterparts do. We have isockstream
derived from istream and osockstream derived from
ostream. We also have iosockstream which is derived from
iostream.
Each domain also has its own set of stream classes. For example,
unix domain has isockunix, osockunix, and
iosockunix derived from isockstream, osockstream,
and iosockstream respectively. Similarly, inet domain has
isockinet, osockinet, and iosockinet.
| 8.1 iosockstreams | Generic IOStream classes for sockbuf buffers. | |
| 8.2 iosockinet Stream Classes | IOStream classes for inet domain of sockets. | |
| 8.3 iosockunix Classes | IOStream classes for unix domain of sockets. |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Since isockstream is publicly derived from istream, most
of the public functions of istream are also available in
isockstream.
isockstream redefines rdbuf() defined in its virtual base
class ios. Since, ios::rdbuf() is not virtual, care must
be taken to call the correct rdbuf() through a reference or a
pointer to an object of class isockstream.
In what follows,
sb is a sockbuf object
sbp is a pointer to a sockbuf object
isockstream is(sb)
isockstream object is with sb as its
sockbuf.
isockstream is(sbp)
isockstream object is with *sbp as its
sockbuf.
sbp = is.rdbuf()
sockbuf of the isockstream object
is.
isockstream::operator -> ()
isockstream's sockbuf so that
the user can use isockstream object as a sockbuf object.
is->connect (sa); // same as is.rdbuf()->connect (sa); |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Since osockstream is publicly derived from ostream, most
of the public functions of ostream are also available in
osockstream.
osockstream redefines rdbuf() defined in its virtual base
class ios. Since, ios::rdbuf() is not virtual, care must
be taken to call the correct rdbuf() through a reference or a
pointer to an object of class osockstream.
In what follows,
sb is a sockbuf object
sbp is a pointer to a sockbuf object
osockstream os(sb)
osockstream object os with sb as its
sockbuf.
osockstream os(sbp)
osockstream object os with *sbp as its
sockbuf.
sbp = os.rdbuf()
sockbuf of the osockstream object
os.
osockstream::operator -> ()
osockstream's sockbuf so that
the user can use osockstream object as a sockbuf object.
os->connect (sa); // same as os.rdbuf()->connect (sa); |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Since iosockstream is publicly derived from iostream, most
of the public functions of iostream are also available in
iosockstream.
iosockstream redefines rdbuf() defined in its virtual base
class ios. Since, ios::rdbuf() is not virtual, care must
be taken to call the correct rdbuf() through a reference or a
pointer to an object of class iosockstream.
In what follows,
sb is a sockbuf object
sbp is a pointer to a sockbuf object
iosockstream io(sb)
iosockstream object io with sb as its
sockbuf.
iosockstream io(sbp)
iosockstream object io with *sbp as its
sockbuf.
sbp = io.rdbuf()
sockbuf of the iosockstream object
io.
iosockstream::operator -> ()
iosockstream's sockbuf so that
the user can use iosockstream object as a sockbuf object.
io->connect (sa); // same as io.rdbuf()->connect (sa); |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
We discus only isockinet class here. osockinet and
iosockinet are similar and are left out. However, they are
covered in the examples that follow.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 section 8.1 iosockstreams, for
more details on isockstream. See section 4. 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 (sb)
isockinet object is whose sockinetbuf
is sb. sb must be in inet domain.
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 (); |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.cc
#include <sockinet.h>
int main ()
{
char buf[256];
isockinet is (sockbuf::sock_dgram);
is->bind ();
cout << is->localhost() << ' ' << is->localport() << endl;
is.getline (buf);
cout << buf << endl;
return 0;
}
|
// tdinwrite.cc--tdinwrite hostname portno
#include <sockinet.h>
#include <stdlib.h>
int main (int ac, char** av)
{
osockinet 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.cc
#include <sockinet.h>
int main ()
{
char buf[1024];
iosockinet 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;
}
|
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
We discuss only isockunix here. osockunix and
iosockunix are similar.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
isockunix is used to handle interprocess communication in
unix domain. It is derived from isockstream class and it
uses a sockunixbuf as its stream buffer. See section 8.1 iosockstreams, for
more details on isockstream. See section 6. sockunixbuf Class, for
information on sockunixbuf.
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 unix domain
sinp is a pointer to an object of sockunixbuf
isockunix is (ty, proto)
isockunix object is whose sockunixbuf
buffer is of the type ty and has the protocol number
proto. The default protocol number is 0.
isockunix is (sb)
isockunix object is whose sockunixbuf
is sb. sb must be in unix domain.
isockunix is (sinp)
isockunix object is whose sockunixbuf
is sinp.
sinp = is.rdbuf ()
sockunixbuf of isockunix object
is.
isockunix::operator ->
sockunixbuf of sockunix so that the sockunix
object acts as a smart pointer to sockunixbuf.
is->localhost (); // same as is.rdbuf ()->localhost (); |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
tsunread listens for connections. When tsunwrite requests
connection, tsunread accepts it and waits for input.
tsunwrite sends the string "Hello!!!" to tsunread.
tsunread reads the string sent by tsunwrite and prints on
its stdout.
// tsunread.cc
#include <sockunix.h>
#include <unistd.h>
int main ()
{
sockunixbuf sunb (sockbuf::sock_stream);
sunb.bind ("/tmp/socket+-");
sunb.listen (2);
isockunix is (sunb.accept ());
char buf[32];
is >> buf; cout << buf << endl;
unlink ("/tmp/socket+-");
return 0;
}
|
// tsunwrite.cc
#include <sockunix.h>
int main ()
{
osockunix os (sockbuf::sock_stream);
os->connect ("/tmp/socket++");
os << "Hello!!!\n" << flush;
return 0;
}
|
| [ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |