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

📄 socket++.texi

📁 《CODE READING》配套书源代码 《CODE READING》配套书源代码
💻 TEXI
📖 第 1 页 / 共 5 页
字号:
function.@item s.is_readready(wp_sec, wp_usec)@findex sockbuf::is_readreadyreturns a non-zero int if @code{s} has data waiting to be read from thecommunication channel. If @code{wp_sec >= 0}, it waits for@code{wp_sec 10^6 + wp_usec} microseconds before returning 0 in casethere are no data waiting to be read. If @code{wp_sec < 0}, then it waits untila datum arrives at the communication channel. @code{wp_usec} defaults to 0.@quotation@emph{Please Note}: The data waiting in @code{sockbuf}'s own buffer isdifferent from the data waiting in the communication channel.@end quotation@item s.is_writeready(wp_sec, wp_usec)@findex sockbuf::is_writereadyreturns a non-zero int if data can be written onto the communicationchannel of @code{s}. If @code{wp_sec >= 0}, it waits for@code{wp_sec 10^6 + wp_usec} microseconds before returning 0 in case nodata can be written. If @code{wp_sec < 0}, then it waits untilthe communication channel is ready to accept data. @code{wp_usec}defaults to 0.@quotation@emph{Please Note}: The buffer of the @code{sockbuf} class is differentfrom the buffer of the communication channel buffer.@end quotation@item s.is_exceptionpending(wp_sec, wp_usec)@findex sockbuf::is_exceptionpendingreturns non-zero int if @code{s} has any exception events pending.If @code{wp_sec >= 0}, it waits for @code{wp_sec 10^6 + wp_usec}microseconds before returning 0 in case @code{s} doesnot have any exception events pending. If @code{wp_sec < 0},then it waits until an expception event occurs. @code{wp_usec} defaultsto 0.@quotation@emph{Please Note}: The exceptions that@code{sockbuf::is_exceptionpending} is looking for are different fromthe C++ exceptions.@end quotation@item s.flush_output()@findex sockbuf::flush_outputflushes the output buffer and returns the number of chars flushed.In case of error, return EOF. @code{sockbuf::flush_output} is aprotected member function and it is not available for general public.@item s.doallocate()@findex sockbuf::doallocateallocates free store for read and write buffers of @code{s} and returns1 if allocation is done and returns 0 if there is no need.@code{sockbuf::doallocate} is a protected virtual member function and itis not available for general public.@item s.underflow()@findex sockbuf::underflowreturns the unread char in the buffer as an unsigned char if there isany. Else returns EOF if @code{s} cannot allocate space for the buffers,cannot read or peer is closed. @code{sockbuf::underflow} is a protectedvirtual member function and it is not available for general public.@item s.overflow(c)@findex sockbuf::overflowif @code{c==EOF}, call and return the result of @code{flush_output()},else if @code{c=='\n'} and @code{s} is linebuffered, call@code{flush_output()} and return @code{c} unless @code{flush_output()}returns EOF, in which case return EOF. In any other case, insert char@code{c} into the buffer and return @code{c} as an unsigned char.@code{sockbuf::overflow} is a protected member virtual function and it is notavailable for general public.@quotation@emph{Node:} linebuffered mode does not work with AT&T IOStream library.Use explicit flushing to flush @code{sockbuf}.@end quotation@item s.sync()@findex sockbuf::sync@cindex flushing outputcalls @code{flush_output()} and returns the result. Useful if the user needsto flush the output without writing newline char into the write buffer.@item s.xsputn(buf, bufsz)@findex sockbuf::xsputnwrite @code{bufsz} chars into the buffer and returns the number of charssuccessfully written. Output is flushed if any char in@code{buf[0..bufsz-1]} is @code{'\n'}.@item s.recvtimeout(wp)@findex sockbuf::recvtimeoutsets the recv timeout to @code{wp} seconds. If @code{wp} is -1, it is ablock and if @code{wp} is 0, it is a poll.It affects all read functions. If the socket is not read ready within@code{wp} seconds, the read call will return 0. It also affects@code{sockbuf::underflow}. @code{sockbuf::underflow} will not set the@code{_S_EOF_SEEN} flag if it is returning EOF because of timeout.@code{sockbuf::recvtimeout} returns the old recv timeout value.@item s.sendtimeout(wp)@findex sockbuf::sendtimeoutsets the send timeout to @code{wp} seconds. If @code{wp} is -1, it is ablock and if @code{wp} is 0, it is a poll.It affects all write functions. If the socket is not write ready within@code{wp} seconds, the write call will return 0. @code{sockbuf::sendtimeout} returns the old send timeout value.@end table@node Connection Establishment@section Establishing connections@cindex connection establishment@cindex namesA name must be bound to a @code{sockbuf} if processes want to refer toit and use it for communication. Names must be unique. A @var{unix} nameis a 3-tuple, @var{<protocol, local path, peer path>}. An @var{inet} nameis a 5-tuple, @var{<protocol, local addr, local port, peer addr, peerport>}. @code{sockbuf::bind} is used to specify the local half of thename---@var{<local path>} for @var{unix} and @var{<local addr, localport>} for @var{inet}. @code{sockbuf::connect} and@code{sockbuf::accept} are used to specify the peer half of thename---@var{<peer path>} for @var{unix} and @var{<peer addr, peer port>}for @var{inet}.In what follows,@itemize @minus@item@code{s} and @code{so} are @code{sockbuf} objects@item@code{sa} is a @code{sockAddr} object@item@code{nc} is an integer denoting the number of connections to allow@end itemize@table @code@item s.bind(sa)@findex sockbuf::bind@cindex binding addressesbinds @code{sockAddr} @code{sa} as the local half of the name for@code{s}. It returns 0 on success and returns the errno on failure.@item s.connect(sa)@findex sockbuf::connect@cindex connect@code{sockbuf::connect} uses @code{sa} to provide the peer half of thename for @code{s} and to establish the connection itself.@code{sockbuf::connect} also provides the local half of the nameautomatically and hence, the user should not use @code{sockbuf::bind} tobind any local half of the name. It returns 0 on success and returnsthe errno on failure.@item s.listen(nc)@findex sockbuf::listen@cindex listeningmakes @code{s} ready to accept connections. @code{nc} specifies themaximum number of outstanding connections that may be queued and must beat least 1 and less than or equal to @code{sockbuf::somaxconn} whichis usually 5 on most systems.@item sockbuf so = s.accept(sa)@itemx sockbuf so = s.accept()@findex sockbuf::accept@cindex accepting connectionsaccepts connections and returns the peer address in @code{sa}. @code{s}must be a listening @code{sockbuf}. See @code{sockbuf::listen} above.@end table@node Socket Options@section Getting and Setting Socket Options@cindex socket options@cindex option setting@cindex option gettingSocket options are used to control a socket communication. New optionscan be set and old value of the options can be retrived at the protocollevel or at the socket level by using @code{setopt} and@code{getopt} member functions. In addition, you can also use specialmember functions to get and set specific options.In what follows,@itemize @minus@item@code{s} is a @code{sockbuf} object@item@code{opval} is an integer and denotes the option value@item@code{op} is of type @code{sockbuf::option} and must be one of@itemize @bullet@item@code{sockbuf::so_error} used to retrieve and clear error status@item@code{sockbuf::so_type} used to retrieve type of the socket@item@code{sockbuf::so_debug} is used to specify recording of debugginginformation@item@code{sockbuf::so_reuseaddr} is used to specify the reuse of local address@item @code{sockbuf::so_keepalive} is used to specify whether to keep connectionsalive or not@item@code{sockbuf::so_dontroute} is used to specify whether to route messagesor not@item@code{sockbuf::so_broadcast} is used to specify whether to broadcast@code{sockbuf::sock_dgram} messages or not@item@code{sockbuf::so_oobinline} is used to specify whether to inline@var{out-of-band} data or not.@item@code{sockbuf::so_linger} is used to specify for how long to linger beforeshutting down@item@code{sockbuf::so_sndbuf} is used to retrieve and to set the size of thesend buffer (communication channel buffer not @code{sockbuf}'s internalbuffer)@item@code{sockbuf::so_rcvbuf} is used to retrieve and to set the size of therecv buffer (communication channel buffer not @code{sockbuf}'s internalbuffer)@end itemize@end itemize@table @code@item s.getopt(op, &opval, sizeof(opval), oplevel)@findex sockbuf::getopt@cindex getsockoptgets the option value of the @code{sockbuf::option} @code{op}  at theoption level @code{oplevel} in @code{opval}. It returns the actual sizeof the buffer @code{opval} used. The default value of the @code{oplevel}is @code{sockbuf::sol_socket}.@item s.setopt(op, &opval, sizeof(opval), oplevel)@findex sockbuf::setopt@cindex setsockoptsets the option value of the @code{sockbuf::option} @code{op}  at theoption level @code{oplevel} to @code{opval}. The default value of the@code{oplevel} is @code{sockbuf::sol_socket}.@item s.gettype()@findex sockbuf::gettypegets the socket type of @code{s}. The return type is@code{sockbuf::type}.@item s.clearerror()@findex sockbuf::clearerrorgets and clears the error status of the socket.@item s.debug(opval)@findex sockbuf::debugif @code{opval} is not -1, set the @code{sockbuf::so_debug} option valueto @code{opval}. In any case, return the old option value of@code{sockbuf::so_debug} option. The default value of @code{opval} is-1.@item s.reuseaddr(opval)@findex sockbuf::reuseaddrif @code{opval} is not -1, set the @code{sockbuf::so_reuseaddr} option valueto @code{opval}. In any case, return the old option value of@code{sockbuf::so_reuseaddr} option. The default value of @code{opval}is -1.@item s.dontroute(opval)@findex sockbuf::dontrouteif @code{opval} is not -1, set the @code{sockbuf::so_dontroute} option valueto @code{opval}. In any case, return the old option value of@code{sockbuf::so_dontroute} option. The default value of @code{opval}is -1.@item s.oobinline(opval)@findex sockbuf::oobinlineif @code{opval} is not -1, set the @code{sockbuf::so_oobinline} option valueto @code{opval}. In any case, return the old option value of@code{sockbuf::so_oobinline} option. The default value of @code{opval}is -1.@item s.broadcast(opval)@findex sockbuf::broadcastif @code{opval} is not -1, set the @code{sockbuf::so_broadcast} option valueto @code{opval}. In any case, return the old option value of@code{sockbuf::so_broadcast} option. The default value of @code{opval}is -1.@item s.keepalive(opval)@findex sockbuf::keepaliveif @code{opval} is not -1, set the @code{sockbuf::so_keepalive} option valueto @code{opval}. In any case, return the old option value of@code{sockbuf::so_keepalive} option. The default value of @code{opval}is -1.@item s.sendbufsz(opval)@findex sockbuf::sndbufif @code{opval} is not -1, set the new send buffer size to @code{opval}.In any case, return the old buffer size of the send buffer. The defaultvalue of @code{opval} is -1.@item s.recvbufsz(opval)@findex sockbuf::rcvbufif @code{opval} is not -1, set the new recv buffer size to @code{opval}.In any case, return the old buffer size of the recv buffer. The defaultvalue of @code{opval} is -1.@item s.linger(tim)@findex sockbuf::lingerif @code{tim} is positive, set the linger time to tim seconds.If @code{tim} is 0, set the linger off.In any case, return the old linger time if it was set earlier.Otherwise return -1. The default value of @code{tim} is -1.@end table@node Timeouts@section Time Outs While Reading and Writing@cindex timeouts@cindex read timeouts@cindex write timeoutsTime outs are very useful in handling data of unknown sizesand formats while reading and writing. For example, how doesone communicate with a socket that sends chunks of dataof unknown size and format? If only @code{sockbuf::read} is usedwithout time out, it will block indefinitely.In such cases, time out facility is the only answer.The following idiom is recommended. @xref{Pitfalls} for a completeexample.@example    int old_tmo = s.recvtimeout (2) // set time out (2 seconds here)    for (;;) @{ // read or write        char buf[256];        int rval = s.read (buf, 256);        if (rval == 0 || rval == EOF) break;        // process buf here    @}    s.recvtimeout (old_tmo); // reset time out@end example        In what follows,@itemize @minus@item@code{s} is a @code{sockbuf} object@item@code{wp} is waiting period in seconds@end itemize@table @code@item s.recvtimeout(wp)@findex sockbuf::recvtimeoutsets the recv timeout to @code{wp} seconds. If @code{wp} is -1, it is ablock and if @code{wp} is 0, it is a poll.It affects all read functions. If the socket is not read ready within@code{wp} seconds, the read call will return 0. It also affects@code{sockbuf::underflow}. @code{sockbuf::underflow} will not set the@code{_S_EOF_SEEN} flag if it is returning EOF because of timeout.@code{sockbuf::recvtimeout} returns the old recv timeout value.@item s.sendtimeout(wp)@findex sockbuf::sendtimeoutsets the send timeout to @code{wp} seconds. If @code{wp} is -1, it is ablock and if @code{wp} is 0, it is a poll.It affects all write functions. If the socket is not write ready within@code{wp} seconds, the write call will return 0. @code{sockbuf::sendtimeout} returns the old send timeout value.@end table@node sockAddr Class@chapter sockAddr Class@cindex sockAddr class@cindex base address classClass @code{sockAddr} is an abstract base class for all socket addressclasses. That is, domain specific socket address classes are all derivedfrom @code{sockAddr} class. @quotation@emph{Note}: @code{sockAddr} is not spelled @code{sockaddr} inorder to prevent name clash with @code{struct sockaddr} declaredin @file{<sys/socket.h>}.@end quotationNon-abstract derived classes must have definitions for the followingfunctions.@table @code@item sockAddr::operator void* ()@findex sockAddr::operator void*should simply return @code{this}.@item sockAddr::size()@findex sockAddr::sizeshould return @code{sizeof(*this)}. The return type is @code{int}.@item sockAddr::family()@findex sockAddr::familyshould return address family (domain name) of the socket address. Thereturn type is @code{int}@end table

⌨️ 快捷键说明

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