socket.html

来自「perl教程」· HTML 代码 · 共 250 行

HTML
250 行
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>IO::Socket - Object interface to socket communications</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',2);</script>
<h1><a>IO::Socket - Object interface to socket communications</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#synopsis">SYNOPSIS</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<li><a href="#constructor">CONSTRUCTOR</a></li>
	<li><a href="#methods">METHODS</a></li>
	<li><a href="#see_also">SEE ALSO</a></li>
	<li><a href="#author">AUTHOR</a></li>
	<li><a href="#copyright">COPYRIGHT</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>IO::Socket - Object interface to socket communications</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
    <span class="keyword">use</span> <span class="variable">IO::Socket</span><span class="operator">;</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p><code>IO::Socket</code> provides an object interface to creating and using sockets. It
is built upon the <a href="../../lib/IO/Handle.html">the IO::Handle manpage</a> interface and inherits all the methods defined
by <a href="../../lib/IO/Handle.html">the IO::Handle manpage</a>.</p>
<p><code>IO::Socket</code> only defines methods for those operations which are common to all
types of socket. Operations which are specified to a socket in a particular 
domain have methods defined in sub classes of <code>IO::Socket</code></p>
<p><code>IO::Socket</code> will export all functions (and constants) defined by <a href="../../lib/Socket.html">the Socket manpage</a>.</p>
<p>
</p>
<hr />
<h1><a name="constructor">CONSTRUCTOR</a></h1>
<dl>
<dt><strong><a name="item_new">new ( [ARGS] )</a></strong>

<dd>
<p>Creates an <code>IO::Socket</code>, which is a reference to a
newly created symbol (see the <code>Symbol</code> package). <a href="#item_new"><code>new</code></a>
optionally takes arguments, these arguments are in key-value pairs.
<a href="#item_new"><code>new</code></a> only looks for one key <code>Domain</code> which tells new which domain
the socket will be in. All other arguments will be passed to the
configuration method of the package for that domain, See below.</p>
</dd>
<dd>
<pre>
 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE</pre>
</dd>
<dd>
<p>As of VERSION 1.18 all IO::Socket objects have autoflush turned on
by default. This was not the case with earlier releases.</p>
</dd>
<dd>
<pre>
 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE</pre>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="methods">METHODS</a></h1>
<p>See <a href="../../lib/Pod/perlfunc.html">the perlfunc manpage</a> for complete descriptions of each of the following
supported <code>IO::Socket</code> methods, which are just front ends for the
corresponding built-in functions:</p>
<pre>
    socket
    socketpair
    bind
    listen
    accept
    send
    recv
    peername (getpeername)
    sockname (getsockname)
    shutdown</pre>
<p>Some methods take slightly different arguments to those defined in <a href="../../lib/Pod/perlfunc.html">the perlfunc manpage</a>
in attempt to make the interface more flexible. These are</p>
<dl>
<dt><strong><a name="item_accept"><code>accept([PKG])</code></a></strong>

<dd>
<p>perform the system call <a href="#item_accept"><code>accept</code></a> on the socket and return a new
object. The new object will be created in the same class as the listen
socket, unless <code>PKG</code> is specified. This object can be used to
communicate with the client that was trying to connect.</p>
</dd>
<dd>
<p>In a scalar context the new socket is returned, or undef upon
failure. In a list context a two-element array is returned containing
the new socket and the peer address; the list will be empty upon
failure.</p>
</dd>
<dd>
<p>The timeout in the [PKG] can be specified as zero to effect a &quot;poll&quot;,
but you shouldn't do that because a new IO::Select object will be
created behind the scenes just to do the single poll.  This is
horrendously inefficient.  Use rather true <a href="../../lib/Pod/perlfunc.html#item_select"><code>select()</code></a> with a zero
timeout on the handle, or non-blocking IO.</p>
</dd>
</li>
<dt><strong><a name="item_socketpair">socketpair(DOMAIN, TYPE, PROTOCOL)</a></strong>

<dd>
<p>Call <a href="#item_socketpair"><code>socketpair</code></a> and return a list of two sockets created, or an
empty list on failure.</p>
</dd>
</li>
</dl>
<p>Additional methods that are provided are:</p>
<dl>
<dt><strong><a name="item_atmark">atmark</a></strong>

