📄 ch17_01.htm
字号:
<HTML><HEAD><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:44:20Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="index.htm"TITLE="Perl Cookbook"><LINKREL="prev"HREF="ch16_23.htm"TITLE="16.22. Program: sigrand"><LINKREL="next"HREF="ch17_02.htm"TITLE="17.1. Writing a TCP Client"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch16_23.htm"TITLE="16.22. Program: sigrand"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 16.22. Program: sigrand"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch17_02.htm"TITLE="17.1. Writing a TCP Client"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 17.1. Writing a TCP Client"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="chapter"><H1CLASS="chapter"><ACLASS="title"NAME="ch17-14176">17. Sockets</A></H1><DIVCLASS="htmltoc"><P><B>Contents:</B><BR><ACLASS="sect1"HREF="#ch17-35785"TITLE="17.0. Introduction">Introduction</A><BR><ACLASS="sect1"HREF="ch17_02.htm"TITLE="17.1. Writing a TCP Client">Writing a TCP Client</A><BR><ACLASS="sect1"HREF="ch17_03.htm"TITLE="17.2. Writing a TCP Server">Writing a TCP Server</A><BR><ACLASS="sect1"HREF="ch17_04.htm"TITLE="17.3. Communicating over TCP">Communicating over TCP</A><BR><ACLASS="sect1"HREF="ch17_05.htm"TITLE="17.4. Setting Up a UDP Client">Setting Up a UDP Client</A><BR><ACLASS="sect1"HREF="ch17_06.htm"TITLE="17.5. Setting Up a UDP Server">Setting Up a UDP Server</A><BR><ACLASS="sect1"HREF="ch17_07.htm"TITLE="17.6. Using UNIX Domain Sockets">Using UNIX Domain Sockets</A><BR><ACLASS="sect1"HREF="ch17_08.htm"TITLE="17.7. Identifying the Other End of a Socket">Identifying the Other End of a Socket</A><BR><ACLASS="sect1"HREF="ch17_09.htm"TITLE="17.8. Finding Your Own Name and Address">Finding Your Own Name and Address</A><BR><ACLASS="sect1"HREF="ch17_10.htm"TITLE="17.9. Closing a Socket After Forking">Closing a Socket After Forking</A><BR><ACLASS="sect1"HREF="ch17_11.htm"TITLE="17.10. Writing Bidirectional Clients">Writing Bidirectional Clients</A><BR><ACLASS="sect1"HREF="ch17_12.htm"TITLE="17.11. Forking Servers">Forking Servers</A><BR><ACLASS="sect1"HREF="ch17_13.htm"TITLE="17.12. Pre-Forking Servers">Pre-Forking Servers</A><BR><ACLASS="sect1"HREF="ch17_14.htm"TITLE="17.13. Non-Forking Servers">Non-Forking Servers</A><BR><ACLASS="sect1"HREF="ch17_15.htm"TITLE="17.14. Writing a Multi-Homed Server">Writing a Multi-Homed Server</A><BR><ACLASS="sect1"HREF="ch17_16.htm"TITLE="17.15. Making a Daemon Server">Making a Daemon Server</A><BR><ACLASS="sect1"HREF="ch17_17.htm"TITLE="17.16. Restarting a Server on Demand">Restarting a Server on Demand</A><BR><ACLASS="sect1"HREF="ch17_18.htm"TITLE="17.17. Program: backsniff">Program: backsniff</A><BR><ACLASS="sect1"HREF="ch17_19.htm"TITLE="17.18. Program: fwdport">Program: fwdport</A></P><P></P></DIV><DIVCLASS="epigraph"ALIGN="right"><PCLASS="para"ALIGN="right"><I>Glendower: I can call spirits from the vasty deep.Hotspur:Why so can I, or so can any man,But will they come when you do call for them?</I></P><PCLASS="attribution"ALIGN="right">- Shakespeare <CITECLASS="citetitle"> King Henry IV Part I, Act III Scene 1</CITE></P></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch17-35785">17.0. Introduction</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="ch17-idx-1000004654-0"></A>Sockets are endpoints for communication. Some types of sockets provide reliable communications. Others offer few guarantees, but consume low system overhead. Socket communication can be used to let processes talk on just one machine or over the Internet.</P><PCLASS="para">In this chapter we consider the two most commonly used types of sockets: <EMCLASS="emphasis">streams</EM><ACLASS="indexterm"NAME="ch17-idx-1000004824-0"></A> and <EMCLASS="emphasis">datagrams</EM><ACLASS="indexterm"NAME="ch17-idx-1000004656-0"></A>. Streams provide a bidirectional, sequenced, and reliable channel of communication - similar to pipes. <EMCLASS="emphasis">Datagram</EM> sockets do not guarantee sequenced, reliable delivery, but they do guarantee that message boundaries will be preserved when read. Your system may support other types of sockets as well; consult your <ICLASS="filename">socket </I>(2) manpage or equivalent documentation for details.</P><PCLASS="para">We also consider both the Internet and Unix <ACLASS="indexterm"NAME="ch17-idx-1000004657-0"></A><ACLASS="indexterm"NAME="ch17-idx-1000004657-1"></A><ACLASS="indexterm"NAME="ch17-idx-1000004657-2"></A><ACLASS="indexterm"NAME="ch17-idx-1000004657-3"></A>domains. The Internet domain gives sockets two-part names: a host (an IP address in a particular format) and a port number. In the Unix domain, sockets are named using files (e.g., <EMCLASS="emphasis">/tmp/mysock</EM>).</P><PCLASS="para">In addition to domains and types, sockets also have a <EMCLASS="emphasis">protocol</EM><ACLASS="indexterm"NAME="ch17-idx-1000004658-0"></A> associated with them. Protocols are not very important to the casual programmer, as there is rarely more than one protocol for a given domain and type of socket.</P><PCLASS="para">Domains and types are normally identified by numeric constants (available through functions exported by the Socket and IO::Socket modules). Stream sockets have the type <ACLASS="indexterm"NAME="ch17-idx-1000004659-0"></A>SOCK_STREAM, and datagram sockets have the type SOCK_DGRAM. The Internet domain is <ACLASS="indexterm"NAME="ch17-idx-1000004660-0"></A>PF_INET, and the Unix domain PF_UNIX. (POSIX uses PF_LOCAL instead of PF_UNIX, but PF_UNIX will almost always be an acceptable constant simply because of the preponderance of existing software that uses it.) You should use these symbolic names instead of numbers because the numbers may change (and historically, have).</P><PCLASS="para">Protocols have names like <CODECLASS="literal">tcp</CODE> and <CODECLASS="literal">udp</CODE>, which correspond to numbers that the operating system uses. The <CODECLASS="literal">getprotobyname</CODE><ACLASS="indexterm"NAME="ch17-idx-1000004661-0"></A> function (built into Perl) returns the number when given a protocol name. Pass protocol number <CODECLASS="literal">0</CODE> to socket functions to have the system select an appropriate default.</P><PCLASS="para">Perl has built-in functions to create and manipulate sockets; these functions largely mimic their C counterparts. While this is good for providing low-level, direct access to every part of the system, most of us prefer something more convenient. That's what the IO::Socket::INET and IO::Socket::UNIX classes are for - they provide a high-level interface to otherwise intricate system calls.</P><PCLASS="para">Let's look at the built-in functions first. They all return <CODECLASS="literal">undef</CODE> and set <CODECLASS="literal">$!</CODE> if an error occurs. The <CODECLASS="literal">socket</CODE><ACLASS="indexterm"NAME="ch17-idx-1000004662-0"></A> function makes a socket, <CODECLASS="literal">bind</CODE><ACLASS="indexterm"NAME="ch17-idx-1000004664-0"></A> gives a socket a local name, <CODECLASS="literal">connect</CODE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -