ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/c++/socket++-1.10/socket++.texi
Revision: 1.2
Committed: Wed Jun 13 19:37:29 2001 UTC (23 years, 5 months ago) by tdb
Content type: application/x-texinfo
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +0 -0 lines
State: FILE REMOVED
Log Message:
Basically done a make distclean (or make real-clean in this case). No idea
why we needed all this "generated" stuff lying about.
Left libsocket++.a though, until the main Makefile will use it.

File Contents

# Content
1 \input texinfo @c -*-texinfo-*-
2 @c %**start of header
3 @setfilename socket++.info
4 @settitle C++ socket classes
5 @c %**end of header
6 @syncodeindex fn cp
7
8 @iftex
9 @finalout
10 @end iftex
11
12 @ifinfo
13 This info file describes the C++ family of socket classes.
14
15 Copyright (C) 1992,1993,1994 Gnanasekaran Swaminathan <gs4t@@virginia.edu>
16
17 Permission is granted to make and distribute verbatim copies of this
18 document provided the copyright notice and this permission notice
19 are preserved on all copies.
20 @end ifinfo
21
22 @titlepage
23 @title C++ Socket Classes
24 @subtitle Version: 17Oct95 1.10
25 @author Gnanasekaran Swaminathan
26
27 @page
28 @vskip 0pt plus 1filll
29 Copyright @copyright{} 1992,1993,1994 Gnanasekaran Swaminathan
30
31 @sp 2
32 This is Version: 17Oct95 1.10 of the C++ family of socket classes.
33 @sp 2
34
35 Permission is granted to make and distribute verbatim copies of this
36 document provided the copyright notice and this permission notice
37 are preserved on all copies.
38 @end titlepage
39
40 @ifinfo
41 @node Top, Copying, (dir), (dir)
42 @top Socket++ Library
43
44 Socket++ is a family of C++ classes that gives the same
45 interface as the iostream classes for input and output
46 for communication between processes.@refill
47
48 This documentation describes Version: 17Oct95 1.10 of
49 socket++ library.@refill
50 @end ifinfo
51
52 @menu
53 * Copying:: Copyright information.
54 * Acknowledgments:: Thanks!
55 * Overview of Socket++:: Overview of socket++ library.
56 * sockbuf Class:: Socket streambuf class.
57 * sockAddr Class:: Base class for socket addresses.
58
59 * sockinetbuf Class:: Socket class for INET address family.
60 * sockinetaddr Class:: Address class for INET address family of
61 sockets.
62
63 * sockunixbuf Class:: Socket class for UNIX address family.
64 * sockunixaddr Class:: Address class for UNIX address family of
65 sockets.
66
67 * sockstream Classes:: I/O socket stream classes and some examples.
68 * pipestream Classes:: I/O stream classes that provides pipe,
69 socketpair, and popen facilities.
70
71 * Fork Class:: Use the Fork class to fork a child
72 process.
73
74 * protocol Class:: Protocol base class.
75 * echo Class:: Class implementing the Echo protocol.
76 * smtp Class:: Class implementing the SMTP protocol.
77
78 * Error Handling:: Describes the default error handling in
79 the socket++ library.
80 * Pitfalls:: Common mistakes that socket++ library
81 users make.
82
83 * Index:: Index to concepts and program names
84 @end menu
85
86 @node Copying
87 @unnumbered Socket++ Library Copyright Notice
88 @cindex copyright notice
89 @cindex Copyright
90
91 Copyright (C) 1992,1993,1994 Gnanasekaran Swaminathan
92
93 Permission is granted to use at your own risk and distribute this
94 software in source and binary forms provided the above copyright
95 notice and this paragraph are preserved on all copies. This software
96 is provided "as is" with no express or implied warranty.@refill
97
98 @node Acknowledgments
99 @unnumbered Acknowledgments
100 @cindex acknowledgments
101
102 Gordon Joly <G.Joly@@cs.ucl.ac.uk> for reporting bugs in
103 pipestream class implementation and providing an ftp site
104 for the socket++ library at
105 cs.ucl.ac.uk:~ftp/coside/gnu/socket++-1.x.tar.gz
106 He also knows how to make the socket++ library a shared
107 library.
108
109 Jim Anderson for reporting a bug in sockinet.C
110
111 Carl Gay <cgay@@skinner.cs.uoregon.edu> for reporting a bug
112 and a fix in sockinet.C
113
114 Oliver Imbusch <flabes@@parystec.de> for reporting a bug
115 in Makefile.in and suggesting several enhancements for sockbuf class.
116
117 Dierk Wendt <wendt@@lambda.hella.de> for reporting errors
118 in the socket++ documentation.
119
120 Per Bothner <bothner@@cygnus.com> for configure, config.sub,
121 config.shared and move-if-change files that are used
122 to generate Makefile. These files are taken from his libg++-2.4
123 and hence, these files are governed by the Copyright Notice found
124 in the file LICENCE in libg++.
125
126 @node Overview of Socket++
127 @chapter Overview of Socket++ Library
128 @cindex overview of socket++
129
130 Socket++ library defines a family of C++ classes that can be used
131 more effectively than directly calling the underlying low-level
132 system functions. One distinct advantage of the socket++ is that
133 it has the same interface as that of the iostream so that
134 the users can perform type-safe input output. See your local
135 IOStream library documentation for more information on iostreams.@refill
136
137 @code{streambuf} counterpart of the socket++ is @code{sockbuf}.
138 @code{sockbuf} is an endpoint for communication with yet another
139 @code{sockbuf} or simply a @code{socket} descriptor. @code{sockbuf}
140 has also methods that act as interfaces for most of the commonly
141 used system calls that involve sockets. @xref{sockbuf Class}, for more
142 information on the socket buffer class.
143
144 For each communication domain, we derive a new class from @code{sockbuf}
145 that has some additional methods that are specific to that domain. At
146 present, only @var{unix} and @var{inet} domains are supported.
147 @code{sockunixbuf} class and @code{sockinetbuf} class define the @var{unix}
148 and @var{inet} domain of sockets respectively. @xref{sockunixbuf Class}, for
149 @var{unix} sockets and @xref{sockinetbuf Class}, for @var{inet} sockets.
150
151 We also have domain specific socket address classes that are
152 derived from a common base class called @code{sockAddr}.
153 @code{sockunixaddr} class is used for @var{unix} domain addresses
154 and @code{sockinetaddr} class is used for @var{inet} domain
155 addresses. For more information on address classes see @ref{sockAddr Class},
156 @ref{sockunixaddr Class}, and @ref{sockinetaddr Class}.
157 @quotation
158 @emph{Note}: @code{sockAddr} is not spelled @code{sockaddr} in
159 order to prevent name clash with the @code{struct sockaddr} declared
160 in @file{<sys/socket.h>}.
161 @end quotation
162
163 We noted earlier that socket++ provides the same interface as the
164 iostream library. For example, in the internet domain, we have
165 @code{isockinet}, @code{osockinet}, and @code{iosockinet} classes
166 that are counterparts to @code{istream}, @code{ostream}, and
167 @code{iostream} classes of IOStream library.
168 For more details on @code{iosockstream} classes see @xref{sockstream Classes}.
169
170 The services of @code{pipe()}, @code{socketpair()}, and @code{popen()}
171 system calls are provided by the @code{pipestream} class.
172 @xref{pipestream Classes}.
173
174 @node sockbuf Class
175 @chapter @code{sockbuf} Class
176 @cindex sockbuf class
177 @cindex class sockbuf
178
179 @code{sockbuf} class is derived from @code{streambuf} class of the
180 iostream library. You can simultaneously read and write into a
181 @code{sockbuf} just like you can listen and talk through a telephone. To
182 accomplish the above goal, we maintain two independent buffers for
183 reading and writing.
184
185 @menu
186 * Constructors:: How to construct a @code{sockbuf} object
187 and how to open a socket?
188 * Destructor:: How to destruct a @code{sockbuf} object
189 and how to close a socket?
190 * Reading and Writing:: How to use @code{sockbuf} as @code{streambuf}?
191 * Connection Establishment:: How to bind an address and establish a
192 connection?
193 * Socket Options:: How to set and get socket options?
194 * Timeouts:: How to gracefully handle connection inactivity?
195 @end menu
196
197 @node Constructors
198 @section Constructors
199 @cindex sockbuf constructors
200 @findex sockbuf::type
201
202 @code{sockbuf} constructors sets up an endpoint for communication. A
203 @code{sockbuf} object so created can be read from and written to in
204 linebuffered mode. To change mode, refer to @code{streambuf} class
205 in your IOStream library.
206 @quotation
207 @cindex flushing buffers
208 @emph{Note}: If you are using AT&T IOStream library, then the
209 linebuffered mode is permanently turned off. Thus, you need to
210 explicitly flush a socket stream. You can flush a socket stream buffer
211 in one of the following four ways:
212 @example
213 // os is a socket ostream
214 os << "this is a test" << endl;
215 os << "this is a test\n" << flush;
216 os << "this is a test\n"; os.flush ();
217 os << "this is a test\n"; os->sync ();
218 @end example
219 @end quotation
220
221 @code{sockbuf} objects are created as follows where
222 @itemize @minus
223 @item
224 @code{s} and @code{so} are @code{sockbuf} objects
225 @item
226 @code{sd} is an integer which is a socket descriptor
227 @item
228 @code{af} and @code{proto} are integers which denote domain number and
229 protocol number respectively
230 @item
231 @code{ty} is a @code{sockbuf::type} and must be one of
232 @code{sockbuf::sock_stream}, @code{sockbuf::sock_dgram},
233 @code{sockbuf::sock_raw}, @code{sockbuf::sock_rdm}, and
234 @code{sockbuf::sock_seqpacket}
235 @end itemize
236
237 @table @code
238
239 @item sockbuf s(sd);
240 @itemx sockbuf s;
241 @findex sockbuf::sockbuf
242 Set socket descriptor of @code{s} to @code{sd} (defaults to -1).
243 @code{sockbuf} destructor will close @code{sd}.
244
245 @item sockbuf s(af, ty, proto);
246 Set socket descriptor of @code{s} to @code{::socket(af, int(ty), proto);}
247
248 @item sockbuf so(s);
249 Set socket descriptor of @code{so} to the socket descriptor of @code{s}.
250
251 @item s.open(ty, proto)
252 @findex sockbuf::open
253 does nothing and returns simply @code{0}, the null pointer
254 to @code{sockbuf}.
255
256 @item s.is_open()
257 @findex sockbuf::is_open
258 returns a non-zero number if the socket descriptor is open else return
259 0.
260
261 @item s = so;
262 @findex sockbuf::operator=
263 return a reference @code{s} after assigning @code{s} with @code{so}.
264
265 @end table
266
267 @node Destructor
268 @section Destructor
269 @cindex sockbuf destructor
270 @findex sockbuf::shuthow
271
272 @code{sockbuf::~sockbuf()} flushes output and closes its socket if no other
273 sockbuf is referencing it and _S_DELETE_DONT_CLOSE flag is not set. It
274 also deletes its read and write buffers.
275
276 In what follows,
277 @itemize @minus
278 @item
279 @code{s} is a @code{sockbuf} object
280 @item
281 @code{how} is of type @code{sockbuf::shuthow} and must be one of
282 @code{sockbuf::shut_read}, @code{sockbuf::shut_write}, and
283 @code{sockbuf::shut_readwrite}
284 @end itemize
285
286 @table @code
287 @item sockbuf::~sockbuf()
288 @findex sockbuf::~sockbuf
289 flushes output and closes its socket if no other
290 @code{sockbuf} object is referencing it before deleting its read and
291 write buffers. If the _S_DELETE_DONT_CLOSE flag is set, then the socket
292 is not closed.
293
294 @item s.close()
295 @findex sockbuf::close
296 closes the socket even if it is referenced by other @code{sockbuf}
297 objects and _S_DELETE_DONT_CLOSE flag is set.
298
299 @item s.shutdown(how)
300 @findex sockbuf::shutdown
301 shuts down read if @code{how} is @code{sockbuf::shut_read}, shuts down
302 write if @code{how} is @code{sockbuf::shut_write}, and shuts down both
303 read and write if @code{how} is @code{sockbuf::shut_readwrite}.
304
305 @end table
306
307 @node Reading and Writing
308 @section Reading and Writing
309 @cindex sockbuf reading
310 @cindex sockbuf writing
311
312 @code{sockbuf} class offers several ways to read and write and tailors
313 the behavior of several virtual functions of @code{streambuf} for socket
314 communication.
315
316 In case of error, @code{sockbuf::error(const char*)} is called.
317
318 In what follows,
319 @itemize @minus
320 @item
321 @code{s} is a @code{sockbuf} object
322 @item
323 @code{buf} is buffer of type @code{char*}
324 @item
325 @code{bufsz} is an integer and is less than @code{sizeof(buf)}
326 @item
327 @code{msgf} is an integer and denotes the message flag
328 @item
329 @code{sa} is of type @code{sockAddr}
330 @item
331 @code{msgh} is a pointer to @code{struct msghdr}
332 @item
333 @code{wp} is an integer and denotes time in seconds
334 @item
335 @code{c} is a char
336 @end itemize
337
338 @table @code
339
340 @item s.is_open()
341 @findex sockbuf::is_open
342 returns a non-zero number if the socket descriptor is open else return
343 0.
344
345 @item s.is_eof()
346 @findex sockbuf::is_eof
347 returns a non-zero number if the socket has seen EOF while reading else
348 return 0.
349
350 @item s.write(buf, bufsz)
351 @findex sockbuf::write
352 returns an int which must be equal to @code{bufsz} if @code{bufsz} chars in
353 the @code{buf} are written successfully. It returns 0 if there is
354 nothing to write or if, in case of timeouts, the socket is not ready
355 for write @ref{Timeouts}.
356
357 @item s.send(buf, bufsz, msgf)
358 @findex sockbuf::send
359 @findex sockbuf::msgflag
360 same as @code{sockbuf::write} described above but allows the user to
361 control the transmission of messages using the message flag @code{msgf}.
362 If @code{msgf} is @code{sockbuf::msg_oob} and the socket type of
363 @code{s} is @code{sockbuf::sock_stream}, @code{s} sends the message in
364 @var{out-of-band} mode. If @code{msgf} is @code{sockbuf::msg_dontroute},
365 @code{s} sends the outgoing packets without routing. If @code{msgf} is
366 0, which is the default case, @code{sockbuf::send} behaves exactly like
367 @code{sockbuf::write}.
368
369 @item s.sendto(sa, buf, bufsz, msgf)
370 @findex sockbuf::sendto
371 same as @code{sockbuf::send} but works on unconnected sockets. @code{sa}
372 specifies the @var{to} address for the message.
373
374 @item s.sendmsg(msgh, msgf)
375 @findex sockbuf::sendmsg
376 same as @code{sockbuf::send} but sends a @code{struct msghdr} object
377 instead.
378
379 @item s.sys_write(buf, bufsz)
380 @findex sockbuf::sys_write
381 calls @code{sockbuf::write} and returns the result. Unlike
382 @code{sockbuf::write} @code{sockbuf::sys_write} is declared as a virtual
383 function.
384
385 @item s.read(buf, bufsz)
386 @findex sockbuf::read
387 returns an int which is the number of chars read into the @code{buf}. In
388 case of EOF, return EOF. Here, @code{bufsz} indicates the size of the
389 @code{buf}. In case of timeouts, return 0 @ref{Timeouts}.
390
391 @item s.recv(buf, bufsz, msgf)
392 @findex sockbuf::recv
393 @findex sockbuf::msgflag
394 same as @code{sockbuf::read} described above but allows the user to receive
395 @var{out-of-band} data if @code{msgf} is @code{sockbuf::msg_oob} or to
396 preview the data waiting to be read if @code{msgf} is
397 @code{sockbuf::msg_peek}. If @code{msgf} is 0, which is the default
398 case, @code{sockbuf::recv} behaves exactly like @code{sockbuf::read}.
399
400 @item s.recvfrom(sa, buf, bufsz, msgf)
401 @findex sockbuf::recvfrom
402 same as @code{sockbuf::recv} but works on unconnected sockets. @code{sa}
403 specifies the @var{from} address for the message.
404
405 @item s.recvmsg(msgh, msgf)
406 @findex sockbuf::recvmsg
407 same as @code{sockbuf::recv} but reads a @code{struct msghdr} object
408 instead.
409
410 @item s.sys_read(buf, bufsz)
411 @findex sockbuf::sys_read
412 calls @code{sockbuf::read} and returns the result. Unlike
413 @code{sockbuf::read} @code{sockbuf::sys_read} is declared as a virtual
414 function.
415
416 @item s.is_readready(wp_sec, wp_usec)
417 @findex sockbuf::is_readready
418 returns a non-zero int if @code{s} has data waiting to be read from the
419 communication channel. If @code{wp_sec >= 0}, it waits for
420 @code{wp_sec 10^6 + wp_usec} microseconds before returning 0 in case
421 there are no data waiting to be read. If @code{wp_sec < 0}, then it waits until
422 a datum arrives at the communication channel. @code{wp_usec} defaults to 0.
423 @quotation
424 @emph{Please Note}: The data waiting in @code{sockbuf}'s own buffer is
425 different from the data waiting in the communication channel.
426 @end quotation
427
428 @item s.is_writeready(wp_sec, wp_usec)
429 @findex sockbuf::is_writeready
430 returns a non-zero int if data can be written onto the communication
431 channel of @code{s}. If @code{wp_sec >= 0}, it waits for
432 @code{wp_sec 10^6 + wp_usec} microseconds before returning 0 in case no
433 data can be written. If @code{wp_sec < 0}, then it waits until
434 the communication channel is ready to accept data. @code{wp_usec}
435 defaults to 0.
436 @quotation
437 @emph{Please Note}: The buffer of the @code{sockbuf} class is different
438 from the buffer of the communication channel buffer.
439 @end quotation
440
441 @item s.is_exceptionpending(wp_sec, wp_usec)
442 @findex sockbuf::is_exceptionpending
443 returns non-zero int if @code{s} has any exception events pending.
444 If @code{wp_sec >= 0}, it waits for @code{wp_sec 10^6 + wp_usec}
445 microseconds before returning 0 in case @code{s} does
446 not have any exception events pending. If @code{wp_sec < 0},
447 then it waits until an expception event occurs. @code{wp_usec} defaults
448 to 0.
449 @quotation
450 @emph{Please Note}: The exceptions that
451 @code{sockbuf::is_exceptionpending} is looking for are different from
452 the C++ exceptions.
453 @end quotation
454
455 @item s.flush_output()
456 @findex sockbuf::flush_output
457 flushes the output buffer and returns the number of chars flushed.
458 In case of error, return EOF. @code{sockbuf::flush_output} is a
459 protected member function and it is not available for general public.
460
461 @item s.doallocate()
462 @findex sockbuf::doallocate
463 allocates free store for read and write buffers of @code{s} and returns
464 1 if allocation is done and returns 0 if there is no need.
465 @code{sockbuf::doallocate} is a protected virtual member function and it
466 is not available for general public.
467
468 @item s.underflow()
469 @findex sockbuf::underflow
470 returns the unread char in the buffer as an unsigned char if there is
471 any. Else returns EOF if @code{s} cannot allocate space for the buffers,
472 cannot read or peer is closed. @code{sockbuf::underflow} is a protected
473 virtual member function and it is not available for general public.
474
475 @item s.overflow(c)
476 @findex sockbuf::overflow
477 if @code{c==EOF}, call and return the result of @code{flush_output()},
478 else if @code{c=='\n'} and @code{s} is linebuffered, call
479 @code{flush_output()} and return @code{c} unless @code{flush_output()}
480 returns EOF, in which case return EOF. In any other case, insert char
481 @code{c} into the buffer and return @code{c} as an unsigned char.
482 @code{sockbuf::overflow} is a protected member virtual function and it is not
483 available for general public.
484 @quotation
485 @emph{Node:} linebuffered mode does not work with AT&T IOStream library.
486 Use explicit flushing to flush @code{sockbuf}.
487 @end quotation
488
489 @item s.sync()
490 @findex sockbuf::sync
491 @cindex flushing output
492 calls @code{flush_output()} and returns the result. Useful if the user needs
493 to flush the output without writing newline char into the write buffer.
494
495 @item s.xsputn(buf, bufsz)
496 @findex sockbuf::xsputn
497 write @code{bufsz} chars into the buffer and returns the number of chars
498 successfully written. Output is flushed if any char in
499 @code{buf[0..bufsz-1]} is @code{'\n'}.
500
501 @item s.recvtimeout(wp)
502 @findex sockbuf::recvtimeout
503 sets the recv timeout to @code{wp} seconds. If @code{wp} is -1, it is a
504 block and if @code{wp} is 0, it is a poll.
505
506 It affects all read functions. If the socket is not read ready within
507 @code{wp} seconds, the read call will return 0. It also affects
508 @code{sockbuf::underflow}. @code{sockbuf::underflow} will not set the
509 @code{_S_EOF_SEEN} flag if it is returning EOF because of timeout.
510
511 @code{sockbuf::recvtimeout} returns the old recv timeout value.
512
513 @item s.sendtimeout(wp)
514 @findex sockbuf::sendtimeout
515 sets the send timeout to @code{wp} seconds. If @code{wp} is -1, it is a
516 block and if @code{wp} is 0, it is a poll.
517
518 It affects all write functions. If the socket is not write ready within
519 @code{wp} seconds, the write call will return 0.
520
521 @code{sockbuf::sendtimeout} returns the old send timeout value.
522
523 @end table
524
525 @node Connection Establishment
526 @section Establishing connections
527 @cindex connection establishment
528 @cindex names
529
530 A name must be bound to a @code{sockbuf} if processes want to refer to
531 it and use it for communication. Names must be unique. A @var{unix} name
532 is a 3-tuple, @var{<protocol, local path, peer path>}. An @var{inet} name
533 is a 5-tuple, @var{<protocol, local addr, local port, peer addr, peer
534 port>}. @code{sockbuf::bind} is used to specify the local half of the
535 name---@var{<local path>} for @var{unix} and @var{<local addr, local
536 port>} for @var{inet}. @code{sockbuf::connect} and
537 @code{sockbuf::accept} are used to specify the peer half of the
538 name---@var{<peer path>} for @var{unix} and @var{<peer addr, peer port>}
539 for @var{inet}.
540
541 In what follows,
542 @itemize @minus
543 @item
544 @code{s} and @code{so} are @code{sockbuf} objects
545 @item
546 @code{sa} is a @code{sockAddr} object
547 @item
548 @code{nc} is an integer denoting the number of connections to allow
549 @end itemize
550
551 @table @code
552
553 @item s.bind(sa)
554 @findex sockbuf::bind
555 @cindex binding addresses
556 binds @code{sockAddr} @code{sa} as the local half of the name for
557 @code{s}. It returns 0 on success and returns the errno on failure.
558
559 @item s.connect(sa)
560 @findex sockbuf::connect
561 @cindex connect
562 @code{sockbuf::connect} uses @code{sa} to provide the peer half of the
563 name for @code{s} and to establish the connection itself.
564 @code{sockbuf::connect} also provides the local half of the name
565 automatically and hence, the user should not use @code{sockbuf::bind} to
566 bind any local half of the name. It returns 0 on success and returns
567 the errno on failure.
568
569
570 @item s.listen(nc)
571 @findex sockbuf::listen
572 @cindex listening
573 makes @code{s} ready to accept connections. @code{nc} specifies the
574 maximum number of outstanding connections that may be queued and must be
575 at least 1 and less than or equal to @code{sockbuf::somaxconn} which
576 is usually 5 on most systems.
577
578 @item sockbuf so = s.accept(sa)
579 @itemx sockbuf so = s.accept()
580 @findex sockbuf::accept
581 @cindex accepting connections
582 accepts connections and returns the peer address in @code{sa}. @code{s}
583 must be a listening @code{sockbuf}. See @code{sockbuf::listen} above.
584
585 @end table
586
587 @node Socket Options
588 @section Getting and Setting Socket Options
589 @cindex socket options
590 @cindex option setting
591 @cindex option getting
592
593 Socket options are used to control a socket communication. New options
594 can be set and old value of the options can be retrived at the protocol
595 level or at the socket level by using @code{setopt} and
596 @code{getopt} member functions. In addition, you can also use special
597 member functions to get and set specific options.
598
599 In what follows,
600 @itemize @minus
601 @item
602 @code{s} is a @code{sockbuf} object
603 @item
604 @code{opval} is an integer and denotes the option value
605 @item
606 @code{op} is of type @code{sockbuf::option} and must be one of
607 @itemize @bullet
608 @item
609 @code{sockbuf::so_error} used to retrieve and clear error status
610 @item
611 @code{sockbuf::so_type} used to retrieve type of the socket
612 @item
613 @code{sockbuf::so_debug} is used to specify recording of debugging
614 information
615 @item
616 @code{sockbuf::so_reuseaddr} is used to specify the reuse of local address
617 @item
618 @code{sockbuf::so_keepalive} is used to specify whether to keep connections
619 alive or not
620 @item
621 @code{sockbuf::so_dontroute} is used to specify whether to route messages
622 or not
623 @item
624 @code{sockbuf::so_broadcast} is used to specify whether to broadcast
625 @code{sockbuf::sock_dgram} messages or not
626 @item
627 @code{sockbuf::so_oobinline} is used to specify whether to inline
628 @var{out-of-band} data or not.
629 @item
630 @code{sockbuf::so_linger} is used to specify for how long to linger before
631 shutting down
632 @item
633 @code{sockbuf::so_sndbuf} is used to retrieve and to set the size of the
634 send buffer (communication channel buffer not @code{sockbuf}'s internal
635 buffer)
636 @item
637 @code{sockbuf::so_rcvbuf} is used to retrieve and to set the size of the
638 recv buffer (communication channel buffer not @code{sockbuf}'s internal
639 buffer)
640 @end itemize
641 @end itemize
642
643 @table @code
644
645 @item s.getopt(op, &opval, sizeof(opval), oplevel)
646 @findex sockbuf::getopt
647 @cindex getsockopt
648 gets the option value of the @code{sockbuf::option} @code{op} at the
649 option level @code{oplevel} in @code{opval}. It returns the actual size
650 of the buffer @code{opval} used. The default value of the @code{oplevel}
651 is @code{sockbuf::sol_socket}.
652
653 @item s.setopt(op, &opval, sizeof(opval), oplevel)
654 @findex sockbuf::setopt
655 @cindex setsockopt
656 sets the option value of the @code{sockbuf::option} @code{op} at the
657 option level @code{oplevel} to @code{opval}. The default value of the
658 @code{oplevel} is @code{sockbuf::sol_socket}.
659
660 @item s.gettype()
661 @findex sockbuf::gettype
662 gets the socket type of @code{s}. The return type is
663 @code{sockbuf::type}.
664
665 @item s.clearerror()
666 @findex sockbuf::clearerror
667 gets and clears the error status of the socket.
668
669 @item s.debug(opval)
670 @findex sockbuf::debug
671 if @code{opval} is not -1, set the @code{sockbuf::so_debug} option value
672 to @code{opval}. In any case, return the old option value of
673 @code{sockbuf::so_debug} option. The default value of @code{opval} is
674 -1.
675
676 @item s.reuseaddr(opval)
677 @findex sockbuf::reuseaddr
678 if @code{opval} is not -1, set the @code{sockbuf::so_reuseaddr} option value
679 to @code{opval}. In any case, return the old option value of
680 @code{sockbuf::so_reuseaddr} option. The default value of @code{opval}
681 is -1.
682
683 @item s.dontroute(opval)
684 @findex sockbuf::dontroute
685 if @code{opval} is not -1, set the @code{sockbuf::so_dontroute} option value
686 to @code{opval}. In any case, return the old option value of
687 @code{sockbuf::so_dontroute} option. The default value of @code{opval}
688 is -1.
689
690 @item s.oobinline(opval)
691 @findex sockbuf::oobinline
692 if @code{opval} is not -1, set the @code{sockbuf::so_oobinline} option value
693 to @code{opval}. In any case, return the old option value of
694 @code{sockbuf::so_oobinline} option. The default value of @code{opval}
695 is -1.
696
697 @item s.broadcast(opval)
698 @findex sockbuf::broadcast
699 if @code{opval} is not -1, set the @code{sockbuf::so_broadcast} option value
700 to @code{opval}. In any case, return the old option value of
701 @code{sockbuf::so_broadcast} option. The default value of @code{opval}
702 is -1.
703
704 @item s.keepalive(opval)
705 @findex sockbuf::keepalive
706 if @code{opval} is not -1, set the @code{sockbuf::so_keepalive} option value
707 to @code{opval}. In any case, return the old option value of
708 @code{sockbuf::so_keepalive} option. The default value of @code{opval}
709 is -1.
710
711 @item s.sendbufsz(opval)
712 @findex sockbuf::sndbuf
713 if @code{opval} is not -1, set the new send buffer size to @code{opval}.
714 In any case, return the old buffer size of the send buffer. The default
715 value of @code{opval} is -1.
716
717 @item s.recvbufsz(opval)
718 @findex sockbuf::rcvbuf
719 if @code{opval} is not -1, set the new recv buffer size to @code{opval}.
720 In any case, return the old buffer size of the recv buffer. The default
721 value of @code{opval} is -1.
722
723 @item s.linger(tim)
724 @findex sockbuf::linger
725 if @code{tim} is positive, set the linger time to tim seconds.
726 If @code{tim} is 0, set the linger off.
727 In any case, return the old linger time if it was set earlier.
728 Otherwise return -1. The default value of @code{tim} is -1.
729
730 @end table
731
732 @node Timeouts
733 @section Time Outs While Reading and Writing
734 @cindex timeouts
735 @cindex read timeouts
736 @cindex write timeouts
737
738 Time outs are very useful in handling data of unknown sizes
739 and formats while reading and writing. For example, how does
740 one communicate with a socket that sends chunks of data
741 of unknown size and format? If only @code{sockbuf::read} is used
742 without time out, it will block indefinitely.
743 In such cases, time out facility is the only answer.
744
745 The following idiom is recommended. @xref{Pitfalls} for a complete
746 example.
747
748 @example
749 int old_tmo = s.recvtimeout (2) // set time out (2 seconds here)
750 for (;;) @{ // read or write
751 char buf[256];
752 int rval = s.read (buf, 256);
753 if (rval == 0 || rval == EOF) break;
754 // process buf here
755 @}
756 s.recvtimeout (old_tmo); // reset time out
757 @end example
758
759 In what follows,
760 @itemize @minus
761 @item
762 @code{s} is a @code{sockbuf} object
763 @item
764 @code{wp} is waiting period in seconds
765 @end itemize
766
767 @table @code
768
769 @item s.recvtimeout(wp)
770 @findex sockbuf::recvtimeout
771 sets the recv timeout to @code{wp} seconds. If @code{wp} is -1, it is a
772 block and if @code{wp} is 0, it is a poll.
773
774 It affects all read functions. If the socket is not read ready within
775 @code{wp} seconds, the read call will return 0. It also affects
776 @code{sockbuf::underflow}. @code{sockbuf::underflow} will not set the
777 @code{_S_EOF_SEEN} flag if it is returning EOF because of timeout.
778
779 @code{sockbuf::recvtimeout} returns the old recv timeout value.
780
781 @item s.sendtimeout(wp)
782 @findex sockbuf::sendtimeout
783 sets the send timeout to @code{wp} seconds. If @code{wp} is -1, it is a
784 block and if @code{wp} is 0, it is a poll.
785
786 It affects all write functions. If the socket is not write ready within
787 @code{wp} seconds, the write call will return 0.
788
789 @code{sockbuf::sendtimeout} returns the old send timeout value.
790
791 @end table
792
793 @node sockAddr Class
794 @chapter sockAddr Class
795 @cindex sockAddr class
796 @cindex base address class
797
798 Class @code{sockAddr} is an abstract base class for all socket address
799 classes. That is, domain specific socket address classes are all derived
800 from @code{sockAddr} class.
801 @quotation
802 @emph{Note}: @code{sockAddr} is not spelled @code{sockaddr} in
803 order to prevent name clash with @code{struct sockaddr} declared
804 in @file{<sys/socket.h>}.
805 @end quotation
806
807 Non-abstract derived classes must have definitions for the following
808 functions.
809
810 @table @code
811
812 @item sockAddr::operator void* ()
813 @findex sockAddr::operator void*
814 should simply return @code{this}.
815
816 @item sockAddr::size()
817 @findex sockAddr::size
818 should return @code{sizeof(*this)}. The return type is @code{int}.
819
820 @item sockAddr::family()
821 @findex sockAddr::family
822 should return address family (domain name) of the socket address. The
823 return type is @code{int}
824
825 @end table
826
827 @node sockinetbuf Class
828 @chapter sockinetbuf Class
829 @cindex sockinetbuf class
830 @cindex inet domain
831
832 @code{sockinetbuf} class is derived from @code{sockbuf} class and inherits
833 most of the public functions of @code{sockbuf}. @xref{sockbuf Class},
834 for more information on @code{sockbuf}. In addition, it provides methods
835 for getting @code{sockinetaddr} of local and peer connections.
836 @xref{sockinetaddr Class}, for more information on @code{sockinetaddr}.
837
838 @menu
839 * Methods sockinetbuf:: Describes sockinetbuf member functions.
840 * Datagram INET:: A pair of example programs demonstrating
841 datagram connection in inet domain.
842 * Stream INET:: A pair of example programs demonstrating
843 stream connection in inet domain.
844 @end menu
845
846 @node Methods sockinetbuf
847 @section Methods
848
849 In what follows,
850 @itemize @minus
851 @item
852 @code{ty} denotes the type of the socket connection and is of type
853 @code{sockbuf::type}
854 @item
855 @code{proto} denotes the protocol and is of type int
856 @item
857 @code{si, ins} are @code{sockbuf} objects and are in @var{inet} domain
858 @item
859 @code{adr} denotes an @var{inet} address in host byte order and is of
860 type unsigned long
861 @item
862 @code{serv} denotes a service like "nntp" and is of type char*
863 @item
864 @code{proto} denotes a protocol like "tcp" and is of type char*
865 @item
866 @code{thostname} is of type char* and denotes the name of a host like
867 @code{"kelvin.acc.virginia.edu"} or @code{"128.143.24.31"}.
868 @item
869 @code{portno} denotes a port in host byte order and is of type int
870 @end itemize
871
872 @table @code
873
874 @item sockinetbuf ins(ty, proto)
875 @findex sockinetbuf::sockinetbuf
876 Constructs a @code{sockinetbuf} object @code{ins} whose socket
877 communication type is @code{ty} and protocol is @code{proto}.
878 @code{proto} defaults to 0.
879
880 @item sockinetbuf ins(si)
881 Constructs a @code{sockinetbuf} object @code{ins} which uses the same
882 socket as @code{si} uses.
883
884 @item ins = si
885 @findex sockinetbuf::operator =
886 performs the same function as @code{sockbuf::operator=}.
887 @xref{sockbuf Class}, for more details.
888
889 @item ins.open(ty, proto)
890 @findex sockinetbuf::open
891 create a new @code{sockinetbuf} whose type and protocol are
892 @code{ty} and @code{proto} respectively and assign it to @code{ins}.
893
894 @item sockinetaddr sina = ins.localaddr()
895 @findex sockinetbuf::localaddr
896 @findex getsockname (see sockinetbuf::localaddr)
897 returns the local @var{inet} address of the @code{sockinetbuf} object
898 @code{ins}. The call will make sense only after a call to either
899 @code{sockbuf::bind} or @code{sockbuf::connect}.
900
901 @item sockinetaddr sina = ins.peeraddr()
902 @findex sockinetbuf::peeraddr
903 @findex getpeername (see sockinetbuf::peeraddr)
904 returns the peer @var{inet} address of the @code{sockinetbuf} object
905 @code{ins}. The call will make sense only after a call to
906 @code{sockbuf::connect}.
907
908 @item const char* hn = ins.localhost()
909 @findex sockinetbuf::localhost
910 returns the local @var{inet} thostname of the @code{sockinetbuf} object
911 @code{ins}. The call will make sense only after a call to either
912 @code{sockbuf::bind} or @code{sockbuf::connect}.
913
914 @item const char* hn = ins.peerhost()
915 @findex sockinetbuf::peerhost
916 returns the peer @var{inet} thostname of the @code{sockinetbuf} object
917 @code{ins}. The call will make sense only after a call to
918 @code{sockbuf::connect}.
919
920 @item int pn = ins.localport()
921 @findex sockinetbuf::localport
922 returns the local @var{inet} port number of the @code{sockinetbuf} object
923 @code{ins} in host byte order. The call will make sense only after a
924 call to either @code{sockbuf::bind} or @code{sockbuf::connect}.
925
926 @item int pn = ins.peerport()
927 @findex sockinetbuf::peerport
928 returns the peer @var{inet} port number of the @code{sockinetbuf} object
929 @code{ins} in local host byte order. The call will make sense only after a
930 call to @code{sockbuf::connect}.
931
932 @item ins.bind ()
933 @findex sockinetbuf::bind
934 binds @code{ins} to the default address @var{INADDR_ANY} and the default
935 port. It returns 0 on success and returns the errno on failure.
936
937 @item ins.bind (adr, portno)
938 binds @code{ins} to the address @code{adr} and the port @code{portno}.
939 It returns 0 on success and returns the errno on failure.
940
941 @item ins.bind (adr, serv, proto)
942 binds @code{ins} to the address, @code{adr} and the port corresponding to
943 the service @code{serv} and the protocol @code{proto}>.
944 It returns 0 on success and returns the errno on failure.
945
946 @item ins.bind (thostname, portno)
947 binds @code{ins} to the address corresponding to the hostname
948 @code{thostname} and the port @code{portno}.
949 It returns 0 on success and returns the errno on failure.
950
951 @item ins.bind (thostname, serv, proto)
952 binds @code{ins} to the address corresponding to the hostname
953 @code{thostname} and the port corresponding to the service @code{serv}
954 and the protocol @code{proto}>. It returns 0 on success and
955 returns the errno on failure.
956
957 @item ins.connect (adr, portno)
958 @findex sockinetbuf::connect
959 connects @code{ins} to the address @code{adr} and the port @code{portno}.
960 It returns 0 on success and returns the errno on failure.
961
962 @item ins.connect (adr, serv, proto)
963 connects @code{ins} to the address, @code{adr} and the port corresponding to
964 the service @code{serv} and the protocol @code{proto}>.
965 It returns 0 on success and returns the errno on failure.
966
967 @item ins.connect (thostname, portno)
968 connects @code{ins} to the address corresponding to the hostname
969 @code{thostname} and the port @code{portno}.
970 It returns 0 on success and returns the errno on failure.
971
972 @item ins.connect (thostname, serv, proto)
973 connects @code{ins} to the address corresponding to the hostname
974 @code{thostname} and the port corresponding to the service @code{serv}
975 and the protocol @code{proto}>.
976 It returns 0 on success and returns the errno on failure.
977
978 @end table
979
980 @node Datagram INET
981 @section @var{inet} Datagram Sockets
982 @cindex sockinetbuf dgram example
983 @cindex datagram inet
984 @cindex isockinet example
985 @cindex osockinet example
986
987 The following two programs illustrates how to use @code{sockinetbuf} class
988 for datagram connection in @var{inet} domain. @code{tdinread.cc} also
989 shows how to use @code{isockinet} class and @code{tdinwrite.cc} shows
990 how to use @code{osockinet} class.
991
992 @subheading tdinread.cc
993 @example
994 // reads data sent by tdinwrite.cc
995 #include <sockinet.h>
996
997 int main(int ac, char** av)
998 @{
999 isockinet is (sockbuf::sock_dgram);
1000 is->bind();
1001
1002 cout << "localhost = " << so.localhost() << endl
1003 << "localport = " << so.localport() << endl;
1004
1005 char buf[256];
1006 int n;
1007
1008 is >> n;
1009 cout << av[0] << ": ";
1010 while(n--) @{
1011 is >> buf;
1012 cout << buf << ' ';
1013 @}
1014 cout << endl;
1015
1016 return 0;
1017 @}
1018 @end example
1019
1020 @subheading tdinwrite.cc
1021 @example
1022 // sends data to tdinread.cc
1023 #include <sockinetbuf.h>
1024 #include <stdlib.h>
1025
1026 int main(int ac, char** av)
1027 @{
1028 if (ac < 3) @{
1029 cerr << "USAGE: " << av[0] << " thostname port-number "
1030 << "data ... " << endl;
1031 return 1;
1032 @}
1033
1034 osockinet os (sockbuf::sock_dgram);
1035 os->connect (av[1], atoi(av[2]));
1036
1037 cout << "local: " << so.localport() << ' '
1038 << so.localhost() << endl
1039 << "peer: " << so.peerport() << ' '
1040 << so.peerhost() << endl;
1041
1042 os << ac-3; av += 3;
1043 while(*av) os << *av++ << ' ';
1044 os << endl;
1045
1046 return 0;
1047 @}
1048
1049 @end example
1050
1051 @node Stream INET
1052 @section @var{inet} Stream Sockets
1053 @cindex stream inet
1054 @cindex sockinetbuf stream example
1055 @cindex iosockinet example
1056
1057 The following two programs illustrates the use of @code{sockinetbuf} class
1058 for stream connection in @var{inet} domain. It also shows how to use
1059 @code{iosockinet} class.
1060
1061 @subheading tsinread.cc
1062 @example
1063 // receives strings from tsinwrite.cc and sends the strlen
1064 // of each string back to tsinwrite.cc
1065 #include <sockinet.h>
1066
1067 int main()
1068 @{
1069 sockinetbuf si(sockbuf::sock_stream);
1070 si.bind();
1071
1072 cout << si.localhost() << ' ' << si.localport() << endl;
1073 si.listen();
1074
1075 iosockinet s = si.accept();
1076 char buf[1024];
1077
1078 while (s >> buf) @{
1079 cout << buf << ' ';
1080 s << ::strlen(buf) << endl;
1081 @}
1082 cout << endl;
1083
1084 return 0;
1085 @}
1086 @end example
1087
1088 @subheading tsinwrite.cc
1089 @example
1090 // sends strings to tsinread.cc and gets back their length
1091 // usage: tsinwrite hostname portno
1092 // see the output of tsinread for what hostname and portno to use
1093
1094 #include <sockinet.h>
1095 #include <stdlib.h>
1096
1097 int main(int ac, char** av)
1098 @{
1099 iosockinet sio (sockbuf::sock_stream);
1100 sio->connect (av[1], atoi (av[2]));
1101
1102 sio << "Hello! This is a test\n" << flush;
1103
1104 // terminate the while loop in tsinread.cc
1105 si.shutdown(sockbuf::shut_write);
1106
1107 int len;
1108 while (s >> len) cout << len << ' ';
1109 cout << endl;
1110
1111 return 0;
1112 @}
1113 @end example
1114
1115 @node sockinetaddr Class
1116 @chapter sockinetaddr Class
1117 @cindex sockinetaddr class
1118 @cindex inet address class
1119
1120 Class @code{sockinetaddr} is derived from @code{sockAddr} declared in
1121 @code{<sockstream.h>} and from @code{sockaddr_in} declared in
1122 @code{<netinet/in.h>}. Always use a @code{sockinetaddr} object for an
1123 address with @var{inet} domain of sockets. @xref{Connection
1124 Establishment}.
1125
1126 In what follows,
1127 @itemize @minus
1128 @item
1129 @code{adr} denotes an @var{inet} address in host byte order and is of
1130 type unsigned long
1131 @item
1132 @code{serv} denotes a service like "nntp" and is of type char*
1133 @item
1134 @code{proto} denotes a protocol like "tcp" and is of type char*
1135 @item
1136 @code{thostname} is of type char* and denotes the name of a host like
1137 @code{"kelvin.acc.virginia.edu"} or @code{"128.143.24.31"}.
1138 @item
1139 @code{portno} denotes a port in host byte order and is of type int
1140 @end itemize
1141
1142 @table @code
1143
1144 @item sockinetaddr sina
1145 @findex sockinetaddr::sockinetaddr
1146 Constructs a @code{sockinetaddr} object @code{sina} with default address
1147 @var{INADDR_ANY} and default port number 0.
1148
1149 @item sockinetaddr sina(adr, portno)
1150 Constructs a @code{sockinetaddr} object @code{sina} setting inet address
1151 to @code{adr} and the port number to @code{portno}. @code{portno}
1152 defaults to 0.
1153
1154 @item sockinetaddr sina(adr, serv, proto)
1155 Constructs a @code{sockinetaddr} object @code{sina} setting inet address
1156 to @code{adr} and the port number corresponding to the service
1157 @code{serv} and the protocol @code{proto}. The protocol defaults to "tcp".
1158
1159 @item sockinetaddr sina(thostname, portno)
1160 Constructs a @code{sockinetaddr} object @code{sina} setting inet address
1161 to the address of @code{thostname} and the port number to @code{portno}.
1162 @code{portno} defaults to 0.
1163
1164 @item sockinetaddr sina(thostname, serv, proto)
1165 Constructs a @code{sockinetaddr} object @code{sina} setting inet address
1166 to the address of @code{thostname} and the port number corresponding to
1167 the service @code{serv} and the protocol @code{proto}. The protocol
1168 defaults to "tcp".
1169
1170 @item void* a = sina
1171 @findex sockinetaddr::operator void*
1172 returns the address of the @code{sockaddr_in} part of
1173 @code{sockinetaddr} object @code{sina} as void*.
1174
1175 @item int sz = sina.size()
1176 @findex sockinetaddr::size
1177 returns the sizeof @code{sockaddr_in} part of @code{sockinetaddr} object
1178 @code{sina}.
1179
1180 @item int af = sina.family()
1181 @findex sockinetaddr::family
1182 returns @code{sockinetbuf::af_inet} if all is well.
1183
1184 @item int pn = sina.getport()
1185 @findex sockinetaddr::getport
1186 returns the port number of the @code{sockinetaddr} object @code{sina} in
1187 host byte order.
1188
1189 @item const char* hn = getthostname()
1190 @findex sockinetaddr::getthostname
1191 returns the host name of the @code{sockinetaddr} object @code{sina}.
1192
1193 @end table
1194
1195 @node sockunixbuf Class
1196 @chapter sockunixbuf Class
1197 @cindex sockunixbuf class
1198 @cindex unix domain
1199
1200 @code{sockunixbuf} class is derived from @code{sockbuf} class declared in
1201 @code{<sockstream.h>} and hence, inherits most of the public member
1202 functions of @code{sockbuf}. @xref{sockbuf Class}, for more information
1203 on @code{sockbuf}.
1204
1205 @menu
1206 * Methods sockunixbuf:: Describes sockunixbuf member functions
1207 * Datagram UNIX:: A pair of example programs demonstrating
1208 datagram connection in @var{unix} domain
1209 * Stream UNIX:: A pair of example programs demonstrating
1210 stream connection in @var{unix} domain
1211 @end menu
1212
1213 @node Methods sockunixbuf
1214 @section Methods
1215
1216 In what follows,
1217 @itemize @minus
1218 @item
1219 @code{ty} denotes the socket type and is of type @code{sockbuf::type}
1220 @item
1221 @code{proto} denotes the protocol number and is of type int
1222 @item
1223 @code{su} is a @code{sockbuf} and must be in @var{unix} domain
1224 @item
1225 @code{path} is the @var{unix} path name like "/tmp/unix_socket"
1226 @end itemize
1227
1228 @table @code
1229
1230 @item sockunixbuf uns(ty, proto)
1231 @findex sockunixbuf::sockunixbuf
1232 Constructs a @code{sockunixbuf} object @code{uns} with @code{ty} as its
1233 type and @code{proto} as its protocol number. @code{proto} defaults to
1234 0.
1235
1236 @item sockunixbuf uns = su
1237 Constructs a @code{sockunixbuf} object @code{uns} which uses the same
1238 socket as is used by @code{su}.
1239
1240 @item uns = su
1241 @findex sockunixbuf::operator =
1242 @code{sockunixbuf} object @code{uns} closes its current socket if no other
1243 @code{sockbuf} is referring to it and uses the socket that @code{sockbuf}
1244 object @code{su} is using.
1245
1246 @item uns.open(ty, proto)
1247 @findex sockunixbuf::open
1248 create a @code{sockunixbuf} object with @code{ty} as its type and
1249 @code{proto} as its protocol and assign the @code{sockunixbuf} object so
1250 created to @code{*this}. It returns @code{this}. @code{proto} defaults
1251 to 0.
1252
1253 @item uns.bind(path)
1254 @findex sockunixbuf::bind
1255 binds @code{uns} to the @var{unix} pathname @code{path}.
1256 It returns 0 on success and returns the errno on failure.
1257
1258 @item uns.connect(path)
1259 @findex sockunixbuf::connect
1260 connects @code{uns} to the @var{unix} pathname @code{path}.
1261 It returns 0 on success and returns the errno on failure.
1262
1263 @end table
1264
1265 @node Datagram UNIX
1266 @section @var{unix} Datagram Sockets
1267 @cindex datagram unix
1268 @cindex isockunix example
1269 @cindex osockunix example
1270
1271 The following two programs illustrates how to use @code{sockunixbuf} class
1272 for datagram connection in @var{unix} domain. @code{tdunread.cc} also
1273 shows how to use @code{isockunix} class and @code{tdunwrite.cc} shows
1274 how to use @code{osockunix} class.
1275
1276 @subheading tdunread.cc
1277 @example
1278 // reads data sent by tdunwrite.cc
1279 #include <sockunix.h>
1280 #include <unistd.h>
1281 #include <errno.h>
1282
1283 int main(int ac, char** av)
1284 @{
1285 if (ac != 2) @{
1286 cerr << "USAGE: " << av[0] << " socket_path_name\n";
1287 return 1;
1288 @}
1289
1290 // isockunix builds the sockunixbuf object
1291 isockunix su (sockbuf::sock_dgram);
1292
1293 su->bind(av[1]);
1294
1295 cout << "Socket name = " << av[1] << endl;
1296
1297 if (chmod(av[1], 0777) == -1) @{
1298 perror("chmod");
1299 return 1;
1300 @}
1301
1302 char buf[1024];
1303 int i;
1304 su >> i;
1305 cout << av[0] << ": " << i << " strings: ";
1306 while (i--) @{
1307 su >> buf;
1308 cout << buf << ' ';
1309 @}
1310 cout << endl;
1311
1312 unlink(av[1]);
1313 return 0;
1314 @}
1315 @end example
1316
1317 @subheading tdunwrite.cc
1318 @example
1319 // sends data to tdunread.cc
1320 #include <sockunix.h>
1321
1322 int main(int ac, char** av)
1323 @{
1324 if (ac < 2) @{
1325 cerr << "USAGE: " << av[0]
1326 << " socket_path_name data...\n";
1327 return 1;
1328 @}
1329
1330 osockunix su (sockbuf::sock_dgram);
1331
1332 su->connect (av[1]);
1333
1334 su << ac << ' ';
1335 while (*av) @{ su << av[i] << ' '; av++; @}
1336 su << endl;
1337
1338 return 0;
1339 @}
1340 @end example
1341
1342 @node Stream UNIX
1343 @section @var{unix} Stream Sockets
1344 @cindex stream unix
1345 @cindex sockunixbuf example
1346 @cindex iosockunix example
1347
1348 The following two programs illustrates how to use @code{sockunixbuf} class
1349 for stream connection in @var{unix} domain. It also shows how to use
1350 @code{iosockunix} class.
1351
1352 @subheading tsunread.cc
1353 @example
1354 // exchanges char strings with tsunwrite.cc
1355 #include <sockunix.h>
1356 #include <unistd.h>
1357 #include <errno.h>
1358
1359 int main(int ac, char** av)
1360 @{
1361 if (ac != 2) @{
1362 cerr << "USAGE: " << av[0] << " socket_path_name\n";
1363 return 1;
1364 @}
1365
1366 sockunixbuf su(sockbuf::sock_stream);
1367 su.bind(av [1]);
1368
1369 cout << "Socket name = " << av[1] << endl;
1370
1371 if (chmod(av[1], 0777) == -1) @{
1372 perror("chmod");
1373 return 1;
1374 @}
1375
1376 su.listen(3);
1377
1378 iosockunix ioput = su.accept ();
1379 char buf[1024];
1380
1381 ioput << av[0] << ' ' << av[1] << endl;
1382 while ( ioput >> buf ) cout << av[0] << ": " << buf << endl;
1383 unlink(av[1]);
1384 return 0;
1385 @}
1386 @end example
1387
1388 @subheading tsunwrite.cc
1389 @example
1390 // exchanges char strings with tsunread.cc
1391 #include <sockunix.h>
1392
1393 int main(int ac, char** av)
1394 @{
1395 if (ac < 2) @{
1396 cerr << "USAGE: " << av[0]
1397 << " socket_path_name data...\n";
1398 return 1;
1399 @}
1400
1401 iosockunix oput (sockbuf::sock_stream);
1402 oput->connect (av [1]);
1403
1404 char buf[128];
1405
1406 oput >> buf;
1407 cout << buf << ' ';
1408 oput >> buf;
1409 cout << buf << endl;
1410
1411 while (*av) oput << *av++ << ' ';
1412 oput << endl;
1413
1414 return 0;
1415 @}
1416 @end example
1417
1418 @node sockunixaddr Class
1419 @chapter sockunixaddr Class
1420 @cindex sockunixaddr class
1421 @cindex unix address class
1422
1423 Class @code{sockunixaddr} is derived from class @code{sockAddr} declared in
1424 @code{<sockstream.h>} and from struct @code{sockaddr_un} declared in
1425 @code{<sys/un.h>}. Always use @code{sockunixaddr} objects for addresses
1426 with @var{unix} domain of sockets. @xref{Connection Establishment}.
1427
1428 In what follows,
1429 @itemize @minus
1430 @item
1431 @code{path} is the @var{unix} path name like "/tmp/unix_socket"
1432 @end itemize
1433
1434 @table @code
1435
1436 @item sockunixaddr suna(path)
1437 @findex sockunixaddr::sockunixaddr
1438 Constructs a @code{sockunixaddr} object @code{suna} with @code{path} as
1439 the @var{unix} path name.
1440
1441 @item void* a = suna
1442 @findex sockunixaddr::operator void*
1443 returns the address of the @code{sockaddr_un} part of
1444 @code{sockunixaddr} object @code{suna} as void*.
1445
1446 @item int sz = suna.size()
1447 @findex sockunixaddr::size
1448 returns the sizeof @code{sockaddr_un} part of @code{sockunixaddr} object
1449 @code{suna}.
1450
1451 @item int af = suna.family()
1452 @findex sockunixaddr::family
1453 returns @code{sockunixbuf::af_unix} if all is well.
1454
1455 @end table
1456
1457 @node sockstream Classes
1458 @chapter sockstream Classes
1459 @cindex sockstream classes
1460 @cindex iosockstream classes
1461
1462 sockstream classes are designed in such a way that they provide the same
1463 interface as their stream counterparts do. We have @code{isockstream}
1464 derived from @code{istream} and @code{osockstream} derived from
1465 @code{ostream}. We also have @code{iosockstream} which is derived from
1466 @code{iostream}.
1467
1468 Each domain also has its own set of @code{stream} classes. For example,
1469 @code{unix} domain has @code{isockunix}, @code{osockunix}, and
1470 @code{iosockunix} derived from @code{isockstream}, @code{osockstream},
1471 and @code{iosockstream} respectively. Similarly, @code{inet} domain has
1472 @code{isockinet}, @code{osockinet}, and @code{iosockinet}.
1473
1474 @menu
1475 * iosockstream:: Generic IOStream classes for sockbuf
1476 buffers.
1477 * iosockinet:: IOStream classes for @var{inet} domain of
1478 sockets.
1479 * iosockunix:: IOStream classes for @var{unix} domain of
1480 sockets.
1481 @end menu
1482
1483 @node iosockstream
1484 @section iosockstreams
1485
1486 @subsection isockstream Class
1487 @cindex isockstream class
1488
1489 Since @code{isockstream} is publicly derived from @code{istream}, most
1490 of the public functions of @code{istream} are also available in
1491 @code{isockstream}.
1492
1493 @code{isockstream} redefines @code{rdbuf()} defined in its virtual base
1494 class @code{ios}. Since, @code{ios::rdbuf()} is not virtual, care must
1495 be taken to call the correct @code{rdbuf()} through a reference or a
1496 pointer to an object of class @code{isockstream}.
1497
1498 In what follows,
1499 @itemize @minus
1500 @item
1501 @code{sb} is a @code{sockbuf} object
1502 @item
1503 @code{sbp} is a pointer to a @code{sockbuf} object
1504 @end itemize
1505
1506 @table @code
1507
1508 @item isockstream is(sb)
1509 @findex isockstream::isockstream
1510 Constructs an @code{isockstream} object @code{is} with @code{sb} as its
1511 @code{sockbuf}.
1512
1513 @item isockstream is(sbp)
1514 Constructs an @code{isockstream} object @code{is} with @code{*sbp} as its
1515 @code{sockbuf}.
1516
1517 @item sbp = is.rdbuf()
1518 @findex isockstream::rdbuf
1519 returns a pointer to the @code{sockbuf} of the @code{isockstream} object
1520 @code{is}.
1521
1522 @item isockstream::operator -> ()
1523 @findex isockstream::operator->
1524 returns a pointer to the @code{isockstream}'s @code{sockbuf} so that
1525 the user can use @code{isockstream} object as a @code{sockbuf} object.
1526
1527 @example
1528 is->connect (sa); // same as is.rdbuf()->connect (sa);
1529 @end example
1530
1531 @end table
1532
1533 @subsection osockstream Class
1534 @cindex osockstream class
1535
1536 Since @code{osockstream} is publicly derived from @code{ostream}, most
1537 of the public functions of @code{ostream} are also available in
1538 @code{osockstream}.
1539
1540 @code{osockstream} redefines @code{rdbuf()} defined in its virtual base
1541 class @code{ios}. Since, @code{ios::rdbuf()} is not virtual, care must
1542 be taken to call the correct @code{rdbuf()} through a reference or a
1543 pointer to an object of class @code{osockstream}.
1544
1545 In what follows,
1546 @itemize @minus
1547 @item
1548 @code{sb} is a @code{sockbuf} object
1549 @item
1550 @code{sbp} is a pointer to a @code{sockbuf} object
1551 @end itemize
1552
1553 @table @code
1554
1555 @item osockstream os(sb)
1556 @findex osockstream::osockstream
1557 Constructs an @code{osockstream} object @code{os} with @code{sb} as its
1558 @code{sockbuf}.
1559
1560 @item osockstream os(sbp)
1561 Constructs an @code{osockstream} object @code{os} with @code{*sbp} as its
1562 @code{sockbuf}.
1563
1564 @item sbp = os.rdbuf()
1565 @findex osockstream::rdbuf
1566 returns a pointer to the @code{sockbuf} of the @code{osockstream} object
1567 @code{os}.
1568
1569 @item osockstream::operator -> ()
1570 @findex osockstream::operator->
1571 returns a pointer to the @code{osockstream}'s @code{sockbuf} so that
1572 the user can use @code{osockstream} object as a @code{sockbuf} object.
1573
1574 @example
1575 os->connect (sa); // same as os.rdbuf()->connect (sa);
1576 @end example
1577
1578 @end table
1579
1580 @subsection iosockstream Class
1581 @cindex iosockstream class
1582
1583 Since @code{iosockstream} is publicly derived from @code{iostream}, most
1584 of the public functions of @code{iostream} are also available in
1585 @code{iosockstream}.
1586
1587 @code{iosockstream} redefines @code{rdbuf()} defined in its virtual base
1588 class @code{ios}. Since, @code{ios::rdbuf()} is not virtual, care must
1589 be taken to call the correct @code{rdbuf()} through a reference or a
1590 pointer to an object of class @code{iosockstream}.
1591
1592 In what follows,
1593 @itemize @minus
1594 @item
1595 @code{sb} is a @code{sockbuf} object
1596 @item
1597 @code{sbp} is a pointer to a @code{sockbuf} object
1598 @end itemize
1599
1600 @table @code
1601
1602 @item iosockstream io(sb)
1603 @findex iosockstream::iosockstream
1604 Constructs an @code{iosockstream} object @code{io} with @code{sb} as its
1605 @code{sockbuf}.
1606
1607 @item iosockstream io(sbp)
1608 Constructs an @code{iosockstream} object @code{io} with @code{*sbp} as its
1609 @code{sockbuf}.
1610
1611 @item sbp = io.rdbuf()
1612 @findex iosockstream::rdbuf
1613 returns a pointer to the @code{sockbuf} of the @code{iosockstream} object
1614 @code{io}.
1615
1616 @item iosockstream::operator -> ()
1617 @findex iosockstream::operator->
1618 returns a pointer to the @code{iosockstream}'s @code{sockbuf} so that
1619 the user can use @code{iosockstream} object as a @code{sockbuf} object.
1620
1621 @example
1622 io->connect (sa); // same as io.rdbuf()->connect (sa);
1623 @end example
1624
1625 @end table
1626
1627 @node iosockinet
1628 @section iosockinet Stream Classes
1629
1630 We discus only @code{isockinet} class here. @code{osockinet} and
1631 @code{iosockinet} are similar and are left out. However, they are
1632 covered in the examples that follow.
1633
1634 @subsection isockinet
1635 @cindex isockinet class
1636 @cindex class isockinet
1637
1638 @code{isockinet} is used to handle interprocess communication in
1639 @var{inet} domain. It is derived from @code{isockstream} class and it
1640 uses a @code{sockinetbuf} as its stream buffer. @xref{iosockstream}, for
1641 more details on @code{isockstream}. @xref{sockinetbuf Class}, for
1642 information on @code{sockinetbuf}.
1643
1644 In what follows,
1645 @itemize @minus
1646 @item
1647 @code{ty} is a @code{sockbuf::type} and must be one of
1648 @code{sockbuf::sock_stream}, @code{sockbuf::sock_dgram},
1649 @code{sockbuf::sock_raw}, @code{sockbuf::sock_rdm}, and
1650 @code{sockbuf::sock_seqpacket}
1651 @item
1652 @code{proto} denotes the protocol number and is of type int
1653 @item
1654 @code{sb} is a @code{sockbuf} object and must be in @var{inet} domain
1655 @item
1656 @code{sinp} is a pointer to an object of @code{sockinetbuf}
1657 @end itemize
1658
1659 @table @code
1660
1661 @item isockinet is (ty, proto)
1662 @findex isockinet::isockinet
1663 constructs an @code{isockinet} object @code{is} whose @code{sockinetbuf}
1664 buffer is of the type @code{ty} and has the protocol number
1665 @code{proto}. The default protocol number is 0.
1666
1667 @item isockinet is (sb)
1668 constructs a @code{isockinet} object @code{is} whose @code{sockinetbuf}
1669 is @code{sb}. @code{sb} must be in @var{inet} domain.
1670
1671 @item isockinet is (sinp)
1672 constructs a @code{isockinet} object @code{is} whose @code{sockinetbuf}
1673 is @code{sinp}.
1674
1675 @item sinp = is.rdbuf ()
1676 @findex isockinet::rdbuf
1677 returns a pointer to the @code{sockinetbuf} of @code{isockinet} object
1678 @code{is}.
1679
1680 @item isockinet::operator ->
1681 @findex isockinet::operator->
1682 returns @code{sockinetbuf} of @code{sockinet} so that the @code{sockinet}
1683 object acts as a smart pointer to @code{sockinetbuf}.
1684
1685 @example
1686 is->localhost (); // same as is.rdbuf ()->localhost ();
1687 @end example
1688
1689 @end table
1690
1691 @subsection iosockinet examples
1692 @cindex iosockinet examples
1693
1694 The first pair of examples demonstrates datagram socket connections in the
1695 @var{inet} domain. First, @code{tdinread} prints its local host and
1696 local port on stdout and waits for input in the connection.
1697 @code{tdinwrite} is started with the local host and local port of
1698 @code{tdinread} as arguments. It sends the string "How do ye do!" to
1699 @code{tdinread} which in turn reads the string and prints on its stdout.
1700
1701 @example
1702 // tdinread.cc
1703 #include <sockinet.h>
1704
1705 int main ()
1706 @{
1707 char buf[256];
1708 isockinet is (sockbuf::sock_dgram);
1709 is->bind ();
1710
1711 cout << is->localhost() << ' ' << is->localport() << endl;
1712
1713 is.getline (buf);
1714 cout << buf << endl;
1715
1716 return 0;
1717 @}
1718 @end example
1719
1720 @example
1721 // tdinwrite.cc--tdinwrite hostname portno
1722 #include <sockinet.h>
1723 #include <stdlib.h>
1724
1725 int main (int ac, char** av)
1726 @{
1727 osockinet os (sockbuf::sock_dgram);
1728 os->connect (av[1], atoi(av[2]));
1729 os << "How do ye do!" << endl;
1730 return 0;
1731 @}
1732 @end example
1733
1734 The next example communicates with an nntp server through a
1735 @code{sockbuf::sock_stream} socket connection in @var{inet} domain.
1736 After establishing a connection to the nntp server, it sends a "HELP"
1737 command and gets back the HELP message before sending the "QUIT"
1738 command.
1739
1740 @example
1741 // tnntp.cc
1742 #include <sockinet.h>
1743
1744 int main ()
1745 @{
1746 char buf[1024];
1747 iosockinet io (sockbuf::sock_stream);
1748 io->connect ("murdoch.acc.virginia.edu", "nntp", "tcp");
1749 io.getline (buf, 1024); cout << buf << endl;
1750 io << "HELP\r\n" << flush;
1751 io.getline (buf, 1024); cout << buf << endl;
1752 while (io.getline (buf, 1024))
1753 if (buf[0] == '.' && buf[1] == '\r') break;
1754 else if (buf[0] == '.' && buf[1] == '.') cout << buf+1 << endl;
1755 else cout << buf << endl;
1756 io << "QUIT\r\n" << flush;
1757 io.getline (buf, 1024); cout << buf << endl;
1758 return 0;
1759 @}
1760 @end example
1761
1762 @node iosockunix
1763 @section iosockunix Classes
1764 @cindex iosockunix class
1765
1766 We discuss only @code{isockunix} here. @code{osockunix} and
1767 @code{iosockunix} are similar.
1768
1769 @subsection isockunix class
1770 @cindex isockunix class
1771 @cindex class isockunix
1772
1773 @code{isockunix} is used to handle interprocess communication in
1774 @var{unix} domain. It is derived from @code{isockstream} class and it
1775 uses a @code{sockunixbuf} as its stream buffer. @xref{iosockstream}, for
1776 more details on @code{isockstream}. @xref{sockunixbuf Class}, for
1777 information on @code{sockunixbuf}.
1778
1779 In what follows,
1780 @itemize @minus
1781 @item
1782 @code{ty} is a @code{sockbuf::type} and must be one of
1783 @code{sockbuf::sock_stream}, @code{sockbuf::sock_dgram},
1784 @code{sockbuf::sock_raw}, @code{sockbuf::sock_rdm}, and
1785 @code{sockbuf::sock_seqpacket}
1786 @item
1787 @code{proto} denotes the protocol number and is of type int
1788 @item
1789 @code{sb} is a @code{sockbuf} object and must be in @var{unix} domain
1790 @item
1791 @code{sinp} is a pointer to an object of @code{sockunixbuf}
1792 @end itemize
1793
1794 @table @code
1795
1796 @item isockunix is (ty, proto)
1797 @findex isockunix::isockunix
1798 constructs an @code{isockunix} object @code{is} whose @code{sockunixbuf}
1799 buffer is of the type @code{ty} and has the protocol number
1800 @code{proto}. The default protocol number is 0.
1801
1802 @item isockunix is (sb)
1803 constructs a @code{isockunix} object @code{is} whose @code{sockunixbuf}
1804 is @code{sb}. @code{sb} must be in @var{unix} domain.
1805
1806 @item isockunix is (sinp)
1807 constructs a @code{isockunix} object @code{is} whose @code{sockunixbuf}
1808 is @code{sinp}.
1809
1810 @item sinp = is.rdbuf ()
1811 @findex isockunix::rdbuf
1812 returns a pointer to the @code{sockunixbuf} of @code{isockunix} object
1813 @code{is}.
1814
1815 @item isockunix::operator ->
1816 @findex isockunix::operator->
1817 returns @code{sockunixbuf} of @code{sockunix} so that the @code{sockunix}
1818 object acts as a smart pointer to @code{sockunixbuf}.
1819
1820 @example
1821 is->localhost (); // same as is.rdbuf ()->localhost ();
1822 @end example
1823
1824 @end table
1825
1826 @subsection iosockunix examples
1827 @cindex iosockunix examples
1828
1829 @code{tsunread} listens for connections. When @code{tsunwrite} requests
1830 connection, @code{tsunread} accepts it and waits for input.
1831 @code{tsunwrite} sends the string "Hello!!!" to @code{tsunread}.
1832 @code{tsunread} reads the string sent by @code{tsunwrite} and prints on
1833 its stdout.
1834
1835 @example
1836 // tsunread.cc
1837 #include <sockunix.h>
1838 #include <unistd.h>
1839
1840 int main ()
1841 @{
1842 sockunixbuf sunb (sockbuf::sock_stream);
1843 sunb.bind ("/tmp/socket+-");
1844 sunb.listen (2);
1845 isockunix is = sunb.accept ();
1846 char buf[32];
1847 is >> buf; cout << buf << endl;
1848 unlink ("/tmp/socket+-");
1849 return 0;
1850 @}
1851 @end example
1852
1853 @example
1854 // tsunwrite.cc
1855 #include <sockunix.h>
1856 int main ()
1857 @{
1858 osockunix os (sockbuf::sock_stream);
1859 os->connect ("/tmp/socket++");
1860 os << "Hello!!!\n" << flush;
1861 return 0;
1862 @}
1863 @end example
1864
1865 @node pipestream Classes
1866 @chapter pipestream Classes
1867 @cindex pipestream classes
1868 @cindex pipestream examples
1869 @findex popen
1870 @findex pipe
1871 @findex socketpair
1872
1873 @code{pipestream} stream classes provide the services of the @var{UNIX}
1874 system calls @code{pipe} and @code{socketpair} and the C library
1875 function @code{popen}. @code{ipipestream}, @code{opipestream}, and
1876 @code{iopipestream} are obtained by simply deriving from
1877 @code{isockstream}, @code{osockstream} and @code{iosockstream}
1878 respectively. @xref{sockstream Classes} for details.
1879
1880 In what follows,
1881 @itemize @minus
1882 @item
1883 @code{ip} is an @code{ipipestream} object
1884 @item
1885 @code{op} is an @code{opipestream} object
1886 @item
1887 @code{iop} is an @code{iopipestream} object
1888 @item
1889 @code{cmd} is a char* denoting an executable like "wc"
1890 @item
1891 @code{ty} is of type @code{sockbuf::type} indicating the type of the
1892 connection
1893 @item
1894 @code{proto} is an @code{int} denoting a protocol number
1895 @end itemize
1896
1897 @table @code
1898
1899 @item ipipestream ip(cmd)
1900 @findex ipipestream::ipipestream
1901 construct an @code{ipipestream} object @code{ip} such that the output of
1902 the command @code{cmd} is available as input through @code{ip}.
1903
1904 @item opipestream op(cmd)
1905 @findex opipestream::opipestream
1906 construct an @code{opipestream} object @code{op} such that the input for
1907 the command @code{cmd} can be send through @code{op}.
1908
1909 @item iopipestream iop(cmd)
1910 @findex iopipestream::iopipestream
1911 construct an @code{iopipestream} object @code{iop} such that the input
1912 and the output to the command @code{cmd} can be sent and received
1913 through @code{iop}.
1914
1915 @item iopipestream iop(ty, proto)
1916 construct a @code{iopipestream} object @code{iop} whose socket is a
1917 socketpair of type @code{ty} with protocol number @code{proto}.
1918 @code{ty} defaults to @code{sockbuf::sock_stream} and @code{proto}
1919 defaults to 0. Object @code{iop} can be used either as a @code{pipe} or
1920 as a @code{socketpair}.
1921
1922 @item iop.pid ()
1923 @findex iopipestream::pid
1924 return the process id of the child if the current process is the parent
1925 or return 0. If the process has not forked yet, return -1.
1926
1927 @item iopipestream::fork ()
1928 @findex iopipestream::fork
1929 @code{fork()} is a static function of class @code{iopipestream}.
1930 @code{fork()} forks the current process and appropriately sets the
1931 @code{cpid} field of the @code{iopipestream} objects that have not
1932 forked yet.
1933
1934 @end table
1935
1936 @menu
1937 * pipe Example:: How to use pipestream as pipe?
1938 * socketpair Example:: How to use pipestream as socketpair?
1939 * popen Example:: How to use pipestream as popen?
1940 @end menu
1941
1942 @node pipe Example
1943 @section pipestream as pipe
1944 @cindex pipe example
1945
1946 @code{pipe} is used to communicate between parent and child processes in
1947 the @var{unix} domain.
1948
1949 The following example illustrates how to use @code{iopipestream} class as
1950 a @code{pipe}. The parent sends the string "I am the parent" to the
1951 child and receives the string "I am the child" from child. The child, in
1952 turn, receives the string "I am the parent" from parent and sends the
1953 string "I am the child" to the parent. Note the same @code{iopipestream}
1954 object is used for input and output in each process.
1955
1956 @example
1957 #include <pipestream.h>
1958
1959 int main()
1960 @{
1961 iopipestream p;
1962 if ( p.fork() ) @{
1963 char buf[128];
1964 p << "I am the parent\n" << flush;
1965 cout << "parent: ";
1966 while(p >> buf)
1967 cout << buf << ' ';
1968 cout << endl;
1969 @}else @{
1970 char buf[128];
1971 p.getline(buf, 127);
1972 cout << "child: " << buf << endl;
1973 p << "I am the child\n" << flush;
1974 @}
1975 return 0;
1976 @}
1977 @end example
1978
1979 @node socketpair Example
1980 @section pipestream as socketpair
1981 @cindex socketpair example
1982
1983 Like pipes, socketpairs also allow communication between parent and
1984 child processes. But socketpairs are more flexible than pipes in the
1985 sense that they let the users choose the socket type and protocol.
1986
1987 The following example illustrates the use of @code{iopipestream} class as
1988 a @code{socketpair} whose type is @code{sockbuf::sock_dgram}. The parent
1989 sends the string "I am the parent" to the child and receives the string
1990 "I am the child" from the child. The child, in turn, receives and sends
1991 the strings "I am the parent" and "I am the child" respectively from and
1992 to the parent. Note in the following example that the same
1993 @code{iopipestream} object is used for both the input and the output in
1994 each process.
1995
1996 @example
1997 #include <pipestream.h>
1998
1999 int main()
2000 @{
2001 iopipestream p(sockbuf::sock_dgram);
2002 if ( iopipestream::fork() ) @{
2003 char buf[128];
2004 p << "I am the parent\n" << flush;
2005 p.getline(buf, 127);
2006 cout << "parent: " << buf << endl;
2007 @}else @{
2008 char buf[128];
2009 p.getline(buf, 127);
2010 cout << "child: " << buf << endl;
2011 p << "I am the child\n" << flush;
2012 @}
2013 return 0;
2014 @}
2015 @end example
2016
2017 @node popen Example
2018 @section pipestream as popen
2019 @cindex popen example
2020
2021 @code{popen} is used to call an executable and send inputs and
2022 outputs to that executable. For example, the following example
2023 executes "/bin/date", gets its output, and prints it to stdout.
2024
2025 @example
2026 #include <pipestream.h>
2027
2028 int main ()
2029 @{
2030 char buf[128];
2031 ipipestream p("/bin/date");
2032
2033 p.getline (buf, 127);
2034 cout << buf << endl;
2035 return 0;
2036 @}
2037 @end example
2038
2039 Here is an example that prints "Hello World!!" on stdout. It uses
2040 @code{opipestream} object.
2041
2042 @example
2043 #include <pipestream.h>
2044
2045 int main ()
2046 @{
2047 opipestream p("/bin/cat");
2048 p << "Hello World!!\n" << endl;
2049 return 0;
2050 @}
2051 @end example
2052
2053 The following example illustrates the use of @code{iopipestream} for
2054 both input and output.
2055
2056 @example
2057 #include <pipestream.h>
2058
2059 int main()
2060 @{
2061 char buf[128];
2062 iopipestream p("lpc");
2063 p << "help\nquit\n" << flush;
2064 while ( p.getline(buf, 127) ) cout << buf << endl;
2065 return 0;
2066 @}
2067 @end example
2068
2069 @node Fork Class
2070 @chapter Fork Class
2071 @cindex fork class
2072
2073 You can effectively use the @code{Fork} wrapper class to create child
2074 processes. You can use the @code{Fork} class, instead of directly using the
2075 system call fork (), if you desire the following:
2076
2077 @itemize @bullet
2078 @item
2079 Avoid zombie processes
2080 @item
2081 Optionally kill child processes when the parent process terminates.
2082 @item
2083 Want to know the reason for abnormal termination of child processes.
2084 @end itemize
2085
2086 In what follows,
2087 @itemize @minus
2088 @item
2089 @code{killchild} is an integer.
2090 @item
2091 @code{reason} is an integer.
2092 @item
2093 @code{signo} is a valid signal.
2094 @item
2095 @code{f} is a @code{Fork} object.
2096 @end itemize
2097
2098 @table @code
2099
2100 @item Fork f(killchild, reason)
2101 @findex Fork::Fork
2102 constructs a @code{Fork} object @code{f}. The constructor creates a child
2103 process. When the parent process terminates, it will kill the child
2104 process if @code{killchild} is not 0. Otherwise, the parent process will
2105 wait until all its child processes die. If @code{reason} is not 0, then
2106 it gives the reason for a child process's death on the stderr.
2107
2108 @item f.is_child ()
2109 @findex Fork::is_child
2110 returns 1 if the current process is the child process following the
2111 fork in constructing the @code{Fork} object @code{f}. Otherwise, return 0.
2112
2113 @item f.is_parent ()
2114 @findex Fork::is_parent
2115 returns 1 if the current process is the parent process following the
2116 fork in constructing the @code{Fork} object @code{f}. Otherwise, return 0.
2117
2118 @item f.process_id ()
2119 @findex Fork::process_id
2120 returns the process id of the child process, if the current process
2121 is the parent process. Returns 0, if the current process is the child
2122 process. Returns -1, if fork failed.
2123
2124 @item Fork::suicide_signal (signo)
2125 @findex Fork::suicide_signal
2126 is a static function. Upon the reciept of the signal
2127 @code{signo}, the current process will kill all its child processes
2128 created through @code{Fork::Fork(int, int)} irrespective of the value of
2129 the @code{killchild} flag used in the construction of the @code{Fork}
2130 objects. @code{signo} defaults to SIGTERM signal.
2131
2132 @end table
2133
2134 @section Fork Example
2135 @cindex fork example
2136
2137 The following example illustrates the use of the @code{Fork} class to
2138 create child processes. First, we set up @var{SIGTERM} signal handler
2139 to kill all the child processes, by callling @code{Fork::suicide_signal
2140 ()}. Second, we create several child and grandchild processes.
2141
2142 You can kill the top most parent process and all its children by sending
2143 a @var{SIGTERM} signal to the top most parent process.
2144
2145 @example
2146 // tfork.C
2147 #include <iostream.h>
2148 #include <Fork.h>
2149
2150 static void print (char* name, pid_t child)
2151 @{
2152 if (child)
2153 cerr << "Parent " << getppid () << "; "
2154 << name << ' ' << getpid () << "; Child " << child << ";\n";
2155 @}
2156
2157 int main (int ac, char** av)
2158 @{
2159 Fork::suicide_signal (SIGTERM);
2160
2161 Fork a(0, 1);
2162
2163 print ("a", a.process_id ());
2164
2165 if (a.is_child ()) @{
2166 sleep (3000);
2167 @} else if (a.is_parent ()) @{
2168 Fork b (1, 1);
2169 print ("b", b.process_id ());
2170 @{
2171 Fork c (b.is_parent (), 1);
2172 if (b.is_child ())
2173 print ("cchild", c.process_id ());
2174 else
2175 print ("cparent", c.process_id ());
2176 if (c.is_child ()) @{
2177 sleep (3000);
2178 return 0;
2179 @}
2180 @}
2181 if (b.is_child ()) @{
2182 sleep (120);
2183 return 0x8;
2184 @}
2185 @}
2186
2187 return 0;
2188 @}
2189
2190 @end example
2191
2192 @node protocol Class
2193 @chapter Class protocol
2194 @cindex protocol class
2195
2196 @code{protocol} class is the base class for all the other application
2197 protocol classes like @code{echo}, @code{smtp}, etc. @code{protocol}
2198 is derived publicly from @code{iosockstream}. It uses @code{protocolbuf}
2199 class, a nested class of @code{protocol}, as its stream buffer.
2200
2201 The @code{protocol} class is an abstract class and thus, you cannot
2202 instantiate an object of @code{protocol}.
2203
2204 @section Class protocol::protocolbuf
2205 @cindex protocolbuf class
2206
2207 @code{protocol::protocolbuf} class is publicly derived from
2208 @code{sockinetbuf} and thus, it inherits all the latter's public member
2209 functions. In addition, the @code{protocolbuf} defines the following
2210 member functions.
2211
2212 In what follows,
2213 @itemize @minus
2214 @item
2215 @code{p} is an object of a non-abstract class derived from
2216 @code{protocolbuf}.
2217 @item
2218 @code{pname} is the transport protocol name which is either
2219 @code{protocol::tcp} or @code{protocol::udp}.
2220 @item
2221 @code{addr} is an unsigned long denoting the valid address of a machine in
2222 host byte order.
2223 @item
2224 @code{host} is a char string denoting the name of a machine like
2225 "kelvin.seas.virginia.edu".
2226 @item
2227 @code{portno} is an int and denotes the port number in host byte order.
2228 @end itemize
2229
2230 @table @code
2231
2232 @item protocol::protocolbuf::protocolbuf (pname)
2233 @findex protocolbuf::protocolbuf
2234 constructs @code{protocolbuf} object with the transport protocol set to
2235 @code{pname}.
2236
2237 @item p.protocol_name ()
2238 @findex protcolbuf::protocol_name
2239 returns the name of the transport protocol of @code{p} as a char string.
2240
2241 @item p.rfc_name ()
2242 @findex protocolbuf::rfc_name
2243 returns the name of the application protocol name of @code{p} as a char
2244 string. @code{protocolbuf::rfc_name ()} is a pure virtual function;
2245 thus, any class derived from @code{protocol::protocolbuf} should provide
2246 a definition for @code{protocolbuf::rfc_name ()}.
2247
2248 @item p.rfc_doc ()
2249 @findex protocolbuf::rfc_doc
2250 returns the RFC document name of the application protocol of
2251 @code{p} as a char string. @code{protocolbuf::rfc_doc ()} is a pure
2252 virtual function; thus, any class derived from
2253 @code{protocol::protocolbuf} should provide a definition for
2254 @code{protocolbuf::rfc_doc ()}.
2255
2256 @item p.serve_clients (portno)
2257 @findex protocolbuf::serve_clients
2258 converts @code{p} into a server. Use the port specified in
2259 @code{/etc/services} for the application if @code{portno} < 0. Use a
2260 default port if @code{0 <= portno <= 1024}. Otherwise, use @code{portno}
2261 as the port to accept clients requesting service.
2262 @code{protocolbuf::serve_clients()} is pure virtual function; thus,
2263 any class derived from @code{protocol::protocolbuf} should provide a
2264 definition for @code{protocolbuf::serve_clients()}.
2265
2266 Please do not change the meaning of @code{portno} when you derive
2267 your own class.
2268
2269 @item p.bind ()
2270 @findex protocolbuf::bind
2271 same as @code{p.serve_clients (-1)}.
2272 It returns 0 on success and returns the errno on failure.
2273
2274 @item p.connect ()
2275 @findex protocolbuf::connect
2276 connects to the local host's server for the application. @code{p} acts as
2277 the client. It returns 0 on success and returns the errno on failure.
2278
2279 @item p.connect (addr)
2280 connects to the server running at the machine with address, @code{addr}.
2281 @code{p} acts as the client.
2282 It returns 0 on success and returns the errno on failure.
2283
2284 @item p.connect (host)
2285 connects to the server running at the machine, @code{host}.
2286 @code{p} acts as the client.
2287 It returns 0 on success and returns the errno on failure.
2288
2289 @item p.connect (host, portno)
2290 connects to the server servicing clients at @code{portno} at the machine,
2291 @code{host}. Unlike this connect call, the other variants of connect
2292 uses the port specified in the @code{/etc/services} file.
2293 It returns 0 on success and returns the errno on failure.
2294
2295 @end table
2296
2297 @node echo Class
2298 @chapter Echo Class
2299 @cindex echo class
2300
2301 The @code{echo} class implements RFC 862. An @code{echo} object, as a
2302 client, will get back what ever data it sends to an @code{echo} server.
2303 Similarly, an @code{echo} object, as a server, will echo back the data
2304 it receives from its client.
2305
2306 The @code{echo} class is derived from @code{protocol} class, and uses
2307 @code{echo::echobuf} as its stream buffer. @code{echo::echobuf} is in
2308 turn is derived from @code{protocol::protcolbuf}.
2309
2310 In what follows,
2311 @itemize @minus
2312 @item
2313 @code{e} is a @code{echo} object.
2314 @item
2315 @code{pname} is a transport protocol name and must be either
2316 @code{protocol::tcp} or @code{protocol::udp}.
2317 @end itemize
2318
2319 @table @code
2320
2321 @item echo e (pname)
2322 @findex echo::echo
2323 constructs the @code{echo} object, @code{e} with @code{pname} as its
2324 transport protocol name.
2325
2326 @item echo::operator -> ()
2327 @findex echo::operator->
2328 an @code{echo} object is a smart pointer for the underlying
2329 @code{echobuf}.
2330
2331 @end table
2332
2333 @subsection tsecho.C
2334 @example
2335 // echo server. Serves clients at port 4000.
2336 #include <echo.h>
2337 #include <stdlib.h>
2338
2339 int main ()
2340 @{
2341 echo server (protocol::tcp);
2342 server->serve_clients (4000);
2343 return 1;
2344 @}
2345 @end example
2346
2347 @subsection tcecho.C
2348 @example
2349 // echo client. Sends "mary had a litte lamb" to the server
2350 #include <echo.h>
2351 #include <stdlib.h>
2352
2353 int main ()
2354 @{
2355 echo e(protocol::tcp);
2356
2357 e->connect ("kelvin.seas.virginia.edu", 4000);
2358
2359 cout << e->rfc_name () << ' ' << e->rfc_doc () << endl;
2360
2361 e << "mary had a little lamb\r\n" << flush;
2362
2363 char buf [256];
2364 e.getline (buf, 255);
2365
2366 cout << "got back: " << buf << endl;
2367 return 0;
2368 @}
2369
2370 @end example
2371
2372 @node smtp Class
2373 @chapter SMTP Class
2374 @cindex smtp class
2375
2376 The @code{smtp} class, which is derived from @code{protocol} class,
2377 implements RFC 821. It can be used only as a client. Server function
2378 is not yet implemented.
2379
2380 @code{smtp} uses @code{smtp::smtpbuf} as its underlying stream buffer.
2381 Also, like the @code{protocol} class, @code{smtp} is a smart pointer
2382 class for it is @code{smtp::smtpbuf}.
2383
2384 In what follows,
2385 @itemize @minus
2386 @item
2387 @code{s} is an @code{smtp} object.
2388 @item
2389 @code{sb} is an @code{smtp::smtpbuf} object.
2390 @item
2391 @code{io} is a pointer to an @code{ostream}.
2392 @item
2393 @code{buf} is a char buffer of length @code{buflen}.
2394 @item
2395 @code{str, str0, str1, ...} are all char strings.
2396 @end itemize
2397
2398 @table @code
2399
2400 @item smtp s (io)
2401 @findex smtp::smtp
2402 constructs an @code{smtp} client, @code{s}. Any response the client gets
2403 from the server is sent to the ostream, @code{io}.
2404
2405 @item sb.get_response ()
2406 @findex smtpbuf::get_response
2407 gets the server response and sends it to @code{io} of the @code{smtpbuf}.
2408
2409 @item sb.send_cmd (str0, str1, str2)
2410 @findex smtpbuf::send_cmd
2411 concatenates strings @code{str0}, @code{str1}, and @code{str2} and sends the
2412 concatenated string to the server before getting its response.
2413
2414 @item sb.send_buf (buf, buflen)
2415 @findex smtpbuf::send_buf
2416 sends the contents of the @code{buf} to the server.
2417
2418 @item sb.helo ()
2419 @item sb.help (str)
2420 @item sb.quit ()
2421 @item sb.turn ()
2422 @item sb.rset ()
2423 @item sb.noop ()
2424 @item sb.data ()
2425 @item sb.vrfy (str)
2426 @item sb.expn (str)
2427 implements the respective @var{smtp} commands. See RFC 821 for the
2428 meaning of each.
2429
2430 @item sb.mail (str)
2431 sends the mail command to the server. @code{str} is the the reverse path
2432 or the @var{FROM} address.
2433
2434 @item sb.rcpt (str)
2435 sends the recipient command to the server. @code{str} is the forward path
2436 or the @var{TO} address.
2437
2438 @item sb.data (buf, buflen)
2439 sends the contents of the buffer, @code{buf} as the mail data to the recipient
2440 previously established through @code{smtpbuf::rcpt()} calls.
2441
2442 @item sb.data (filename)
2443 sends the contents of the file, @code{filename} as the mail data to the
2444 recipient previously established through @code{smtpbuf::rcpt()} calls.
2445
2446 @end table
2447
2448 @subsection tcsmtp.C
2449 @example
2450 // smtp client.
2451 // The president sends a message to gs4t@@virginia.edu.
2452 #include <smtp.h>
2453 #include <stdio.h>
2454 #include <pwd.h>
2455 #include <unistd.h>
2456
2457 int main ()
2458 @{
2459 smtp client (&cout);
2460
2461 // establish connection
2462 client->connect ("fulton.seas.virginia.edu");
2463 client->helo ();
2464
2465 // get help
2466 client->help ();
2467
2468 // setup the FROM address
2469 client->mail ("president@@whitehouse.gov");
2470
2471 // setup the TO address
2472 client->rcpt ("gs4t@@virginia.edu");
2473
2474 // send the message
2475 client->data ();
2476 client << "Hi Sekar, I appoint you as the director of NASA\r\n" << flush;
2477 client << " -Bill, Hill, and Chel\r\n" << flush;
2478 cout << client; // get the server response.
2479
2480 // finally quit
2481 client->quit ();
2482
2483 return 0;
2484 @}
2485
2486 @end example
2487
2488 @node Error Handling
2489 @chapter Error Handling
2490 @cindex error handling
2491
2492 Each class in the Socket++ library uses @code{error(const char*)} member
2493 function to report any errors that may occur during a system call. It
2494 first calls @code{perror()} to report the error message for the
2495 @code{errno} set by the system call. It then calls
2496 @code{sock_error (const char* nm, const char* errmsg)}
2497 where @code{nm} is the name of the class.
2498
2499 The @code{sock_error()} function simply prints the @code{nm} and the
2500 @code{errmsg} on the @var{stderr}.
2501
2502 @node Pitfalls
2503 @chapter Pitfalls
2504 @cindex pitfalls
2505 @cindex common mistakes
2506
2507 Deadlocks in datagram sockets are the most common mistakes that novices
2508 make. To alleviate the problem, @code{sockbuf} class provides timeout
2509 facilities that can be used effectively to avoid deadlocks.
2510
2511 Consider the following simple tsmtp example which sends the HELP command to a
2512 smtp server and gets back the help message. Suppose it does not know
2513 the size of the help message nor the format of the message. In such
2514 cases, the timeout facilities of @code{sockbuf} class provides the
2515 required tools.
2516
2517 The example terminates the help message reception if the there is no
2518 input activity from the smtp server for 10 seconds.
2519
2520 @subheading tsmtp.cc
2521 @example
2522 @cindex timeout example
2523 #include <sockinet.h>
2524
2525 int main()
2526 @{
2527 iosockinet sio(sockbuf::sock_stream);
2528
2529 sio->connect("kelvin.seas.virginia.edu", "smtp", "tcp");
2530
2531 char buf[512];
2532 sio.getline(buf, 511); cout << buf << endl;
2533 sio << "HELO kelvin\n" << flush;
2534 sio.getline(buf, 511); cout << buf << endl;
2535
2536 sio << "HELP\n" << flush;
2537
2538 // set the receive timeout to 10 seconds
2539 int tmo = sio->recvtimeout(10);
2540
2541 while ( sio.getline(buf, 511) ) cout << buf << endl;
2542 // if the above while loop terminated due to timeout
2543 // clear the state of sio.
2544 if ( !sio->is_eof() )
2545 sio.clear();
2546 sio->recvtimeout(tmo); // reset the receive timeout time
2547
2548 sio << "QUIT\n" << flush;
2549 sio.getline(buf, 511); cout << buf << endl;
2550 return 0;
2551 @}
2552 @end example
2553
2554 @node Index
2555 @unnumbered Index
2556
2557 @printindex cp
2558
2559 @contents
2560 @bye
2561