<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >socket_bind</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="Socket Functions" HREF="ref.sockets.html"><LINK REL="PREVIOUS" TITLE="socket_accept" HREF="function.socket-accept.html"><LINK REL="NEXT" TITLE="socket_clear_error" HREF="function.socket-clear-error.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.socket-accept.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.socket-clear-error.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.socket-bind" ></A >socket_bind</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN212706" ></A ><P > (PHP 4 >= 4.1.0, PHP 5)</P >socket_bind -- Binds a name to a socket</DIV ><DIV CLASS="refsect1" ><A NAME="AEN212709" ></A ><H2 >说明</H2 >bool <B CLASS="methodname" >socket_bind</B > ( resource socket, string address [, int port] )<BR ></BR ><P > Binds the name given in <CODE CLASS="parameter" >address</CODE > to the socket described by <CODE CLASS="parameter" >socket</CODE >. This has to be done before a connection can be established using <A HREF="function.socket-connect.html" ><B CLASS="function" >socket_connect()</B ></A > or <A HREF="function.socket-listen.html" ><B CLASS="function" >socket_listen()</B ></A >. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN212728" ></A ><H2 >参数</H2 ><P > <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><CODE CLASS="parameter" >socket</CODE ></DT ><DD ><P > A valid socket resource created with <A HREF="function.socket-create.html" ><B CLASS="function" >socket_create()</B ></A >. </P ></DD ><DT ><CODE CLASS="parameter" >address</CODE ></DT ><DD ><P > If the socket is of the <TT CLASS="constant" ><B >AF_INET</B ></TT > family, the <CODE CLASS="parameter" >address</CODE > is an IP in dotted-quad notation (e.g. <TT CLASS="literal" >127.0.0.1</TT >). </P ><P > If the socket is of the <TT CLASS="constant" ><B >AF_UNIX</B ></TT > family, the <CODE CLASS="parameter" >address</CODE > is the path of a Unix-domain socket (e.g. <TT CLASS="filename" >/tmp/my.sock</TT >). </P ></DD ><DT ><CODE CLASS="parameter" >port</CODE > (Optional)</DT ><DD ><P > The <CODE CLASS="parameter" >port</CODE > parameter is only used when connecting to an <TT CLASS="constant" ><B >AF_INET</B ></TT > socket, and designates the port on the remote host to which a connection should be made. </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN212757" ></A ><H2 >返回值</H2 ><P >如果成功则返回 <TT CLASS="constant" ><B >TRUE</B ></TT >,失败则返回 <TT CLASS="constant" ><B >FALSE</B ></TT >。</P ><P > The error code can be retrieved with <A HREF="function.socket-last-error.html" ><B CLASS="function" >socket_last_error()</B ></A >. This code may be passed to <A HREF="function.socket-strerror.html" ><B CLASS="function" >socket_strerror()</B ></A > to get a textual explanation of the error. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN212765" ></A ><H2 >范例</H2 ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN212768" ></A ><P ><B >例 1. Using <B CLASS="function" >socket_bind()</B > to set the source address</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// Create a new socket<br /></font><font color="#0000BB">$sock </font><font color="#007700">= </font><font color="#0000BB">socket_create</font><font color="#007700">(</font><font color="#0000BB">AF_INET</font><font color="#007700">, </font><font color="#0000BB">SOCK_STREAM</font><font color="#007700">, </font><font color="#0000BB">SOL_TCP</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// An example list of IP addresses owned by the computer<br /></font><font color="#0000BB">$sourceips</font><font color="#007700">[</font><font color="#DD0000">'kevin'</font><font color="#007700">] = </font><font color="#DD0000">'127.0.0.1'</font><font color="#007700">;<br /></font><font color="#0000BB">$sourceips</font><font color="#007700">[</font><font color="#DD0000">'madcoder'</font><font color="#007700">] = </font><font color="#DD0000">'127.0.0.2'</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Bind the source address<br /></font><font color="#0000BB">socket_bind</font><font color="#007700">(</font><font color="#0000BB">$sock</font><font color="#007700">, </font><font color="#0000BB">$sourceips</font><font color="#007700">[</font><font color="#DD0000">'madcoder'</font><font color="#007700">]);<br /><br /></font><font color="#FF8000">// Connect to destination address<br /></font><font color="#0000BB">socket_connect</font><font color="#007700">(</font><font color="#0000BB">$sock</font><font color="#007700">, </font><font color="#DD0000">'127.0.0.1'</font><font color="#007700">, </font><font color="#0000BB">80</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Write<br /></font><font color="#0000BB">$request </font><font color="#007700">= </font><font color="#DD0000">'GET / HTTP/1.1' </font><font color="#007700">. </font><font color="#DD0000">"\r\n" </font><font color="#007700">.<br /> </font><font color="#DD0000">'Host: example.com' </font><font color="#007700">. </font><font color="#DD0000">"\r\n\r\n"</font><font color="#007700">;<br /></font><font color="#0000BB">socket_write</font><font color="#007700">(</font><font color="#0000BB">$sock</font><font color="#007700">, </font><font color="#0000BB">$request</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Close<br /></font><font color="#0000BB">socket_close</font><font color="#007700">(</font><font color="#0000BB">$sock</font><font color="#007700">);<br /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN212772" ></A ><H2 >注释</H2 ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > This function must be used on the socket before <A HREF="function.socket-connect.html" ><B CLASS="function" >socket_connect()</B ></A >. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > Windows 9x/ME compatibility note: <A HREF="function.socket-last-error.html" ><B CLASS="function" >socket_last_error()</B ></A > may return an invalid error code if trying to bind the socket to a wrong address that does not belong to your machine. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN212780" ></A ><H2 >参见</H2 ><P > <P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD ><A HREF="function.socket-connect.html" ><B CLASS="function" >socket_connect()</B ></A ></TD ></TR ><TR ><TD ><A HREF="function.socket-listen.html" ><B CLASS="function" >socket_listen()</B ></A ></TD ></TR ><TR ><TD ><A HREF="function.socket-create.html" ><B CLASS="function" >socket_create()</B ></A ></TD ></TR ><TR ><TD ><A HREF="function.socket-last-error.html" ><B CLASS="function" >socket_last_error()</B ></A ></TD ></TR ><TR ><TD ><A HREF="function.socket-strerror.html" ><B CLASS="function" >socket_strerror()</B ></A ></TD ></TR ></TBODY ></TABLE ><P ></P > </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.socket-accept.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.socket-clear-error.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >socket_accept</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.sockets.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >socket_clear_error</TD ></TR ></TABLE ></DIV ></BODY ></HTML >