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

📄 socket++.texi

📁 《CODE READING》配套书源代码 《CODE READING》配套书源代码
💻 TEXI
📖 第 1 页 / 共 5 页
字号:
@item uns = su@findex sockunixbuf::operator =@code{sockunixbuf} object @code{uns} closes its current socket if no other@code{sockbuf} is referring to it and uses the socket that @code{sockbuf}object @code{su} is using.@item uns.open(ty, proto)@findex sockunixbuf::opencreate a @code{sockunixbuf} object with @code{ty} as its type and@code{proto} as its protocol and assign the @code{sockunixbuf} object socreated to @code{*this}. It returns @code{this}. @code{proto} defaultsto 0.@item uns.bind(path)@findex sockunixbuf::bindbinds @code{uns} to the @var{unix} pathname @code{path}.It returns 0 on success and returns the errno on failure.@item uns.connect(path)@findex sockunixbuf::connectconnects @code{uns} to the @var{unix} pathname @code{path}.It returns 0 on success and returns the errno on failure.@end table@node Datagram UNIX@section @var{unix} Datagram Sockets@cindex datagram unix@cindex isockunix example@cindex osockunix exampleThe following two programs illustrates how to use @code{sockunixbuf} classfor datagram connection in @var{unix} domain. @code{tdunread.cc} alsoshows how to use @code{isockunix} class and @code{tdunwrite.cc} showshow to use @code{osockunix} class.@subheading tdunread.cc@example// reads data sent by tdunwrite.cc#include <sockunix.h>#include <unistd.h>#include <errno.h>int main(int ac, char** av)@{        if (ac != 2) @{                cerr << "USAGE: " << av[0] << " socket_path_name\n";                return 1;        @}                // isockunix builds the sockunixbuf object        isockunix 	su (sockbuf::sock_dgram);            su->bind(av[1]);        cout << "Socket name = " << av[1] << endl;        if (chmod(av[1], 0777) == -1) @{                perror("chmod");                return 1;        @}        char buf[1024];        int i;        su >> i;        cout << av[0] << ": " << i << " strings: ";        while (i--) @{                su >> buf;                cout  << buf << ' ';        @}        cout << endl;                unlink(av[1]);        return 0;@}@end example@subheading tdunwrite.cc@example// sends data to tdunread.cc#include <sockunix.h>int main(int ac, char** av)@{        if (ac < 2) @{                cerr << "USAGE: " << av[0]                     << " socket_path_name data...\n";                return 1;        @}        osockunix	su (sockbuf::sock_dgram);	su->connect (av[1]);        su << ac << ' ';        while (*av) @{ su << av[i] << ' '; av++; @}        su << endl;        return 0;@}@end example@node Stream UNIX@section @var{unix} Stream Sockets@cindex stream unix@cindex sockunixbuf example@cindex iosockunix exampleThe following two programs illustrates how to use @code{sockunixbuf} classfor stream connection in @var{unix} domain. It also shows how to use@code{iosockunix} class.@subheading tsunread.cc@example// exchanges char strings with tsunwrite.cc#include <sockunix.h>#include <unistd.h>#include <errno.h>int main(int ac, char** av)@{        if (ac != 2) @{                cerr << "USAGE: " << av[0] << " socket_path_name\n";                return 1;        @}                sockunixbuf  su(sockbuf::sock_stream);        su.bind(av [1]);        cout << "Socket name = " << av[1] << endl;        if (chmod(av[1], 0777) == -1) @{                perror("chmod");                return 1;        @}        su.listen(3);                iosockunix ioput = su.accept ();        char       buf[1024];                ioput << av[0] << ' ' << av[1] << endl;        while ( ioput >> buf ) cout << av[0] << ": " << buf << endl;        unlink(av[1]);        return 0;@}@end example@subheading tsunwrite.cc@example// exchanges char strings with tsunread.cc#include <sockunix.h>int main(int ac, char** av)@{        if (ac < 2) @{                cerr << "USAGE: " << av[0]                     << " socket_path_name data...\n";                return 1;        @}        iosockunix oput (sockbuf::sock_stream);        oput->connect (av [1]);        char buf[128];        oput >> buf;        cout << buf << ' ';        oput >> buf;        cout << buf << endl;        while (*av) oput << *av++ << ' ';        oput << endl;                return 0;@}@end example@node sockunixaddr Class@chapter sockunixaddr Class@cindex sockunixaddr class@cindex unix address classClass @code{sockunixaddr} is derived from class @code{sockAddr} declared in@code{<sockstream.h>} and from struct @code{sockaddr_un} declared in@code{<sys/un.h>}. Always use @code{sockunixaddr} objects for addresseswith @var{unix} domain of sockets. @xref{Connection Establishment}.In what follows,@itemize @minus@item@code{path} is the @var{unix} path name like "/tmp/unix_socket"@end itemize@table @code@item sockunixaddr suna(path)@findex sockunixaddr::sockunixaddrConstructs a @code{sockunixaddr} object @code{suna} with @code{path} asthe @var{unix} path name.@item void* a = suna@findex sockunixaddr::operator void*returns the address of the @code{sockaddr_un} part of@code{sockunixaddr} object @code{suna} as void*.@item int sz = suna.size()@findex sockunixaddr::sizereturns the sizeof @code{sockaddr_un} part of @code{sockunixaddr} object@code{suna}.@item int af = suna.family()@findex sockunixaddr::familyreturns @code{sockunixbuf::af_unix} if all is well.@end table@node sockstream Classes@chapter sockstream Classes@cindex sockstream classes@cindex iosockstream classessockstream classes are designed in such a way that they provide the sameinterface as their stream counterparts do. We have @code{isockstream}derived from @code{istream} and @code{osockstream} derived from@code{ostream}. We also have @code{iosockstream} which is derived from@code{iostream}.Each domain also has its own set of @code{stream} classes. For example,@code{unix} domain has @code{isockunix}, @code{osockunix}, and@code{iosockunix} derived from @code{isockstream}, @code{osockstream},and @code{iosockstream} respectively. Similarly, @code{inet} domain has@code{isockinet}, @code{osockinet}, and @code{iosockinet}.@menu* iosockstream::             Generic IOStream classes for sockbuf                                   buffers.* iosockinet::               IOStream classes for @var{inet} domain of                                   sockets.* iosockunix::               IOStream classes for @var{unix} domain of                                   sockets.@end menu@node iosockstream@section iosockstreams@subsection isockstream Class@cindex isockstream classSince @code{isockstream} is publicly derived from @code{istream}, mostof the public functions of @code{istream} are also available in@code{isockstream}. @code{isockstream} redefines @code{rdbuf()} defined in its virtual baseclass @code{ios}. Since, @code{ios::rdbuf()} is not virtual, care mustbe taken to call the correct @code{rdbuf()} through a reference or apointer to an object of class @code{isockstream}.In what follows,@itemize @minus@item@code{sb} is a @code{sockbuf} object@item@code{sbp} is a pointer to a @code{sockbuf} object@end itemize@table @code@item isockstream is(sb)@findex isockstream::isockstreamConstructs an @code{isockstream} object @code{is} with @code{sb} as its@code{sockbuf}.@item isockstream is(sbp)Constructs an @code{isockstream} object @code{is} with @code{*sbp} as its@code{sockbuf}.@item sbp = is.rdbuf()@findex isockstream::rdbufreturns a pointer to the @code{sockbuf} of the @code{isockstream} object@code{is}.@item isockstream::operator -> ()@findex isockstream::operator->returns a pointer to the @code{isockstream}'s @code{sockbuf} so thatthe user can use @code{isockstream} object as a @code{sockbuf} object.@example    is->connect (sa); // same as is.rdbuf()->connect (sa);@end example@end table@subsection osockstream Class@cindex osockstream classSince @code{osockstream} is publicly derived from @code{ostream}, mostof the public functions of @code{ostream} are also available in@code{osockstream}. @code{osockstream} redefines @code{rdbuf()} defined in its virtual baseclass @code{ios}. Since, @code{ios::rdbuf()} is not virtual, care mustbe taken to call the correct @code{rdbuf()} through a reference or apointer to an object of class @code{osockstream}.In what follows,@itemize @minus@item@code{sb} is a @code{sockbuf} object@item@code{sbp} is a pointer to a @code{sockbuf} object@end itemize@table @code@item osockstream os(sb)@findex osockstream::osockstreamConstructs an @code{osockstream} object @code{os} with @code{sb} as its@code{sockbuf}.@item osockstream os(sbp)Constructs an @code{osockstream} object @code{os} with @code{*sbp} as its@code{sockbuf}.@item sbp = os.rdbuf()@findex osockstream::rdbufreturns a pointer to the @code{sockbuf} of the @code{osockstream} object@code{os}.@item osockstream::operator -> ()@findex osockstream::operator->returns a pointer to the @code{osockstream}'s @code{sockbuf} so thatthe user can use @code{osockstream} object as a @code{sockbuf} object.@example    os->connect (sa); // same as os.rdbuf()->connect (sa);@end example@end table@subsection iosockstream Class@cindex iosockstream classSince @code{iosockstream} is publicly derived from @code{iostream}, mostof the public functions of @code{iostream} are also available in@code{iosockstream}. @code{iosockstream} redefines @code{rdbuf()} defined in its virtual baseclass @code{ios}. Since, @code{ios::rdbuf()} is not virtual, care mustbe taken to call the correct @code{rdbuf()} through a reference or apointer to an object of class @code{iosockstream}.In what follows,@itemize @minus@item@code{sb} is a @code{sockbuf} object@item@code{sbp} is a pointer to a @code{sockbuf} object@end itemize@table @code@item iosockstream io(sb)@findex iosockstream::iosockstreamConstructs an @code{iosockstream} object @code{io} with @code{sb} as its@code{sockbuf}.@item iosockstream io(sbp)Constructs an @code{iosockstream} object @code{io} with @code{*sbp} as its@code{sockbuf}.@item sbp = io.rdbuf()@findex iosockstream::rdbufreturns a pointer to the @code{sockbuf} of the @code{iosockstream} object@code{io}.@item iosockstream::operator -> ()@findex iosockstream::operator->returns a pointer to the @code{iosockstream}'s @code{sockbuf} so thatthe user can use @code{iosockstream} object as a @code{sockbuf} object.@example    io->connect (sa); // same as io.rdbuf()->connect (sa);@end example@end table@node iosockinet@section iosockinet Stream ClassesWe discus only @code{isockinet} class here. @code{osockinet} and@code{iosockinet} are similar and are left out. However, they arecovered in the examples that follow.@subsection isockinet@cindex isockinet class@cindex class isockinet@code{isockinet} is used to handle interprocess communication in@var{inet} domain. It is derived from @code{isockstream} class and ituses a @code{sockinetbuf} as its stream buffer. @xref{iosockstream}, formore details on @code{isockstream}. @xref{sockinetbuf Class}, forinformation on @code{sockinetbuf}.In what follows,@itemize @minus@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}@item@code{proto} denotes the protocol number and is of type int

⌨️ 快捷键说明

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