<dd>
<p>True if the socket is currently positioned at the urgent data mark,
false otherwise.</p>
</dd>
<dd>
<pre>
    <span class="keyword">use</span> <span class="variable">IO::Socket</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<pre>
    <span class="keyword">my</span> <span class="variable">$sock</span> <span class="operator">=</span> <span class="variable">IO::Socket::INET</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">(</span><span class="string">'some_server'</span><span class="operator">);</span>
    <span class="variable">$sock</span><span class="operator">-&gt;</span><span class="keyword">read</span><span class="operator">(</span><span class="variable">$data</span><span class="operator">,</span> <span class="number">1024</span><span class="operator">)</span> <span class="keyword">until</span> <span class="variable">$sock</span><span class="operator">-&gt;</span><span class="variable">atmark</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>Note: this is a reasonably new addition to the family of socket
functions, so all systems may not support this yet.  If it is
unsupported by the system, an attempt to use this method will
abort the program.</p>
</dd>
<dd>
<p>The <a href="#item_atmark"><code>atmark()</code></a> functionality is also exportable as <code>sockatmark()</code> function:</p>
</dd>
<dd>
<pre>
        <span class="keyword">use</span> <span class="variable">IO::Socket</span> <span class="string">'sockatmark'</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>This allows for a more traditional use of <code>sockatmark()</code> as a procedural
socket function.  If your system does not support sockatmark(), the
<a href="../../lib/Pod/perlfunc.html#item_use"><code>use</code></a> declaration will fail at compile time.</p>
</dd>
</li>
<dt><strong><a name="item_connected">connected</a></strong>

<dd>
<p>If the socket is in a connected state the peer address is returned.
If the socket is not in a connected state then undef will be returned.</p>
</dd>
</li>
<dt><strong><a name="item_protocol">protocol</a></strong>

<dd>
<p>Returns the numerical number for the protocol being used on the socket, if
known. If the protocol is unknown, as with an AF_UNIX socket, zero
is returned.</p>
</dd>
</li>
<dt><strong><a name="item_sockdomain">sockdomain</a></strong>

<dd>
<p>Returns the numerical number for the socket domain type. For example, for
an AF_INET socket the value of &amp;AF_INET will be returned.</p>
</dd>
</li>
<dt><strong><a name="item_sockopt">sockopt(OPT [, VAL])</a></strong>

<dd>
<p>Unified method to both set and get options in the SOL_SOCKET level. If called
with one argument then getsockopt is called, otherwise setsockopt is called.</p>
</dd>
</li>
<dt><strong><a name="item_socktype">socktype</a></strong>

<dd>
<p>Returns the numerical number for the socket type. For example, for
a SOCK_STREAM socket the value of &amp;SOCK_STREAM will be returned.</p>
</dd>
</li>
<dt><strong><a name="item_timeout"><code>timeout([VAL])</code></a></strong>

<dd>
<p>Set or get the timeout value associated with this socket. If called without
any arguments then the current setting is returned. If called with an argument
the current setting is changed and the previous value returned.</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p><a href="../../lib/Socket.html">the Socket manpage</a>, <a href="../../lib/IO/Handle.html">the IO::Handle manpage</a>, <a href="../../lib/IO/Socket/INET.html">the IO::Socket::INET manpage</a>, <a href="../../lib/IO/Socket/UNIX.html">the IO::Socket::UNIX manpage</a></p>
<p>
</p>
<hr />
<h1><a name="author">AUTHOR</a></h1>
<p>Graham Barr.  <a href="#item_atmark"><code>atmark()</code></a> by Lincoln Stein.  Currently maintained by the
Perl Porters.  Please report all bugs to &lt;<a href="mailto:perl5-porters@perl.org">perl5-porters@perl.org</a>&gt;.</p>
<p>
</p>
<hr />
<h1><a name="copyright">COPYRIGHT</a></h1>
<p>Copyright (c) 1997-8 Graham Barr &lt;<a href="mailto:gbarr@pobox.com">gbarr@pobox.com</a>&gt;. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.</p>
<p>The <a href="#item_atmark"><code>atmark()</code></a> implementation: Copyright 2001, Lincoln Stein &lt;<a href="mailto:lstein@cshl.org">lstein@cshl.org</a>&gt;.
This module is distributed under the same terms as Perl itself.
Feel free to use, modify and redistribute it as long as you retain
the correct attribution.</p>

</body>

</html>

⌨️ 快捷键说明

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