<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.socket-bind" xmlns="http://docbook.org/ns/docbook">
 <refnamediv>
  <refname>socket_bind</refname>
  <refpurpose>Binds a name to a socket</refpurpose>
 </refnamediv>

 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis>
   <type>bool</type><methodname>socket_bind</methodname>
   <methodparam><type>Socket</type><parameter>socket</parameter></methodparam>
   <methodparam><type>string</type><parameter>address</parameter></methodparam>
   <methodparam choice="opt"><type>int</type><parameter>port</parameter><initializer>0</initializer></methodparam>
  </methodsynopsis>
  <para>
   Binds the name given in <parameter>address</parameter> to the socket
   described by <parameter>socket</parameter>. This has to be done before
   a connection is established using <function>socket_connect</function>
   or <function>socket_listen</function>.
  </para>
 </refsect1>

 <refsect1 role="parameters">
  &reftitle.parameters;
  <para>
   <variablelist>
    <varlistentry>
     <term><parameter>socket</parameter></term>
     <listitem>
      <para>
       A <classname>Socket</classname> instance created with <function>socket_create</function>.
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>address</parameter></term>
     <listitem>
      <para>
       If the socket is of the <constant>AF_INET</constant> family, the
       <parameter>address</parameter> is an IP in dotted-quad notation
       (e.g. <literal>127.0.0.1</literal>).
      </para>
      <para>
       If the socket is of the <constant>AF_UNIX</constant> family, the
       <parameter>address</parameter> is the path of a
       Unix-domain socket (e.g. <filename>/tmp/my.sock</filename>).
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>port</parameter> (Optional)</term>
     <listitem>
      <para>
       The <parameter>port</parameter> parameter is only used when
       binding an <constant>AF_INET</constant> socket, and designates
       the port on which to listen for connections.
      </para>
     </listitem>
    </varlistentry>
   </variablelist>
  </para>
 </refsect1>

 <refsect1 role="returnvalues">
  &reftitle.returnvalues;
  <para>
   &return.success;
  </para>
  <para>
   The error code can be retrieved with <function>socket_last_error</function>.
   This code may be passed to <function>socket_strerror</function> to get a
   textual explanation of the error.
  </para>
 </refsect1>

 <refsect1 role="changelog">
  &reftitle.changelog;
  <informaltable>
   <tgroup cols="2">
    <thead>
     <row>
      <entry>&Version;</entry>
      <entry>&Description;</entry>
     </row>
    </thead>
    <tbody>
     &sockets.changelog.socket-param;
    </tbody>
   </tgroup>
  </informaltable>
 </refsect1>
 
 <refsect1 role="examples">
  &reftitle.examples;
  <para>
   <example>
    <title>Using <function>socket_bind</function> to set the source address</title>
    <programlisting role="php">
<![CDATA[
<?php
// Create a new socket
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

// An example list of IP addresses owned by the computer
$sourceips['kevin']    = '127.0.0.1';
$sourceips['madcoder'] = '127.0.0.2';

// Bind the source address
socket_bind($sock, $sourceips['madcoder']);

// Connect to destination address
socket_connect($sock, '127.0.0.1', 80);

// Write
$request = 'GET / HTTP/1.1' . "\r\n" .
           'Host: example.com' . "\r\n\r\n";
socket_write($sock, $request);

// Close
socket_close($sock);

?> 
]]>
    </programlisting>
   </example>
  </para>
 </refsect1>

 <refsect1 role="notes">
  &reftitle.notes;
  <note>
   <para>
    This function must be used on the socket before
    <function>socket_connect</function>.
   </para>
  </note>
  <note>
   <para>
    Windows 9x/ME compatibility note:
    <function>socket_last_error</function> may return an invalid error code
    if trying to bind the socket to a wrong address that does not belong to
    your machine.
   </para>
  </note>
 </refsect1>

 <refsect1 role="seealso">
  &reftitle.seealso;
  <para>
   <simplelist>
    <member><function>socket_connect</function></member>
    <member><function>socket_listen</function></member>
    <member><function>socket_create</function></member>
    <member><function>socket_last_error</function></member>
    <member><function>socket_strerror</function></member>
   </simplelist>
  </para>
 </refsect1>

</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
