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

📄 socket++.texi

📁 《CODE READING》配套书源代码 《CODE READING》配套书源代码
💻 TEXI
📖 第 1 页 / 共 5 页
字号:
@node sockinetbuf Class@chapter sockinetbuf Class@cindex sockinetbuf class@cindex inet domain@code{sockinetbuf} class is derived from @code{sockbuf} class and inheritsmost of the public functions of @code{sockbuf}. @xref{sockbuf Class},for more information on @code{sockbuf}. In addition, it provides methodsfor getting @code{sockinetaddr} of local and peer connections.@xref{sockinetaddr Class}, for more information on @code{sockinetaddr}.@menu* Methods sockinetbuf::         Describes sockinetbuf member functions.* Datagram INET::               A pair of example programs demonstrating                                    datagram connection in inet domain.* Stream INET::                 A pair of example programs demonstrating                                    stream connection in inet domain.@end menu@node Methods sockinetbuf@section MethodsIn what follows,@itemize @minus@item@code{ty} denotes the type of the socket connection and is of type@code{sockbuf::type}@item@code{proto} denotes the protocol and is of type int@item@code{si, ins} are @code{sockbuf} objects and are in @var{inet} domain@item@code{adr} denotes an @var{inet} address in host byte order and is oftype unsigned long@item@code{serv} denotes a service like "nntp" and is of type char*@item@code{proto} denotes a protocol like "tcp" and is of type char*@item@code{thostname} is of type char* and denotes the name of a host like@code{"kelvin.acc.virginia.edu"} or @code{"128.143.24.31"}.@item@code{portno} denotes a port in host byte order and is of type int@end itemize@table @code@item sockinetbuf ins(ty, proto)@findex sockinetbuf::sockinetbufConstructs a @code{sockinetbuf} object @code{ins} whose socketcommunication type is @code{ty} and protocol is @code{proto}.@code{proto} defaults to 0. @item sockinetbuf ins(si)Constructs a @code{sockinetbuf} object @code{ins} which uses the samesocket as @code{si} uses.@item ins = si@findex sockinetbuf::operator =performs the same function as @code{sockbuf::operator=}.@xref{sockbuf Class}, for more details.@item ins.open(ty, proto)@findex sockinetbuf::opencreate a new @code{sockinetbuf} whose type and protocol are@code{ty} and @code{proto} respectively and assign it to @code{ins}.@item sockinetaddr sina = ins.localaddr()@findex sockinetbuf::localaddr@findex getsockname (see sockinetbuf::localaddr)returns the local @var{inet} address of the @code{sockinetbuf} object@code{ins}. The call will make sense only after a call to either@code{sockbuf::bind} or @code{sockbuf::connect}.@item sockinetaddr sina = ins.peeraddr()@findex sockinetbuf::peeraddr@findex getpeername (see sockinetbuf::peeraddr)returns the peer @var{inet} address of the @code{sockinetbuf} object@code{ins}. The call will make sense only after a call to@code{sockbuf::connect}.@item const char* hn = ins.localhost()@findex sockinetbuf::localhostreturns the local @var{inet} thostname of the @code{sockinetbuf} object@code{ins}. The call will make sense only after a call to either@code{sockbuf::bind} or @code{sockbuf::connect}.@item const char* hn = ins.peerhost()@findex sockinetbuf::peerhostreturns the peer @var{inet} thostname of the @code{sockinetbuf} object@code{ins}. The call will make sense only after a call to@code{sockbuf::connect}.@item int pn = ins.localport()@findex sockinetbuf::localportreturns the local @var{inet} port number of the @code{sockinetbuf} object@code{ins} in host byte order. The call will make sense only after acall to either @code{sockbuf::bind} or @code{sockbuf::connect}.@item int pn = ins.peerport()@findex sockinetbuf::peerportreturns the peer @var{inet} port number of the @code{sockinetbuf} object@code{ins} in local host byte order. The call will make sense only after acall to @code{sockbuf::connect}.@item ins.bind ()@findex sockinetbuf::bindbinds @code{ins} to the default address @var{INADDR_ANY} and the defaultport. It returns 0 on success and returns the errno on failure.@item ins.bind (adr, portno)binds @code{ins} to the address @code{adr} and the port @code{portno}.It returns 0 on success and returns the errno on failure.@item ins.bind (adr, serv, proto)binds @code{ins} to the address, @code{adr} and the port corresponding tothe service @code{serv} and the protocol @code{proto}>.It returns 0 on success and returns the errno on failure.@item ins.bind (thostname, portno)binds @code{ins} to the address corresponding to the hostname@code{thostname} and the port @code{portno}.It returns 0 on success and returns the errno on failure.@item ins.bind (thostname, serv, proto)binds @code{ins} to the address corresponding to the hostname@code{thostname} and the port corresponding to the service @code{serv}and the protocol @code{proto}>. It returns 0 on success andreturns the errno on failure.@item ins.connect (adr, portno)@findex sockinetbuf::connectconnects @code{ins} to the address @code{adr} and the port @code{portno}.It returns 0 on success and returns the errno on failure.@item ins.connect (adr, serv, proto)connects @code{ins} to the address, @code{adr} and the port corresponding tothe service @code{serv} and the protocol @code{proto}>.It returns 0 on success and returns the errno on failure.@item ins.connect (thostname, portno)connects @code{ins} to the address corresponding to the hostname@code{thostname} and the port @code{portno}.It returns 0 on success and returns the errno on failure.@item ins.connect (thostname, serv, proto)connects @code{ins} to the address corresponding to the hostname@code{thostname} and the port corresponding to the service @code{serv}and the protocol @code{proto}>.It returns 0 on success and returns the errno on failure.@end table@node Datagram INET@section @var{inet} Datagram Sockets@cindex sockinetbuf dgram example@cindex datagram inet@cindex isockinet example@cindex osockinet exampleThe following two programs illustrates how to use @code{sockinetbuf} classfor datagram connection in @var{inet} domain. @code{tdinread.cc} alsoshows how to use @code{isockinet} class and @code{tdinwrite.cc} showshow to use @code{osockinet} class.@subheading tdinread.cc@example// reads data sent by tdinwrite.cc#include <sockinet.h>int main(int ac, char** av)@{        isockinet  is (sockbuf::sock_dgram);        is->bind();        cout << "localhost = " << so.localhost() << endl             << "localport = " << so.localport() << endl;        char         buf[256];        int          n;        is >> n;        cout << av[0] << ": ";        while(n--) @{                is >> buf;                cout << buf << ' ';        @}        cout << endl;        return 0;@}@end example@subheading tdinwrite.cc@example// sends data to tdinread.cc#include <sockinetbuf.h>#include <stdlib.h>int main(int ac, char** av)@{        if (ac < 3) @{                cerr << "USAGE: " << av[0] << " thostname port-number "                     << "data ... " << endl;                return 1;        @}        osockinet os (sockbuf::sock_dgram);        os->connect (av[1], atoi(av[2]));        cout << "local: " << so.localport() << ' '                          << so.localhost() << endl             << "peer:  " << so.peerport() << ' '                          << so.peerhost() << endl;        os << ac-3; av += 3;        while(*av) os << *av++ << ' ';        os << endl;        return 0;        @}@end example@node Stream INET@section @var{inet} Stream Sockets@cindex stream inet@cindex sockinetbuf stream example@cindex iosockinet exampleThe following two programs illustrates the use of @code{sockinetbuf} classfor stream connection in @var{inet} domain. It also shows how to use@code{iosockinet} class.@subheading tsinread.cc@example// receives strings from tsinwrite.cc and sends the strlen// of each string back to tsinwrite.cc#include        <sockinet.h>int main()@{        sockinetbuf     si(sockbuf::sock_stream);        si.bind();        cout << si.localhost() << ' ' << si.localport() << endl;        si.listen();        iosockinet s = si.accept();        char          buf[1024];        while (s >> buf) @{                cout << buf << ' ';                s << ::strlen(buf) << endl;        @}        cout << endl;                return 0;@}@end example@subheading tsinwrite.cc@example// sends strings to tsinread.cc and gets back their length// usage: tsinwrite hostname portno//        see the output of tsinread for what hostname and portno to use#include        <sockinet.h>#include        <stdlib.h>int main(int ac, char** av)@{        iosockinet sio (sockbuf::sock_stream);        sio->connect (av[1], atoi (av[2]));        sio << "Hello! This is a test\n" << flush;        // terminate the while loop in tsinread.cc        si.shutdown(sockbuf::shut_write);        int len;        while (s >> len) cout << len << ' ';        cout << endl;        return 0;@}@end example@node sockinetaddr Class@chapter sockinetaddr Class@cindex sockinetaddr class@cindex inet address classClass @code{sockinetaddr} is derived from @code{sockAddr} declared in@code{<sockstream.h>} and from @code{sockaddr_in} declared in@code{<netinet/in.h>}. Always use a @code{sockinetaddr} object for anaddress with @var{inet} domain of sockets. @xref{ConnectionEstablishment}.In what follows,@itemize @minus@item@code{adr} denotes an @var{inet} address in host byte order and is oftype unsigned long@item@code{serv} denotes a service like "nntp" and is of type char*@item@code{proto} denotes a protocol like "tcp" and is of type char*@item@code{thostname} is of type char* and denotes the name of a host like@code{"kelvin.acc.virginia.edu"} or @code{"128.143.24.31"}.@item@code{portno} denotes a port in host byte order and is of type int@end itemize@table @code@item sockinetaddr sina@findex sockinetaddr::sockinetaddrConstructs a @code{sockinetaddr} object @code{sina} with default address@var{INADDR_ANY} and default port number 0.@item sockinetaddr sina(adr, portno)Constructs a @code{sockinetaddr} object @code{sina} setting inet addressto @code{adr} and the port number to @code{portno}. @code{portno}defaults to 0.@item sockinetaddr sina(adr, serv, proto)Constructs a @code{sockinetaddr} object @code{sina} setting inet addressto @code{adr} and the port number corresponding to the service@code{serv} and the protocol @code{proto}. The protocol defaults to "tcp".@item sockinetaddr sina(thostname, portno)Constructs a @code{sockinetaddr} object @code{sina} setting inet addressto the address of @code{thostname} and the port number to @code{portno}.@code{portno} defaults to 0.@item sockinetaddr sina(thostname, serv, proto)Constructs a @code{sockinetaddr} object @code{sina} setting inet addressto the address of @code{thostname} and the port number corresponding tothe service @code{serv} and the protocol @code{proto}. The protocoldefaults to "tcp".@item void* a = sina@findex sockinetaddr::operator void*returns the address of the @code{sockaddr_in} part of@code{sockinetaddr} object @code{sina} as void*.@item int sz = sina.size()@findex sockinetaddr::sizereturns the sizeof @code{sockaddr_in} part of @code{sockinetaddr} object@code{sina}.@item int af = sina.family()@findex sockinetaddr::familyreturns @code{sockinetbuf::af_inet} if all is well.@item int pn = sina.getport()@findex sockinetaddr::getportreturns the port number of the @code{sockinetaddr} object @code{sina} inhost byte order.@item const char* hn = getthostname()@findex sockinetaddr::getthostnamereturns the host name of the @code{sockinetaddr} object @code{sina}.@end table@node sockunixbuf Class@chapter sockunixbuf Class@cindex sockunixbuf class@cindex unix domain@code{sockunixbuf} class is derived from @code{sockbuf} class declared in@code{<sockstream.h>} and hence, inherits most of the public memberfunctions of @code{sockbuf}. @xref{sockbuf Class}, for more informationon @code{sockbuf}. @menu* Methods sockunixbuf::         Describes sockunixbuf member functions* Datagram UNIX::               A pair of example programs demonstrating                                    datagram connection in @var{unix} domain* Stream UNIX::                 A pair of example programs demonstrating                                    stream connection in @var{unix} domain@end menu@node Methods sockunixbuf@section MethodsIn what follows,@itemize @minus@item@code{ty} denotes the socket type and is of type @code{sockbuf::type}@item@code{proto} denotes the protocol number and is of type int@item@code{su} is a @code{sockbuf} and must be in @var{unix} domain@item@code{path} is the @var{unix} path name like "/tmp/unix_socket"@end itemize@table @code@item sockunixbuf uns(ty, proto)@findex sockunixbuf::sockunixbufConstructs a @code{sockunixbuf} object @code{uns} with @code{ty} as itstype and @code{proto} as its protocol number. @code{proto} defaults to0.@item sockunixbuf uns = suConstructs a @code{sockunixbuf} object @code{uns} which uses the samesocket as is used by @code{su}.

⌨️ 快捷键说明

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