⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socket++.texi

📁 《CODE READING》配套书源代码 《CODE READING》配套书源代码
💻 TEXI
📖 第 1 页 / 共 5 页
字号:
\input texinfo @c -*-texinfo-*-@c %**start of header@setfilename socket++.info@settitle   C++ socket classes@c %**end of header@syncodeindex fn cp@iftex@finalout@end iftex@ifinfoThis info file describes the C++ family of socket classes.Copyright (C) 1992,1993,1994 Gnanasekaran Swaminathan <gs4t@@virginia.edu>Permission is granted to make and distribute verbatim copies of thisdocument provided the copyright notice and this permission noticeare preserved on all copies.@end ifinfo@titlepage@title C++ Socket Classes@subtitle Version: 12Jan97 1.11@author Gnanasekaran Swaminathan@page@vskip 0pt plus 1filllCopyright @copyright{} 1992,1993,1994 Gnanasekaran Swaminathan@sp 2This is Version: 12Jan97 1.11 of the C++ family of socket classes.@sp 2Permission is granted to make and distribute verbatim copies of thisdocument provided the copyright notice and this permission noticeare preserved on all copies.@end titlepage@ifinfo@node    Top,       Copying, (dir),     (dir)@top Socket++ LibrarySocket++ is a family of C++ classes that gives the sameinterface as the iostream classes for input and outputfor communication between processes.@refillThis documentation describes Version: 12Jan97 1.11 ofsocket++ library.@refill@end ifinfo@menu* Copying::                     Copyright information.* Acknowledgments::             Thanks!* Overview of Socket++::        Overview of socket++ library.* sockbuf Class::               Socket streambuf class.* sockAddr Class::              Base class for socket addresses.* sockinetbuf Class::           Socket class for INET address family.* sockinetaddr Class::          Address class for INET address family of                                        sockets.* sockunixbuf Class::           Socket class for UNIX address family.* sockunixaddr Class::          Address class for UNIX address family of                                        sockets.* sockstream Classes::          I/O socket stream classes and some examples.* pipestream Classes::          I/O stream classes that provides pipe,                                        socketpair, and popen facilities.* Fork Class::                  Use the Fork class to fork a child                                        process.* protocol Class::              Protocol base class.* echo Class::                  Class implementing the Echo protocol.* smtp Class::                  Class implementing the SMTP protocol.* Error Handling::              Describes the default error handling in                                        the socket++ library.* Pitfalls::                    Common mistakes that socket++ library                                        users make.* Index::                       Index to concepts and program names@end menu@node Copying@unnumbered Socket++ Library Copyright Notice@cindex copyright notice@cindex CopyrightCopyright (C) 1992,1993,1994 Gnanasekaran SwaminathanPermission is granted to use at your own risk and distribute thissoftware in source and binary forms provided the above copyrightnotice and this paragraph are preserved on all copies. This softwareis provided "as is" with no express or implied warranty.@refill@node Acknowledgments@unnumbered Acknowledgments@cindex acknowledgmentsGordon Joly <G.Joly@@cs.ucl.ac.uk> for reporting bugs inpipestream class implementation and providing an ftp sitefor the socket++ library at	cs.ucl.ac.uk:~ftp/coside/gnu/socket++-1.x.tar.gzHe also knows how to make the socket++ library a sharedlibrary. Jim Anderson for reporting a bug in sockinet.CCarl Gay <cgay@@skinner.cs.uoregon.edu> for reporting a bugand a fix in sockinet.COliver Imbusch <flabes@@parystec.de> for reporting a bugin Makefile.in and suggesting several enhancements for sockbuf class.Dierk Wendt <wendt@@lambda.hella.de> for reporting errorsin the socket++ documentation.Per Bothner <bothner@@cygnus.com> for configure, config.sub,config.shared and move-if-change files that are usedto generate Makefile. These files are taken from his libg++-2.4and hence, these files are governed by the Copyright Notice foundin the file LICENCE in libg++.@node Overview of Socket++@chapter Overview of Socket++ Library@cindex overview of socket++Socket++ library defines a family of C++ classes that can be usedmore effectively than directly calling the underlying low-levelsystem functions. One distinct advantage of the socket++ is thatit has the same interface as that of the iostream so thatthe users can perform type-safe input output. See your localIOStream library documentation for more information on iostreams.@refill@code{streambuf} counterpart of the socket++ is @code{sockbuf}.@code{sockbuf} is an endpoint for communication with yet another@code{sockbuf} or simply a @code{socket} descriptor. @code{sockbuf}has also methods that act as interfaces for most of the commonlyused system calls that involve sockets. @xref{sockbuf Class}, for moreinformation on the socket buffer class.For each communication domain, we derive a new class from @code{sockbuf}that has some additional methods that are specific to that domain. Atpresent, only @var{unix} and @var{inet} domains are supported.@code{sockunixbuf} class and @code{sockinetbuf} class define the @var{unix}and @var{inet} domain of sockets respectively. @xref{sockunixbuf Class}, for@var{unix} sockets and @xref{sockinetbuf Class}, for @var{inet} sockets.We also have domain specific socket address classes that arederived from a common base class called @code{sockAddr}.@code{sockunixaddr} class is used for @var{unix} domain addressesand @code{sockinetaddr} class is used for @var{inet} domainaddresses. For more information on address classes see @ref{sockAddr Class},@ref{sockunixaddr Class}, and @ref{sockinetaddr Class}.@quotation@emph{Note}: @code{sockAddr} is not spelled @code{sockaddr} inorder to prevent name clash with the @code{struct sockaddr} declaredin @file{<sys/socket.h>}.@end quotationWe noted earlier that socket++ provides the same interface as theiostream library. For example, in the internet domain, we have@code{isockinet}, @code{osockinet}, and @code{iosockinet} classesthat are counterparts to @code{istream}, @code{ostream}, and@code{iostream} classes of IOStream library.For more details on @code{iosockstream} classes see @xref{sockstream Classes}.The services of @code{pipe()}, @code{socketpair()}, and @code{popen()}system calls are provided by the @code{pipestream} class.@xref{pipestream Classes}.@node sockbuf Class@chapter @code{sockbuf} Class@cindex sockbuf class@cindex class sockbuf@code{sockbuf} class is derived from @code{streambuf} class of theiostream library. You can simultaneously read and write into a@code{sockbuf} just like you can listen and talk through a telephone. Toaccomplish the above goal, we maintain two independent buffers forreading and writing.@menu* Constructors::              How to construct a @code{sockbuf} object                                     and how to open a socket?* Destructor::                How to destruct a @code{sockbuf} object                                     and how to close a socket?* Reading and Writing::       How to use @code{sockbuf} as @code{streambuf}?* Connection Establishment::  How to bind an address and establish a                                     connection?* Socket Options::            How to set and get socket options?* Timeouts::                  How to gracefully handle connection inactivity?@end menu@node Constructors@section Constructors@cindex sockbuf constructors@findex sockbuf::type@code{sockbuf} constructors sets up an endpoint for communication. A@code{sockbuf} object so created can be read from and written to inlinebuffered mode. To change mode, refer to @code{streambuf} classin your IOStream library.@quotation@cindex flushing buffers@emph{Note}: If you are using AT&T IOStream library, then thelinebuffered mode is permanently turned off. Thus, you need toexplicitly flush a socket stream. You can flush a socket stream bufferin one of the following four ways:@example    // os is a socket ostream    os << "this is a test" << endl;    os << "this is a test\n" << flush;    os << "this is a test\n"; os.flush ();    os << "this is a test\n"; os->sync ();@end example@end quotation@code{sockbuf} objects are created as follows where@itemize @minus@item@code{s} and @code{so} are @code{sockbuf} objects@item@code{sd} is an integer which is a socket descriptor@item@code{af} and @code{proto} are integers which denote domain number andprotocol number respectively@item@code{ty} is a @code{sockbuf::type} and must be one of@code{sockbuf::sock_stream}, @code{sockbuf::sock_dgram},@code{sockbuf::sock_raw}, @code{sockbuf::sock_rdm}, and@code{sockbuf::sock_seqpacket}@end itemize@table @code@item sockbuf s(sd);@itemx sockbuf s;@findex sockbuf::sockbufSet socket descriptor of @code{s} to @code{sd} (defaults to -1).@code{sockbuf} destructor will close @code{sd}.@item sockbuf s(af, ty, proto);Set socket descriptor of @code{s} to @code{::socket(af, int(ty), proto);}@item sockbuf so(s);Set socket descriptor of @code{so} to the socket descriptor of @code{s}.@item s.open(ty, proto)@findex sockbuf::opendoes nothing and returns simply @code{0}, the null pointerto @code{sockbuf}.@item s.is_open()@findex sockbuf::is_openreturns a non-zero number if the socket descriptor is open else return0.@item s = so;@findex sockbuf::operator=return a reference @code{s} after assigning @code{s} with @code{so}.@end table@node Destructor@section Destructor@cindex sockbuf destructor@findex sockbuf::shuthow@code{sockbuf::~sockbuf()} flushes output and closes its socket if no othersockbuf is referencing it and _S_DELETE_DONT_CLOSE flag is not set. Italso deletes its read and write buffers.In what follows,@itemize @minus@item@code{s} is a @code{sockbuf} object@item@code{how} is of type @code{sockbuf::shuthow} and must be one of@code{sockbuf::shut_read}, @code{sockbuf::shut_write}, and@code{sockbuf::shut_readwrite}@end itemize@table @code@item sockbuf::~sockbuf()@findex sockbuf::~sockbufflushes output and closes its socket if no other@code{sockbuf} object is referencing it before deleting its read andwrite buffers. If the _S_DELETE_DONT_CLOSE flag is set, then the socketis not closed.@item s.close()@findex sockbuf::closecloses the socket even if it is referenced by other @code{sockbuf}objects and _S_DELETE_DONT_CLOSE flag is set.@item s.shutdown(how)@findex sockbuf::shutdownshuts down read if @code{how} is @code{sockbuf::shut_read}, shuts downwrite if @code{how} is @code{sockbuf::shut_write}, and shuts down bothread and write if @code{how} is @code{sockbuf::shut_readwrite}.@end table@node Reading and Writing@section Reading and Writing@cindex sockbuf reading@cindex sockbuf writing@code{sockbuf} class offers several ways to read and write and tailorsthe behavior of several virtual functions of @code{streambuf} for socketcommunication.In case of error, @code{sockbuf::error(const char*)} is called.In what follows, @itemize @minus@item@code{s} is a @code{sockbuf} object@item@code{buf} is buffer of type @code{char*}@item@code{bufsz} is an integer and is less than @code{sizeof(buf)}@item@code{msgf} is an integer and denotes the message flag@item@code{sa} is of type @code{sockAddr}@item@code{msgh} is a pointer to @code{struct msghdr}@item@code{wp} is an integer and denotes time in seconds@item@code{c} is a char@end itemize@table @code@item s.is_open()@findex sockbuf::is_openreturns a non-zero number if the socket descriptor is open else return0.@item s.is_eof()@findex sockbuf::is_eofreturns a non-zero number if the socket has seen EOF while reading elsereturn 0.@item s.write(buf, bufsz)@findex sockbuf::writereturns an int which must be equal to @code{bufsz} if @code{bufsz} chars inthe @code{buf} are written successfully. It returns 0 if there isnothing to write or if, in case of timeouts, the socket is not readyfor write @ref{Timeouts}.@item s.send(buf, bufsz, msgf)@findex sockbuf::send@findex sockbuf::msgflagsame as @code{sockbuf::write} described above but allows the user tocontrol the transmission of messages using the message flag @code{msgf}.If @code{msgf} is @code{sockbuf::msg_oob} and the socket type of@code{s} is @code{sockbuf::sock_stream}, @code{s} sends the message in@var{out-of-band} mode. If @code{msgf} is @code{sockbuf::msg_dontroute},@code{s} sends the outgoing packets without routing. If @code{msgf} is0, which is the default case, @code{sockbuf::send} behaves exactly like@code{sockbuf::write}.@item s.sendto(sa, buf, bufsz, msgf)@findex sockbuf::sendtosame as @code{sockbuf::send} but works on unconnected sockets. @code{sa}specifies the @var{to} address for the message.@item s.sendmsg(msgh, msgf)@findex sockbuf::sendmsgsame as @code{sockbuf::send} but sends a @code{struct msghdr} objectinstead.@item s.sys_write(buf, bufsz)@findex sockbuf::sys_writecalls @code{sockbuf::write} and returns the result. Unlike@code{sockbuf::write} @code{sockbuf::sys_write} is declared as a virtualfunction.@item s.read(buf, bufsz)@findex sockbuf::readreturns an int which is the number of chars read into the @code{buf}. Incase of EOF, return EOF. Here, @code{bufsz} indicates the size of the@code{buf}. In case of timeouts, return 0 @ref{Timeouts}.@item s.recv(buf, bufsz, msgf)@findex sockbuf::recv@findex sockbuf::msgflagsame as @code{sockbuf::read} described above but allows the user to receive@var{out-of-band} data if @code{msgf} is @code{sockbuf::msg_oob} or topreview the data waiting to be read if @code{msgf} is@code{sockbuf::msg_peek}. If @code{msgf} is 0, which is the defaultcase, @code{sockbuf::recv} behaves exactly like @code{sockbuf::read}.@item s.recvfrom(sa, buf, bufsz, msgf)@findex sockbuf::recvfromsame as @code{sockbuf::recv} but works on unconnected sockets. @code{sa}specifies the @var{from} address for the message.@item s.recvmsg(msgh, msgf)@findex sockbuf::recvmsgsame as @code{sockbuf::recv} but reads a @code{struct msghdr} objectinstead.@item s.sys_read(buf, bufsz)@findex sockbuf::sys_readcalls @code{sockbuf::read} and returns the result. Unlike@code{sockbuf::read} @code{sockbuf::sys_read} is declared as a virtual

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -