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

📄 socket++.texi

📁 《CODE READING》配套书源代码 《CODE READING》配套书源代码
💻 TEXI
📖 第 1 页 / 共 5 页
字号:
@item@code{sb} is a @code{sockbuf} object and must be in @var{inet} domain@item@code{sinp} is a pointer to an object of @code{sockinetbuf}@end itemize@table @code@item isockinet is (ty, proto)@findex isockinet::isockinetconstructs an @code{isockinet} object @code{is} whose @code{sockinetbuf}buffer is of the type @code{ty} and has the protocol number@code{proto}. The default protocol number is 0.@item isockinet is (sb)constructs a @code{isockinet} object @code{is} whose @code{sockinetbuf}is @code{sb}. @code{sb} must be in @var{inet} domain.@item isockinet is (sinp)constructs a @code{isockinet} object @code{is} whose @code{sockinetbuf}is @code{sinp}.@item sinp = is.rdbuf ()@findex isockinet::rdbufreturns a pointer to the @code{sockinetbuf} of @code{isockinet} object@code{is}.@item isockinet::operator ->@findex isockinet::operator->returns @code{sockinetbuf} of @code{sockinet} so that the @code{sockinet}object acts as a smart pointer to @code{sockinetbuf}.@example        is->localhost (); // same as is.rdbuf ()->localhost ();@end example@end table@subsection iosockinet examples@cindex iosockinet examplesThe first pair of examples demonstrates datagram socket connections in the@var{inet} domain. First, @code{tdinread} prints its local host andlocal port on stdout and waits for input in the connection.@code{tdinwrite} is started with the local host and local port of@code{tdinread} as arguments. It sends the string "How do ye do!" to@code{tdinread} which in turn reads the string and prints on its stdout.@example// tdinread.cc#include <sockinet.h>int main ()@{    char buf[256];    isockinet is (sockbuf::sock_dgram);    is->bind ();    cout << is->localhost() << ' ' << is->localport() << endl;    is.getline (buf);    cout << buf << endl;    return 0;@}@end example@example// tdinwrite.cc--tdinwrite hostname portno#include <sockinet.h>#include <stdlib.h>int main (int ac, char** av)@{    osockinet os (sockbuf::sock_dgram);    os->connect (av[1], atoi(av[2]));    os << "How do ye do!" << endl;    return 0;@}@end exampleThe next example communicates with an nntp server through a@code{sockbuf::sock_stream} socket connection in @var{inet} domain.After establishing a connection to the nntp server, it sends a "HELP"command and gets back the HELP message before sending the "QUIT"command.@example// tnntp.cc#include <sockinet.h>int main ()@{    char  buf[1024];    iosockinet io (sockbuf::sock_stream);    io->connect ("murdoch.acc.virginia.edu", "nntp", "tcp");    io.getline (buf, 1024); cout << buf << endl;    io << "HELP\r\n" << flush;    io.getline (buf, 1024); cout << buf << endl;    while (io.getline (buf, 1024))        if (buf[0] == '.' && buf[1] == '\r') break;        else if (buf[0] == '.' && buf[1] == '.') cout << buf+1 << endl;        else cout << buf << endl;    io << "QUIT\r\n" << flush;    io.getline (buf, 1024); cout << buf << endl;    return 0;@}@end example@node iosockunix@section iosockunix Classes@cindex iosockunix classWe discuss only @code{isockunix} here. @code{osockunix} and@code{iosockunix} are similar.@subsection isockunix class@cindex isockunix class@cindex class isockunix@code{isockunix} is used to handle interprocess communication in@var{unix} domain. It is derived from @code{isockstream} class and ituses a @code{sockunixbuf} as its stream buffer. @xref{iosockstream}, formore details on @code{isockstream}. @xref{sockunixbuf Class}, forinformation on @code{sockunixbuf}.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@item@code{sb} is a @code{sockbuf} object and must be in @var{unix} domain@item@code{sinp} is a pointer to an object of @code{sockunixbuf}@end itemize@table @code@item isockunix is (ty, proto)@findex isockunix::isockunixconstructs an @code{isockunix} object @code{is} whose @code{sockunixbuf}buffer is of the type @code{ty} and has the protocol number@code{proto}. The default protocol number is 0.@item isockunix is (sb)constructs a @code{isockunix} object @code{is} whose @code{sockunixbuf}is @code{sb}. @code{sb} must be in @var{unix} domain.@item isockunix is (sinp)constructs a @code{isockunix} object @code{is} whose @code{sockunixbuf}is @code{sinp}.@item sinp = is.rdbuf ()@findex isockunix::rdbufreturns a pointer to the @code{sockunixbuf} of @code{isockunix} object@code{is}.@item isockunix::operator ->@findex isockunix::operator->returns @code{sockunixbuf} of @code{sockunix} so that the @code{sockunix}object acts as a smart pointer to @code{sockunixbuf}.@example        is->localhost (); // same as is.rdbuf ()->localhost ();@end example@end table@subsection iosockunix examples@cindex iosockunix examples@code{tsunread} listens for connections. When @code{tsunwrite} requestsconnection, @code{tsunread} accepts it and waits for input.@code{tsunwrite} sends the string "Hello!!!" to @code{tsunread}.@code{tsunread} reads the string sent by @code{tsunwrite} and prints onits stdout.@example// tsunread.cc#include <sockunix.h>#include <unistd.h>int main ()@{    sockunixbuf sunb (sockbuf::sock_stream);    sunb.bind ("/tmp/socket+-");    sunb.listen (2);    isockunix is = sunb.accept ();    char buf[32];    is >> buf; cout << buf << endl;    unlink ("/tmp/socket+-");    return 0;@}@end example@example// tsunwrite.cc#include <sockunix.h>int main ()@{    osockunix os (sockbuf::sock_stream);    os->connect ("/tmp/socket++");    os << "Hello!!!\n" << flush;    return 0;@}@end example    @node pipestream Classes@chapter pipestream Classes@cindex pipestream classes@cindex pipestream examples@findex popen@findex pipe@findex socketpair@code{pipestream} stream classes provide the services of the @var{UNIX}system calls @code{pipe} and @code{socketpair} and the C libraryfunction @code{popen}. @code{ipipestream}, @code{opipestream}, and@code{iopipestream} are obtained by simply deriving from@code{isockstream}, @code{osockstream} and @code{iosockstream}respectively. @xref{sockstream Classes} for details.In what follows,@itemize @minus@item@code{ip} is an @code{ipipestream} object@item@code{op} is an @code{opipestream} object@item@code{iop} is an @code{iopipestream} object@item@code{cmd} is a char* denoting an executable like "wc"@item@code{ty} is of type @code{sockbuf::type} indicating the type of theconnection@item@code{proto} is an @code{int} denoting a protocol number@end itemize@table @code@item ipipestream ip(cmd)@findex ipipestream::ipipestreamconstruct an @code{ipipestream} object @code{ip} such that the output ofthe command @code{cmd} is available as input through @code{ip}.@item opipestream op(cmd)@findex opipestream::opipestreamconstruct an @code{opipestream} object @code{op} such that the input forthe command @code{cmd} can be send through @code{op}.@item iopipestream iop(cmd)@findex iopipestream::iopipestreamconstruct an @code{iopipestream} object @code{iop} such that the inputand the output to the command @code{cmd} can be sent and receivedthrough @code{iop}.@item iopipestream iop(ty, proto)construct a @code{iopipestream} object @code{iop} whose socket is asocketpair of type @code{ty} with protocol number @code{proto}.@code{ty} defaults to @code{sockbuf::sock_stream} and @code{proto}defaults to 0. Object @code{iop} can be used either as a @code{pipe} oras a @code{socketpair}.@item iop.pid ()@findex iopipestream::pidreturn the process id of the child if the current process is the parentor return 0. If the process has not forked yet, return -1.@item iopipestream::fork ()@findex iopipestream::fork@code{fork()} is a static function of class @code{iopipestream}.@code{fork()} forks the current process and appropriately sets the@code{cpid} field of the @code{iopipestream} objects that have notforked yet.@end table@menu* pipe Example::                 How to use pipestream as pipe?* socketpair Example::           How to use pipestream as socketpair?* popen Example::                How to use pipestream as popen?@end menu@node pipe Example@section pipestream as pipe@cindex pipe example@code{pipe} is used to communicate between parent and child processes inthe @var{unix} domain. The following example illustrates how to use @code{iopipestream} class asa @code{pipe}. The parent sends the string "I am the parent" to thechild and receives the string "I am the child" from child. The child, inturn, receives the string "I am the parent" from parent and sends thestring "I am the child" to the parent. Note the same @code{iopipestream}object is used for input and output in each process.@example#include <pipestream.h>int main()@{        iopipestream p;        if ( p.fork() ) @{                char buf[128];                p << "I am the parent\n" << flush;                cout << "parent: ";                while(p >> buf)                        cout << buf << ' ';                cout << endl;        @}else @{                char buf[128];                p.getline(buf, 127);                cout << "child: " << buf << endl;                p << "I am the child\n" << flush;        @}        return 0;@}@end example@node socketpair Example@section pipestream as socketpair@cindex socketpair exampleLike pipes, socketpairs also allow communication between parent andchild processes. But socketpairs are more flexible than pipes in thesense that they let the users choose the socket type and protocol.The following example illustrates the use of @code{iopipestream} class asa @code{socketpair} whose type is @code{sockbuf::sock_dgram}. The parentsends the string "I am the parent" to the child and receives the string"I am the child" from the child. The child, in turn, receives and sendsthe strings "I am the parent" and "I am the child" respectively from andto the parent. Note in the following example that the same@code{iopipestream} object is used for both the input and the output ineach process.@example#include <pipestream.h>int main()@{        iopipestream p(sockbuf::sock_dgram);        if ( iopipestream::fork() ) @{                char buf[128];                p << "I am the parent\n" << flush;                p.getline(buf, 127);                cout << "parent: " << buf << endl;        @}else @{                char buf[128];                p.getline(buf, 127);                cout << "child: " << buf << endl;                p << "I am the child\n" << flush;        @}        return 0;@}@end example@node popen Example@section pipestream as popen@cindex popen example@code{popen} is used to call an executable and send inputs andoutputs to that executable. For example, the following exampleexecutes "/bin/date", gets its output, and prints it to stdout.@example#include <pipestream.h>int main ()@{    char buf[128];    ipipestream p("/bin/date");    p.getline (buf, 127);    cout << buf << endl;    return 0;@}@end exampleHere is an example that prints "Hello World!!" on stdout. It uses@code{opipestream} object.@example#include <pipestream.h>int main ()@{    opipestream p("/bin/cat");    p << "Hello World!!\n" << endl;    return 0;@}@end exampleThe following example illustrates the use of @code{iopipestream} forboth input and output.@example#include <pipestream.h>int main()@{        char buf[128];     

⌨️ 快捷键说明

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