1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
|
<?xml version="1.0" encoding="utf-8"?>
<reference id="ref.sockets">
<title>Socket functions</title>
<titleabbrev>Sockets</titleabbrev>
<partintro>
&warn.experimental;
<simpara>
The socket extension implements a low-level interface to the
socket communication functions, providing the possibility to act
as a socket server as well as a client.
</simpara>
<para>
The socket functions described here are part of an extension to
PHP which must be enabled at compile time by giving the <option
role="configure">--enable-sockets</option> option to
<command>configure</command>.
</para>
<para>
For a more generic client-side socket interface, see
<function>fsockopen</function> and
<function>pfsockopen</function>.
</para>
<para>
When using these functions, it is important to remember that while
many of them have identical names to their C counterparts, they
often have different declarations. Please be sure to read the
descriptions to avoid confusion.
</para>
<para>
That said, those unfamiliar with socket programming can still find
a lot of useful material in the appropriate Unix man pages, and
there is a great deal of tutorial information on socket
programming in C on the web, much of which can be applied, with
slight modifications, to socket programming in PHP.
</para>
<para>
<example>
<title>Socket example: Simple TCP/IP server</title>
<para>
This example shows a simple talkback server. Change the
<varname>address</varname> and <varname>port</varname> variables
to suit your setup and execute. You may then connect to the
server with a command similar to: <command>telnet 192.168.1.53
10000</command> (where the address and port match your
setup). Anything you type will then be output on the server
side, and echoed back to you. To disconnect, enter 'quit'.
</para>
<programlisting role="php">
<?php
error_reporting (E_ALL);
/* Allow the script to hang around waiting for connections. */
set_time_limit (0);
$address = '192.168.1.53';
$port = 10000;
if (($sock = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
echo "socket() failed: reason: " . strerror ($sock) . "\n";
}
if (($ret = bind ($sock, $address, $port)) < 0) {
echo "bind() failed: reason: " . strerror ($ret) . "\n";
}
if (($ret = listen ($sock, 5)) < 0) {
echo "listen() failed: reason: " . strerror ($ret) . "\n";
}
do {
if (($msgsock = accept_connect($sock)) < 0) {
echo "accept_connect() failed: reason: " . strerror ($msgsock) . "\n";
break;
}
do {
$buf = '';
$ret = read ($msgsock, $buf, 2048);
if ($ret < 0) {
echo "read() failed: reason: " . strerror ($ret) . "\n";
break 2;
}
if ($ret == 0) {
break 2;
}
$buf = trim ($buf);
if ($buf == 'quit') {
close ($msgsock);
break 2;
}
$talkback = "PHP: You said '$buf'.\n";
write ($msgsock, $talkback, strlen ($talkback));
echo "$buf\n";
} while (true);
close ($msgsock);
} while (true);
close ($sock);
?>
</programlisting>
</example>
</para>
<para>
<example>
<title>Socket example: Simple TCP/IP client</title>
<para>
This example shows a simple, one-shot HTTP client. It simply
connects to a page, submits a HEAD request, echoes the reply,
and exits.
</para>
<programlisting>
<?php
error_reporting (E_ALL);
echo "<h2>TCP/IP Connection</h2>\n";
/* Get the port for the WWW service. */
$service_port = getservbyname ('www', 'tcp');
/* Get the IP address for the target host. */
$address = gethostbyname ('www.php.net');
/* Create a TCP/IP socket. */
$socket = socket (AF_INET, SOCK_STREAM, 0);
if ($socket < 0) {
echo "socket() failed: reason: " . strerror ($socket) . "\n";
} else {
"socket() successful: " . strerror ($socket) . "\n";
}
echo "Attempting to connect to '$address' on port '$service_port'...";
$result = connect ($socket, $address, $service_port);
if ($result < 0) {
echo "connect() failed.\nReason: ($result) " . strerror($result) . "\n";
} else {
echo "OK.\n";
}
$in = "HEAD / HTTP/1.0\r\n\r\n";
$out = '';
echo "Sending HTTP HEAD request...";
write ($socket, $in, strlen ($in));
echo "OK.\n";
echo "Reading response:\n\n";
while (read ($socket, $out, 2048)) {
echo $out;
}
echo "Closing socket...";
close ($socket);
echo "OK.\n\n";
?>
</programlisting>
</example>
</para>
</partintro>
<refentry id="function.accept-connect">
<refnamediv>
<refname>accept_connect</refname>
<refpurpose>Accepts a connection on a socket</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>int</type><methodname>accept_connect</methodname>
<methodparam><type>int</type><parameter>socket</parameter></methodparam>
</methodsynopsis>
<para>
After the socket <parameter>socket</parameter> has been created
using <function>socket</function>, bound to a name with
<function>bind</function>, and told to listen for connections
with <function>listen</function>, this function will accept
incoming connections on that socket. Once a successful connection
is made, a new socket descriptor is returned, which may be used
for communication. If there are multiple connections queued on
the socket, the first will be used. If there are no pending
connections, <function>accept_connect</function> will block until
a connection becomes present. If <parameter>socket</parameter>
has been made non-blocking using
<function>socket_set_blocking</function> or
<function>set_nonblock</function>, an error code will be
returned.
</para>
<para>
The socket descriptor returned by
<function>accept_connect</function> may not be used to accept new
connections. The original listening socket
<parameter>socket</parameter>, however, remains open and may be
reused.
</para>
<para>
Returns a new socket descriptor on success, or a negative error
code on failure. This code may be passed to
<function>strerror</function> to get a textual explanation of the
error.
</para>
<para>
See also
<function>bind</function>,
<function>connect</function>,
<function>listen</function>,
<function>socket</function>,
<function>socket_get_status</function>, and
<function>strerror</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.bind">
<refnamediv>
<refname>bind</refname>
<refpurpose>Binds a name to a socket</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>int</type><methodname>bind</methodname>
<methodparam><type>int</type><parameter>socket</parameter></methodparam>
<methodparam><type>string</type><parameter>address</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>port</parameter></methodparam>
</methodsynopsis>
<para>
<function>bind</function> binds the name given in
<parameter>address</parameter> to the socket described by
<parameter>socket</parameter>, which must be a valid socket
descriptor created with <function>socket</function>.
</para>
<para>
The <parameter>address</parameter> parameter is either an IP
address in dotted-quad notation
(e.g. <literal>127.0.0.1</literal>), if the socket is of the
<constant>AF_INET</constant> family; or the pathname of a
Unix-domain socket, if the socket family is
<constant>AF_UNIX</constant>.
</para>
<para>
The <parameter>port</parameter> parameter is only used when
connecting to an <constant>AF_INET</constant> socket, and
designates the port on the remote host to which a connection
should be made.
</para>
<para>
Returns zero on success, or a negative error code on
failure. This code may be passed to <function>strerror</function>
to get a textual explanation of the error.
</para>
<para>
See also
<function>accept_connect</function>,
<function>connect</function>,
<function>listen</function>,
<function>socket</function>,
<function>socket_get_status</function>, and
<function>strerror</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.close">
<refnamediv>
<refname>close</refname>
<refpurpose>Closes a file descriptor</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>bool</type><methodname>close</methodname>
<methodparam><type>int</type><parameter>socket</parameter></methodparam>
</methodsynopsis>
<para>
<function>close</function> closes the file (or socket) descriptor
given by <parameter>socket</parameter>.
</para>
<para>
Note that <function>close</function> should not be used on PHP
file descriptors created with <function>fopen</function>,
<function>popen</function>, <function>fsockopen</function>, or
<function>psockopen</function>; it is meant for sockets created
with <function>socket</function> or
<function>accept_connect</function>.
</para>
<para>
Returns &true; on success, or &false; if an error occurs (i.e.,
<parameter>socket</parameter> is invalid).
</para>
<para>
See also <function>bind</function>, <function>listen</function>,
<function>socket</function>,
<function>socket_get_status</function>, and
<function>strerror</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.connect">
<refnamediv>
<refname>connect</refname>
<refpurpose>Initiates a connection on a socket</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>int</type><methodname>connect</methodname>
<methodparam><type>int</type><parameter>socket</parameter></methodparam>
<methodparam><type>string</type><parameter>address</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>port</parameter></methodparam>
</methodsynopsis>
<para>
Initiates a connection using the socket descriptor
<parameter>socket</parameter>, which must be a valid socket
descriptor created with <function>socket</function>.
</para>
<para>
The <parameter>address</parameter> parameter is either an IP
address in dotted-quad notation
(e.g. <literal>127.0.0.1</literal>), if the socket is of the
<constant>AF_INET</constant> family; or the pathname of a
Unix-domain socket, if the socket family is
<constant>AF_UNIX</constant>.
</para>
<para>
The <parameter>port</parameter> parameter is only used when
connecting to an <constant>AF_INET</constant> socket, and
designates the port on the remote host to which a connection
should be made.
</para>
<para>
Returns zero on success, or a negative error code on
failure. This code may be passed to <function>strerror</function>
to get a textual explanation of the error.
</para>
<para>
See also
<function>bind</function>,
<function>listen</function>,
<function>socket</function>,
<function>socket_get_status</function>, and
<function>strerror</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.listen">
<refnamediv>
<refname>listen</refname>
<refpurpose>Listens for a connection on a socket</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>int</type><methodname>listen</methodname>
<methodparam><type>int</type><parameter>socket</parameter></methodparam>
<methodparam><type>int</type><parameter>backlog</parameter></methodparam>
</methodsynopsis>
<para>
After the socket <parameter>socket</parameter> has been created
using <function>socket</function> and bound to a name with
<function>bind</function>, it may be told to listen for incoming
connections on <parameter>socket</parameter>. A maximum of
<parameter>backlog</parameter> incoming connections will be
queued for processing.
</para>
<para>
<function>listen</function> is applicable only to sockets with
type <literal>SOCK_STREAM</literal> or
<literal>SOCK_SEQPACKET</literal>.
</para>
<para>
Returns zero on success, or a negative error code on
failure. This code may be passed to <function>strerror</function>
to get a textual explanation of the error.
</para>
<para>
See also
<function>accept_connect</function>,
<function>bind</function>,
<function>connect</function>,
<function>socket</function>,
<function>socket_get_status</function>, and
<function>strerror</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.read">
<refnamediv>
<refname>read</refname>
<refpurpose>Read from a socket</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>int</type><methodname>read</methodname>
<methodparam><type>int</type><parameter>socket_des</parameter></methodparam>
<methodparam><type>string</type><parameter>&buffer</parameter></methodparam>
<methodparam><type>int</type><parameter>length</parameter></methodparam>
</methodsynopsis>
<para>
The function <function>read</function> reads from socket
<parameter>socket_des</parameter>created by the
<function>accept_connect</function> function into
<parameter>&buffer</parameter> the number of bytes set by
<parameter>length</parameter>. Otherwise you can use \n, \t or \0 to
end reading. Returns number of bytes that have been read.
</para>
<para>
See also
<function>accept_connect</function>,
<function>bind</function>,
<function>connect</function>,
<function>listen</function>,
<function>strerror</function>,
<function>socket_get_status</function>. and
<function>write</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.socket">
<refnamediv>
<refname>socket</refname>
<refpurpose>Create a socket (endpoint for communication)</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>int</type><methodname>socket</methodname>
<methodparam><type>int</type><parameter>domain</parameter></methodparam>
<methodparam><type>int</type><parameter>type</parameter></methodparam>
<methodparam><type>int</type><parameter>protocol</parameter></methodparam>
</methodsynopsis>
<para>
Creates a communication endpoint (a socket), and returns a
descriptor to the socket.
</para>
<para>
The <parameter>domain</parameter> parameter sets the
domain. Currently, <constant>AF_INET</constant> and
<constant>AF_UNIX</constant> are understood.
</para>
<para>
The <parameter>type</parameter> parameter selects the socket
type. This is one of <constant>SOCK_STREAM</constant>,
<constant>SOCK_DGRAM</constant>,
<constant>SOCK_SEQPACKET</constant>,
<constant>SOCK_RAW</constant>, <constant>SOCK_RDM</constant>, or
<constant>SOCK_PACKET</constant>.
</para>
<para>
<parameter>protocol</parameter> sets the protocol.
</para>
<para>
Returns a valid socket descriptor on success, or a negative error
code on failure. This code may be passed to
<function>strerror</function> to get a textual explanation of the
error.
</para>
<para>
For more information on the usage of <function>socket</function>,
as well as on the meanings of the various parameters, see the
Unix man page socket (2).
</para>
<para>
See also
<function>accept_connect</function>,
<function>bind</function>,
<function>connect</function>,
<function>listen</function>,
<function>strerror</function>, and
<function>socket_get_status</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.strerror">
<refnamediv>
<refname>strerror</refname>
<refpurpose>Return a string describing a socket error</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>string</type><methodname>strerror</methodname>
<methodparam><type>int</type><parameter>errno</parameter></methodparam>
</methodsynopsis>
<para>
<function>strerror</function> takes as its
<parameter>errno</parameter> parameter the return value of one of
the socket functions, and returns the corresponding explanatory
text. This makes it a bit more pleasant to figure out why
something didn't work; for instance, instead of having to track
down a system include file to find out what '-111' means, you
just pass it to <function>strerror</function>, and it tells you
what happened.
</para>
<para>
<example>
<title><function>strerror</function> example</title>
<programlisting role="php">
<?php
if (($socket = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
echo "socket() failed: reason: " . strerror ($socket) . "\n";
}
if (($ret = bind ($socket, '127.0.0.1', 80)) < 0) {
echo "bind() failed: reason: " . strerror ($ret) . "\n";
}
?>
</programlisting>
<para>
The expected output from the above example (assuming the script
is not run with root privileges):
<screen>
bind() failed: reason: Permission denied
</screen>
</para>
</example>
</para>
<para>
See also
<function>accept_connect</function>,
<function>bind</function>,
<function>connect</function>,
<function>listen</function>,
<function>socket</function>, and
<function>socket_get_status</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.write">
<refnamediv>
<refname>write</refname>
<refpurpose>Write to a socket</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>int</type><methodname>write</methodname>
<methodparam><type>int</type><parameter>socket_des</parameter></methodparam>
<methodparam><type>string</type><parameter>&buffer</parameter></methodparam>
<methodparam><type>int</type><parameter>length</parameter></methodparam>
</methodsynopsis>
<para>
The function <function>write</function> writes to the socket
<parameter>socket_des</parameter> from
<parameter>&buffer</parameter> the number of bytes set by
<parameter>length</parameter>.
</para>
<para>
See also
<function>accept_connect</function>,
<function>bind</function>,
<function>connect</function>,
<function>listen</function>,
<function>read</function>,
<function>strerror</function>, and
<function>socket_get_status</function>.
</para>
</refsect1>
</refentry>
</reference>
<!-- 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:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
-->
|