| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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] | [ ? ] |
In what follows,
ty denotes the type of the socket connection and is of type
sockbuf::type
proto denotes the protocol and is of type int
si, ins are sockbuf objects and are in inet domain
adr denotes an inet address in host byte order and is of
type unsigned long
serv denotes a service like "nntp" and is of type char*
proto denotes a protocol like "tcp" and is of type char*
thostname is of type char* and denotes the name of a host like
"kelvin.acc.virginia.edu" or "128.143.24.31".
portno denotes a port in host byte order and is of type int
sockinetbuf ins(ty, proto)
sockinetbuf object ins whose socket
communication type is ty and protocol is proto.
proto defaults to 0.
sockinetbuf ins(si)
sockinetbuf object ins which uses the same
socket as si uses.
ins = si
sockbuf::operator=.
See section 2. sockbuf Class, for more details.
ins.open(ty, proto)
sockinetbuf whose type and protocol are
ty and proto respectively and assign it to ins.
sockinetaddr sina = ins.localaddr()
sockinetbuf object
ins. The call will make sense only after a call to either
sockbuf::bind or sockbuf::connect.
sockinetaddr sina = ins.peeraddr()
sockinetbuf object
ins. The call will make sense only after a call to
sockbuf::connect.
const char* hn = ins.localhost()
sockinetbuf object
ins. The call will make sense only after a call to either
sockbuf::bind or sockbuf::connect.
const char* hn = ins.peerhost()
sockinetbuf object
ins. The call will make sense only after a call to
sockbuf::connect.
int pn = ins.localport()
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()
sockinetbuf object
ins in local host byte order. The call will make sense only after a
call to sockbuf::connect.
ins.bind ()
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)
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)
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)
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)
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)
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)
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)
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)
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] | [ ? ] |
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.
// 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;
}
|
// 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] | [ ? ] |
The following two programs illustrates the use of sockinetbuf class
for stream connection in inet domain. It also shows how to use
iosockinet class.
// 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;
}
|
// 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] | [ ? ] |