PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/kdewebdev-3.5.10/php/ref.sockets.html

#
HTML | 1293 lines | 1275 code | 18 blank | 0 comment | 0 complexity | edf05fe11e218028b2545f5df9c2a90f MD5 | raw file
Possible License(s): AGPL-1.0, CC-BY-SA-3.0, GPL-2.0
  1. <HTML
  2. ><HEAD
  3. ><TITLE
  4. >Socket functions</TITLE
  5. ><META
  6. NAME="GENERATOR"
  7. CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
  8. REL="HOME"
  9. TITLE="PHP Manual"
  10. HREF="index.html"><LINK
  11. REL="UP"
  12. TITLE="Function Reference"
  13. HREF="funcref.html"><LINK
  14. REL="PREVIOUS"
  15. TITLE="snmpwalkoid"
  16. HREF="function.snmpwalkoid.html"><LINK
  17. REL="NEXT"
  18. TITLE="socket_accept"
  19. HREF="function.socket-accept.html"><META
  20. HTTP-EQUIV="Content-type"
  21. CONTENT="text/html; charset=ISO-8859-1"></HEAD
  22. ><BODY
  23. CLASS="reference"
  24. BGCOLOR="#FFFFFF"
  25. TEXT="#000000"
  26. LINK="#0000FF"
  27. VLINK="#840084"
  28. ALINK="#0000FF"
  29. ><DIV
  30. CLASS="NAVHEADER"
  31. ><TABLE
  32. SUMMARY="Header navigation table"
  33. WIDTH="100%"
  34. BORDER="0"
  35. CELLPADDING="0"
  36. CELLSPACING="0"
  37. ><TR
  38. ><TH
  39. COLSPAN="3"
  40. ALIGN="center"
  41. >PHP Manual</TH
  42. ></TR
  43. ><TR
  44. ><TD
  45. WIDTH="10%"
  46. ALIGN="left"
  47. VALIGN="bottom"
  48. ><A
  49. HREF="function.snmpwalkoid.html"
  50. ACCESSKEY="P"
  51. >Prev</A
  52. ></TD
  53. ><TD
  54. WIDTH="80%"
  55. ALIGN="center"
  56. VALIGN="bottom"
  57. ></TD
  58. ><TD
  59. WIDTH="10%"
  60. ALIGN="right"
  61. VALIGN="bottom"
  62. ><A
  63. HREF="function.socket-accept.html"
  64. ACCESSKEY="N"
  65. >Next</A
  66. ></TD
  67. ></TR
  68. ></TABLE
  69. ><HR
  70. ALIGN="LEFT"
  71. WIDTH="100%"></DIV
  72. ><DIV
  73. CLASS="reference"
  74. ><A
  75. NAME="ref.sockets"
  76. ></A
  77. ><DIV
  78. CLASS="TITLEPAGE"
  79. ><H1
  80. CLASS="title"
  81. >XCVII. Socket functions</H1
  82. ><DIV
  83. CLASS="PARTINTRO"
  84. ><A
  85. NAME="AEN86785"
  86. ></A
  87. ><DIV
  88. CLASS="section"
  89. ><H1
  90. CLASS="section"
  91. ><A
  92. NAME="sockets.intro"
  93. ></A
  94. >Introduction</H1
  95. ><P
  96. >&#13; The socket extension implements a low-level interface to the socket
  97. communication functions based on the popular BSD sockets, providing the
  98. possibility to act as a socket server as well as a client.
  99. </P
  100. ><P
  101. >&#13; For a more generic client-side socket interface, see
  102. <A
  103. HREF="function.fsockopen.html"
  104. ><B
  105. CLASS="function"
  106. >fsockopen()</B
  107. ></A
  108. > and
  109. <A
  110. HREF="function.pfsockopen.html"
  111. ><B
  112. CLASS="function"
  113. >pfsockopen()</B
  114. ></A
  115. >.
  116. </P
  117. ><P
  118. >&#13; When using these functions, it is important to remember that while
  119. many of them have identical names to their C counterparts, they
  120. often have different declarations. Please be sure to read the
  121. descriptions to avoid confusion.
  122. </P
  123. ><P
  124. >&#13; Those unfamiliar with socket programming can find a lot
  125. of useful material in the appropriate Unix man pages, and there is a great
  126. deal of tutorial information on socket programming in C on the web, much
  127. of which can be applied, with slight modifications, to socket programming
  128. in PHP. The <A
  129. HREF="http://www.developerweb.net/sock-faq/"
  130. TARGET="_top"
  131. >UNIX Socket
  132. FAQ</A
  133. > might be a good start.
  134. </P
  135. ><DIV
  136. CLASS="warning"
  137. ><P
  138. ></P
  139. ><TABLE
  140. CLASS="warning"
  141. BORDER="1"
  142. WIDTH="100%"
  143. ><TR
  144. ><TD
  145. ALIGN="CENTER"
  146. ><B
  147. >Warning</B
  148. ></TD
  149. ></TR
  150. ><TR
  151. ><TD
  152. ALIGN="LEFT"
  153. ><P
  154. >This extension is
  155. <SPAN
  156. CLASS="emphasis"
  157. ><I
  158. CLASS="emphasis"
  159. >EXPERIMENTAL</I
  160. ></SPAN
  161. >. The behaviour of this extension --
  162. including the names of its functions and anything else documented
  163. about this extension -- may change without notice in a future release of PHP.
  164. Use this extension at your own risk.</P
  165. ></TD
  166. ></TR
  167. ></TABLE
  168. ></DIV
  169. ></DIV
  170. ><DIV
  171. CLASS="section"
  172. ><H1
  173. CLASS="section"
  174. ><A
  175. NAME="sockets.requirements"
  176. ></A
  177. >Requirements</H1
  178. ><P
  179. >No external libraries are needed to build this extension.</P
  180. ></DIV
  181. ><DIV
  182. CLASS="section"
  183. ><H1
  184. CLASS="section"
  185. ><A
  186. NAME="sockets.installation"
  187. ></A
  188. >Installation</H1
  189. ><P
  190. >&#13; The socket functions described here are part of an extension to
  191. PHP which must be enabled at compile time by giving the <TT
  192. CLASS="option"
  193. >--enable-sockets</TT
  194. > option to
  195. <B
  196. CLASS="command"
  197. >configure</B
  198. >.
  199. </P
  200. ></DIV
  201. ><DIV
  202. CLASS="section"
  203. ><H1
  204. CLASS="section"
  205. ><A
  206. NAME="sockets.configuration"
  207. ></A
  208. >Runtime Configuration</H1
  209. ><P
  210. >This extension has no configuration directives defined in <TT
  211. CLASS="filename"
  212. >php.ini</TT
  213. >.</P
  214. ></DIV
  215. ><DIV
  216. CLASS="section"
  217. ><H1
  218. CLASS="section"
  219. ><A
  220. NAME="sockets.resources"
  221. ></A
  222. >Resource Types</H1
  223. ><P
  224. >This extension has no resource types defined.</P
  225. ></DIV
  226. ><DIV
  227. CLASS="section"
  228. ><H1
  229. CLASS="section"
  230. ><A
  231. NAME="sockets.constants"
  232. ></A
  233. >Predefined Constants</H1
  234. ><P
  235. >&#13;The constants below are defined by this extension, and
  236. will only be available when the extension has either
  237. been compiled into PHP or dynamically loaded at runtime.
  238. </P
  239. ><P
  240. ></P
  241. ><DIV
  242. CLASS="variablelist"
  243. ><DL
  244. ><DT
  245. ><TT
  246. CLASS="constant"
  247. ><B
  248. >AF_UNIX</B
  249. ></TT
  250. >
  251. (<A
  252. HREF="language.types.integer.html"
  253. >integer</A
  254. >)</DT
  255. ><DD
  256. ><P
  257. >&#13;
  258. </P
  259. ></DD
  260. ><DT
  261. ><TT
  262. CLASS="constant"
  263. ><B
  264. >AF_INET</B
  265. ></TT
  266. >
  267. (<A
  268. HREF="language.types.integer.html"
  269. >integer</A
  270. >)</DT
  271. ><DD
  272. ><P
  273. >&#13;
  274. </P
  275. ></DD
  276. ><DT
  277. ><TT
  278. CLASS="constant"
  279. ><B
  280. >SOCK_STREAM</B
  281. ></TT
  282. >
  283. (<A
  284. HREF="language.types.integer.html"
  285. >integer</A
  286. >)</DT
  287. ><DD
  288. ><P
  289. >&#13;
  290. </P
  291. ></DD
  292. ><DT
  293. ><TT
  294. CLASS="constant"
  295. ><B
  296. >SOCK_DGRAM</B
  297. ></TT
  298. >
  299. (<A
  300. HREF="language.types.integer.html"
  301. >integer</A
  302. >)</DT
  303. ><DD
  304. ><P
  305. >&#13;
  306. </P
  307. ></DD
  308. ><DT
  309. ><TT
  310. CLASS="constant"
  311. ><B
  312. >SOCK_RAW</B
  313. ></TT
  314. >
  315. (<A
  316. HREF="language.types.integer.html"
  317. >integer</A
  318. >)</DT
  319. ><DD
  320. ><P
  321. >&#13;
  322. </P
  323. ></DD
  324. ><DT
  325. ><TT
  326. CLASS="constant"
  327. ><B
  328. >SOCK_SEQPACKET</B
  329. ></TT
  330. >
  331. (<A
  332. HREF="language.types.integer.html"
  333. >integer</A
  334. >)</DT
  335. ><DD
  336. ><P
  337. >&#13;
  338. </P
  339. ></DD
  340. ><DT
  341. ><TT
  342. CLASS="constant"
  343. ><B
  344. >SOCK_RDM</B
  345. ></TT
  346. >
  347. (<A
  348. HREF="language.types.integer.html"
  349. >integer</A
  350. >)</DT
  351. ><DD
  352. ><P
  353. >&#13;
  354. </P
  355. ></DD
  356. ><DT
  357. ><TT
  358. CLASS="constant"
  359. ><B
  360. >MSG_OOB</B
  361. ></TT
  362. >
  363. (<A
  364. HREF="language.types.integer.html"
  365. >integer</A
  366. >)</DT
  367. ><DD
  368. ><P
  369. >&#13;
  370. </P
  371. ></DD
  372. ><DT
  373. ><TT
  374. CLASS="constant"
  375. ><B
  376. >MSG_WAITALL</B
  377. ></TT
  378. >
  379. (<A
  380. HREF="language.types.integer.html"
  381. >integer</A
  382. >)</DT
  383. ><DD
  384. ><P
  385. >&#13;
  386. </P
  387. ></DD
  388. ><DT
  389. ><TT
  390. CLASS="constant"
  391. ><B
  392. >MSG_PEEK</B
  393. ></TT
  394. >
  395. (<A
  396. HREF="language.types.integer.html"
  397. >integer</A
  398. >)</DT
  399. ><DD
  400. ><P
  401. >&#13;
  402. </P
  403. ></DD
  404. ><DT
  405. ><TT
  406. CLASS="constant"
  407. ><B
  408. >MSG_DONTROUTE</B
  409. ></TT
  410. >
  411. (<A
  412. HREF="language.types.integer.html"
  413. >integer</A
  414. >)</DT
  415. ><DD
  416. ><P
  417. >&#13;
  418. </P
  419. ></DD
  420. ><DT
  421. ><TT
  422. CLASS="constant"
  423. ><B
  424. >SO_DEBUG</B
  425. ></TT
  426. >
  427. (<A
  428. HREF="language.types.integer.html"
  429. >integer</A
  430. >)</DT
  431. ><DD
  432. ><P
  433. >&#13;
  434. </P
  435. ></DD
  436. ><DT
  437. ><TT
  438. CLASS="constant"
  439. ><B
  440. >SO_REUSEADDR</B
  441. ></TT
  442. >
  443. (<A
  444. HREF="language.types.integer.html"
  445. >integer</A
  446. >)</DT
  447. ><DD
  448. ><P
  449. >&#13;
  450. </P
  451. ></DD
  452. ><DT
  453. ><TT
  454. CLASS="constant"
  455. ><B
  456. >SO_KEEPALIVE</B
  457. ></TT
  458. >
  459. (<A
  460. HREF="language.types.integer.html"
  461. >integer</A
  462. >)</DT
  463. ><DD
  464. ><P
  465. >&#13;
  466. </P
  467. ></DD
  468. ><DT
  469. ><TT
  470. CLASS="constant"
  471. ><B
  472. >SO_DONTROUTE</B
  473. ></TT
  474. >
  475. (<A
  476. HREF="language.types.integer.html"
  477. >integer</A
  478. >)</DT
  479. ><DD
  480. ><P
  481. >&#13;
  482. </P
  483. ></DD
  484. ><DT
  485. ><TT
  486. CLASS="constant"
  487. ><B
  488. >SO_LINGER</B
  489. ></TT
  490. >
  491. (<A
  492. HREF="language.types.integer.html"
  493. >integer</A
  494. >)</DT
  495. ><DD
  496. ><P
  497. >&#13;
  498. </P
  499. ></DD
  500. ><DT
  501. ><TT
  502. CLASS="constant"
  503. ><B
  504. >SO_BROADCAST</B
  505. ></TT
  506. >
  507. (<A
  508. HREF="language.types.integer.html"
  509. >integer</A
  510. >)</DT
  511. ><DD
  512. ><P
  513. >&#13;
  514. </P
  515. ></DD
  516. ><DT
  517. ><TT
  518. CLASS="constant"
  519. ><B
  520. >SO_OOBINLINE</B
  521. ></TT
  522. >
  523. (<A
  524. HREF="language.types.integer.html"
  525. >integer</A
  526. >)</DT
  527. ><DD
  528. ><P
  529. >&#13;
  530. </P
  531. ></DD
  532. ><DT
  533. ><TT
  534. CLASS="constant"
  535. ><B
  536. >SO_SNDBUF</B
  537. ></TT
  538. >
  539. (<A
  540. HREF="language.types.integer.html"
  541. >integer</A
  542. >)</DT
  543. ><DD
  544. ><P
  545. >&#13;
  546. </P
  547. ></DD
  548. ><DT
  549. ><TT
  550. CLASS="constant"
  551. ><B
  552. >SO_RCVBUF</B
  553. ></TT
  554. >
  555. (<A
  556. HREF="language.types.integer.html"
  557. >integer</A
  558. >)</DT
  559. ><DD
  560. ><P
  561. >&#13;
  562. </P
  563. ></DD
  564. ><DT
  565. ><TT
  566. CLASS="constant"
  567. ><B
  568. >SO_SNDLOWAT</B
  569. ></TT
  570. >
  571. (<A
  572. HREF="language.types.integer.html"
  573. >integer</A
  574. >)</DT
  575. ><DD
  576. ><P
  577. >&#13;
  578. </P
  579. ></DD
  580. ><DT
  581. ><TT
  582. CLASS="constant"
  583. ><B
  584. >SO_RCVLOWAT</B
  585. ></TT
  586. >
  587. (<A
  588. HREF="language.types.integer.html"
  589. >integer</A
  590. >)</DT
  591. ><DD
  592. ><P
  593. >&#13;
  594. </P
  595. ></DD
  596. ><DT
  597. ><TT
  598. CLASS="constant"
  599. ><B
  600. >SO_SNDTIMEO</B
  601. ></TT
  602. >
  603. (<A
  604. HREF="language.types.integer.html"
  605. >integer</A
  606. >)</DT
  607. ><DD
  608. ><P
  609. >&#13;
  610. </P
  611. ></DD
  612. ><DT
  613. ><TT
  614. CLASS="constant"
  615. ><B
  616. >SO_RCVTIMEO</B
  617. ></TT
  618. >
  619. (<A
  620. HREF="language.types.integer.html"
  621. >integer</A
  622. >)</DT
  623. ><DD
  624. ><P
  625. >&#13;
  626. </P
  627. ></DD
  628. ><DT
  629. ><TT
  630. CLASS="constant"
  631. ><B
  632. >SO_TYPE</B
  633. ></TT
  634. >
  635. (<A
  636. HREF="language.types.integer.html"
  637. >integer</A
  638. >)</DT
  639. ><DD
  640. ><P
  641. >&#13;
  642. </P
  643. ></DD
  644. ><DT
  645. ><TT
  646. CLASS="constant"
  647. ><B
  648. >SO_ERROR</B
  649. ></TT
  650. >
  651. (<A
  652. HREF="language.types.integer.html"
  653. >integer</A
  654. >)</DT
  655. ><DD
  656. ><P
  657. >&#13;
  658. </P
  659. ></DD
  660. ><DT
  661. ><TT
  662. CLASS="constant"
  663. ><B
  664. >SOL_SOCKET</B
  665. ></TT
  666. >
  667. (<A
  668. HREF="language.types.integer.html"
  669. >integer</A
  670. >)</DT
  671. ><DD
  672. ><P
  673. >&#13;
  674. </P
  675. ></DD
  676. ><DT
  677. ><TT
  678. CLASS="constant"
  679. ><B
  680. >PHP_NORMAL_READ</B
  681. ></TT
  682. >
  683. (<A
  684. HREF="language.types.integer.html"
  685. >integer</A
  686. >)</DT
  687. ><DD
  688. ><P
  689. >&#13;
  690. </P
  691. ></DD
  692. ><DT
  693. ><TT
  694. CLASS="constant"
  695. ><B
  696. >PHP_BINARY_READ</B
  697. ></TT
  698. >
  699. (<A
  700. HREF="language.types.integer.html"
  701. >integer</A
  702. >)</DT
  703. ><DD
  704. ><P
  705. >&#13;
  706. </P
  707. ></DD
  708. ><DT
  709. ><TT
  710. CLASS="constant"
  711. ><B
  712. >SOL_TCP</B
  713. ></TT
  714. >
  715. (<A
  716. HREF="language.types.integer.html"
  717. >integer</A
  718. >)</DT
  719. ><DD
  720. ><P
  721. >&#13;
  722. </P
  723. ></DD
  724. ><DT
  725. ><TT
  726. CLASS="constant"
  727. ><B
  728. >SOL_UDP</B
  729. ></TT
  730. >
  731. (<A
  732. HREF="language.types.integer.html"
  733. >integer</A
  734. >)</DT
  735. ><DD
  736. ><P
  737. >&#13;
  738. </P
  739. ></DD
  740. ></DL
  741. ></DIV
  742. ></DIV
  743. ><DIV
  744. CLASS="section"
  745. ><H1
  746. CLASS="section"
  747. ><A
  748. NAME="sockets.errors"
  749. ></A
  750. >Socket Errors</H1
  751. ><P
  752. >&#13; The socket extension was written to provide a useable interface to the
  753. powerful BSD sockets. Care has been taken that the functions work equally
  754. well on Win32 and Unix implementations. Almost all of the sockets
  755. functions may fail under certain conditions and therefore emit an
  756. <TT
  757. CLASS="constant"
  758. ><B
  759. >E_WARNING</B
  760. ></TT
  761. > message describing the error. Sometimes this
  762. doesn't happen to the desire of the developer. For example the function
  763. <A
  764. HREF="function.socket-read.html"
  765. ><B
  766. CLASS="function"
  767. >socket_read()</B
  768. ></A
  769. > may suddenly emit an
  770. <TT
  771. CLASS="constant"
  772. ><B
  773. >E_WARNING</B
  774. ></TT
  775. > message because the connection broke
  776. unexpectedly. It's common to suppress the warning with the
  777. <TT
  778. CLASS="literal"
  779. >@</TT
  780. >-operator and catch the error code within the
  781. application with the <A
  782. HREF="function.socket-last-error.html"
  783. ><B
  784. CLASS="function"
  785. >socket_last_error()</B
  786. ></A
  787. > function. You
  788. may call the <A
  789. HREF="function.socket-strerror.html"
  790. ><B
  791. CLASS="function"
  792. >socket_strerror()</B
  793. ></A
  794. > function with this error
  795. code to retrieve a string describing the error. See their description for
  796. more information.
  797. </P
  798. ><DIV
  799. CLASS="note"
  800. ><BLOCKQUOTE
  801. CLASS="note"
  802. ><P
  803. ><B
  804. >Note: </B
  805. >
  806. The <TT
  807. CLASS="constant"
  808. ><B
  809. >E_WARNING</B
  810. ></TT
  811. > messages generated by the socket
  812. extension are in english though the retrieved error message will appear
  813. depending on the current locale (<TT
  814. CLASS="constant"
  815. ><B
  816. >LC_MESSAGES</B
  817. ></TT
  818. >):
  819. <TABLE
  820. BORDER="0"
  821. BGCOLOR="#E0E0E0"
  822. CELLPADDING="5"
  823. ><TR
  824. ><TD
  825. ><PRE
  826. CLASS="php"
  827. >Warning - socket_bind() unable to bind address [98]: Die Adresse wird bereits verwendet</PRE
  828. ></TD
  829. ></TR
  830. ></TABLE
  831. >
  832. </P
  833. ></BLOCKQUOTE
  834. ></DIV
  835. ></DIV
  836. ><DIV
  837. CLASS="section"
  838. ><H1
  839. CLASS="section"
  840. ><A
  841. NAME="sockets.examples"
  842. ></A
  843. >Examples</H1
  844. ><P
  845. >&#13; <TABLE
  846. WIDTH="100%"
  847. BORDER="0"
  848. CELLPADDING="0"
  849. CELLSPACING="0"
  850. CLASS="EXAMPLE"
  851. ><TR
  852. ><TD
  853. ><DIV
  854. CLASS="example"
  855. ><A
  856. NAME="AEN87020"
  857. ></A
  858. ><P
  859. ><B
  860. >Example 1. Socket example: Simple TCP/IP server</B
  861. ></P
  862. ><P
  863. >&#13; This example shows a simple talkback server. Change the
  864. <TT
  865. CLASS="varname"
  866. >address</TT
  867. > and <TT
  868. CLASS="varname"
  869. >port</TT
  870. > variables
  871. to suit your setup and execute. You may then connect to the
  872. server with a command similar to: <B
  873. CLASS="command"
  874. >telnet 192.168.1.53
  875. 10000</B
  876. > (where the address and port match your
  877. setup). Anything you type will then be output on the server
  878. side, and echoed back to you. To disconnect, enter 'quit'.
  879. </P
  880. ><TABLE
  881. BORDER="0"
  882. BGCOLOR="#E0E0E0"
  883. CELLPADDING="5"
  884. ><TR
  885. ><TD
  886. ><PRE
  887. CLASS="php"
  888. >#!/usr/local/bin/php -q
  889. &#60;?php
  890. error_reporting (E_ALL);
  891. /* Allow the script to hang around waiting for connections. */
  892. set_time_limit (0);
  893. /* Turn on implicit output flushing so we see what we're getting
  894. * as it comes in. */
  895. ob_implicit_flush ();
  896. $address = '192.168.1.53';
  897. $port = 10000;
  898. if (($sock = socket_create (AF_INET, SOCK_STREAM, 0)) &#60; 0) {
  899. echo "socket_create() failed: reason: " . socket_strerror ($sock) . "\n";
  900. }
  901. if (($ret = socket_bind ($sock, $address, $port)) &#60; 0) {
  902. echo "socket_bind() failed: reason: " . socket_strerror ($ret) . "\n";
  903. }
  904. if (($ret = socket_listen ($sock, 5)) &#60; 0) {
  905. echo "socket_listen() failed: reason: " . socket_strerror ($ret) . "\n";
  906. }
  907. do {
  908. if (($msgsock = socket_accept($sock)) &#60; 0) {
  909. echo "socket_accept() failed: reason: " . socket_strerror ($msgsock) . "\n";
  910. break;
  911. }
  912. /* Send instructions. */
  913. $msg = "\nWelcome to the PHP Test Server. \n" .
  914. "To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
  915. socket_write($msgsock, $msg, strlen($msg));
  916. do {
  917. if (FALSE === ($buf = socket_read ($msgsock, 2048))) {
  918. echo "socket_read() failed: reason: " . socket_strerror ($ret) . "\n";
  919. break 2;
  920. }
  921. if (!$buf = trim ($buf)) {
  922. continue;
  923. }
  924. if ($buf == 'quit') {
  925. break;
  926. }
  927. if ($buf == 'shutdown') {
  928. socket_close ($msgsock);
  929. break 2;
  930. }
  931. $talkback = "PHP: You said '$buf'.\n";
  932. socket_write ($msgsock, $talkback, strlen ($talkback));
  933. echo "$buf\n";
  934. } while (true);
  935. socket_close ($msgsock);
  936. } while (true);
  937. socket_close ($sock);
  938. ?&#62;</PRE
  939. ></TD
  940. ></TR
  941. ></TABLE
  942. ></DIV
  943. ></TD
  944. ></TR
  945. ></TABLE
  946. >
  947. </P
  948. ><P
  949. >&#13; <TABLE
  950. WIDTH="100%"
  951. BORDER="0"
  952. CELLPADDING="0"
  953. CELLSPACING="0"
  954. CLASS="EXAMPLE"
  955. ><TR
  956. ><TD
  957. ><DIV
  958. CLASS="example"
  959. ><A
  960. NAME="AEN87028"
  961. ></A
  962. ><P
  963. ><B
  964. >Example 2. Socket example: Simple TCP/IP client</B
  965. ></P
  966. ><P
  967. >&#13; This example shows a simple, one-shot HTTP client. It simply
  968. connects to a page, submits a HEAD request, echoes the reply,
  969. and exits.
  970. </P
  971. ><TABLE
  972. BORDER="0"
  973. BGCOLOR="#E0E0E0"
  974. CELLPADDING="5"
  975. ><TR
  976. ><TD
  977. ><PRE
  978. CLASS="programlisting"
  979. >&#60;?php
  980. error_reporting (E_ALL);
  981. echo "&#60;h2&#62;TCP/IP Connection&#60;/h2&#62;\n";
  982. /* Get the port for the WWW service. */
  983. $service_port = getservbyname ('www', 'tcp');
  984. /* Get the IP address for the target host. */
  985. $address = gethostbyname ('www.example.com');
  986. /* Create a TCP/IP socket. */
  987. $socket = socket_create (AF_INET, SOCK_STREAM, 0);
  988. if ($socket &#60; 0) {
  989. echo "socket_create() failed: reason: " . socket_strerror ($socket) . "\n";
  990. } else {
  991. echo "OK.\n";
  992. }
  993. echo "Attempting to connect to '$address' on port '$service_port'...";
  994. $result = socket_connect ($socket, $address, $service_port);
  995. if ($result &#60; 0) {
  996. echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n";
  997. } else {
  998. echo "OK.\n";
  999. }
  1000. $in = "HEAD / HTTP/1.0\r\n\r\n";
  1001. $out = '';
  1002. echo "Sending HTTP HEAD request...";
  1003. socket_write ($socket, $in, strlen ($in));
  1004. echo "OK.\n";
  1005. echo "Reading response:\n\n";
  1006. while ($out = socket_read ($socket, 2048)) {
  1007. echo $out;
  1008. }
  1009. echo "Closing socket...";
  1010. socket_close ($socket);
  1011. echo "OK.\n\n";
  1012. ?&#62;</PRE
  1013. ></TD
  1014. ></TR
  1015. ></TABLE
  1016. ></DIV
  1017. ></TD
  1018. ></TR
  1019. ></TABLE
  1020. >
  1021. </P
  1022. ></DIV
  1023. ></DIV
  1024. ><DIV
  1025. CLASS="TOC"
  1026. ><DL
  1027. ><DT
  1028. ><B
  1029. >Table of Contents</B
  1030. ></DT
  1031. ><DT
  1032. ><A
  1033. HREF="function.socket-accept.html"
  1034. >socket_accept</A
  1035. >&nbsp;--&nbsp;Accepts a connection on a socket</DT
  1036. ><DT
  1037. ><A
  1038. HREF="function.socket-bind.html"
  1039. >socket_bind</A
  1040. >&nbsp;--&nbsp;Binds a name to a socket</DT
  1041. ><DT
  1042. ><A
  1043. HREF="function.socket-clear-error.html"
  1044. >socket_clear_error</A
  1045. >&nbsp;--&nbsp;Clears the error on the socket or the last error code</DT
  1046. ><DT
  1047. ><A
  1048. HREF="function.socket-close.html"
  1049. >socket_close</A
  1050. >&nbsp;--&nbsp;Closes a socket resource</DT
  1051. ><DT
  1052. ><A
  1053. HREF="function.socket-connect.html"
  1054. >socket_connect</A
  1055. >&nbsp;--&nbsp;Initiates a connection on a socket</DT
  1056. ><DT
  1057. ><A
  1058. HREF="function.socket-create-listen.html"
  1059. >socket_create_listen</A
  1060. >&nbsp;--&nbsp;Opens a socket on port to accept connections </DT
  1061. ><DT
  1062. ><A
  1063. HREF="function.socket-create-pair.html"
  1064. >socket_create_pair</A
  1065. >&nbsp;--&nbsp;Creates a pair of indistinguishable sockets and stores them in fds. </DT
  1066. ><DT
  1067. ><A
  1068. HREF="function.socket-create.html"
  1069. >socket_create</A
  1070. >&nbsp;--&nbsp;Create a socket (endpoint for communication)</DT
  1071. ><DT
  1072. ><A
  1073. HREF="function.socket-get-option.html"
  1074. >socket_get_option</A
  1075. >&nbsp;--&nbsp;Gets socket options for the socket </DT
  1076. ><DT
  1077. ><A
  1078. HREF="function.socket-getpeername.html"
  1079. >socket_getpeername</A
  1080. >&nbsp;--&nbsp;
  1081. Queries the remote side of the given socket which may either result in host/port
  1082. or in a UNIX filesystem path, dependent on its type.
  1083. </DT
  1084. ><DT
  1085. ><A
  1086. HREF="function.socket-getsockname.html"
  1087. >socket_getsockname</A
  1088. >&nbsp;--&nbsp;
  1089. Queries the local side of the given socket which may either result in host/port
  1090. or in a UNIX filesystem path, dependent on its type.
  1091. </DT
  1092. ><DT
  1093. ><A
  1094. HREF="function.socket-iovec-add.html"
  1095. >socket_iovec_add</A
  1096. >&nbsp;--&nbsp;Adds a new vector to the scatter/gather array </DT
  1097. ><DT
  1098. ><A
  1099. HREF="function.socket-iovec-alloc.html"
  1100. >socket_iovec_alloc</A
  1101. >&nbsp;--&nbsp;...]) Builds a 'struct iovec' for use with sendmsg, recvmsg, writev, and readv </DT
  1102. ><DT
  1103. ><A
  1104. HREF="function.socket-iovec-delete.html"
  1105. >socket_iovec_delete</A
  1106. >&nbsp;--&nbsp;Deletes a vector from an array of vectors </DT
  1107. ><DT
  1108. ><A
  1109. HREF="function.socket-iovec-fetch.html"
  1110. >socket_iovec_fetch</A
  1111. >&nbsp;--&nbsp;Returns the data held in the iovec specified by iovec_id[iovec_position] </DT
  1112. ><DT
  1113. ><A
  1114. HREF="function.socket-iovec-free.html"
  1115. >socket_iovec_free</A
  1116. >&nbsp;--&nbsp;Frees the iovec specified by iovec_id </DT
  1117. ><DT
  1118. ><A
  1119. HREF="function.socket-iovec-set.html"
  1120. >socket_iovec_set</A
  1121. >&nbsp;--&nbsp;Sets the data held in iovec_id[iovec_position] to new_val </DT
  1122. ><DT
  1123. ><A
  1124. HREF="function.socket-last-error.html"
  1125. >socket_last_error</A
  1126. >&nbsp;--&nbsp;Returns the last error on the socket </DT
  1127. ><DT
  1128. ><A
  1129. HREF="function.socket-listen.html"
  1130. >socket_listen</A
  1131. >&nbsp;--&nbsp;Listens for a connection on a socket</DT
  1132. ><DT
  1133. ><A
  1134. HREF="function.socket-read.html"
  1135. >socket_read</A
  1136. >&nbsp;--&nbsp;Reads a maximum of length bytes from a socket</DT
  1137. ><DT
  1138. ><A
  1139. HREF="function.socket-readv.html"
  1140. >socket_readv</A
  1141. >&nbsp;--&nbsp;Reads from an fd, using the scatter-gather array defined by iovec_id </DT
  1142. ><DT
  1143. ><A
  1144. HREF="function.socket-recv.html"
  1145. >socket_recv</A
  1146. >&nbsp;--&nbsp;Receives data from a connected socket </DT
  1147. ><DT
  1148. ><A
  1149. HREF="function.socket-recvfrom.html"
  1150. >socket_recvfrom</A
  1151. >&nbsp;--&nbsp;Receives data from a socket, connected or not </DT
  1152. ><DT
  1153. ><A
  1154. HREF="function.socket-recvmsg.html"
  1155. >socket_recvmsg</A
  1156. >&nbsp;--&nbsp;Used to receive messages on a socket, whether connection-oriented or not </DT
  1157. ><DT
  1158. ><A
  1159. HREF="function.socket-select.html"
  1160. >socket_select</A
  1161. >&nbsp;--&nbsp;Runs the select() system call on the given arrays of sockets with a timeout specified by tv_sec and tv_usec </DT
  1162. ><DT
  1163. ><A
  1164. HREF="function.socket-send.html"
  1165. >socket_send</A
  1166. >&nbsp;--&nbsp;Sends data to a connected socket </DT
  1167. ><DT
  1168. ><A
  1169. HREF="function.socket-sendmsg.html"
  1170. >socket_sendmsg</A
  1171. >&nbsp;--&nbsp;Sends a message to a socket, regardless of whether it is connection-oriented or not </DT
  1172. ><DT
  1173. ><A
  1174. HREF="function.socket-sendto.html"
  1175. >socket_sendto</A
  1176. >&nbsp;--&nbsp;Sends a message to a socket, whether it is connected or not </DT
  1177. ><DT
  1178. ><A
  1179. HREF="function.socket-set-nonblock.html"
  1180. >socket_set_nonblock</A
  1181. >&nbsp;--&nbsp;Sets nonblocking mode for file descriptor fd </DT
  1182. ><DT
  1183. ><A
  1184. HREF="function.socket-set-option.html"
  1185. >socket_set_option</A
  1186. >&nbsp;--&nbsp;Sets socket options for the socket</DT
  1187. ><DT
  1188. ><A
  1189. HREF="function.socket-shutdown.html"
  1190. >socket_shutdown</A
  1191. >&nbsp;--&nbsp;Shuts down a socket for receiving, sending, or both. </DT
  1192. ><DT
  1193. ><A
  1194. HREF="function.socket-strerror.html"
  1195. >socket_strerror</A
  1196. >&nbsp;--&nbsp;Return a string describing a socket error</DT
  1197. ><DT
  1198. ><A
  1199. HREF="function.socket-write.html"
  1200. >socket_write</A
  1201. >&nbsp;--&nbsp;Write to a socket</DT
  1202. ><DT
  1203. ><A
  1204. HREF="function.socket-writev.html"
  1205. >socket_writev</A
  1206. >&nbsp;--&nbsp;Writes to a file descriptor, fd, using the scatter-gather array defined by iovec_id </DT
  1207. ></DL
  1208. ></DIV
  1209. ></DIV
  1210. ></DIV
  1211. ><DIV
  1212. CLASS="NAVFOOTER"
  1213. ><HR
  1214. ALIGN="LEFT"
  1215. WIDTH="100%"><TABLE
  1216. SUMMARY="Footer navigation table"
  1217. WIDTH="100%"
  1218. BORDER="0"
  1219. CELLPADDING="0"
  1220. CELLSPACING="0"
  1221. ><TR
  1222. ><TD
  1223. WIDTH="33%"
  1224. ALIGN="left"
  1225. VALIGN="top"
  1226. ><A
  1227. HREF="function.snmpwalkoid.html"
  1228. ACCESSKEY="P"
  1229. >Prev</A
  1230. ></TD
  1231. ><TD
  1232. WIDTH="34%"
  1233. ALIGN="center"
  1234. VALIGN="top"
  1235. ><A
  1236. HREF="index.html"
  1237. ACCESSKEY="H"
  1238. >Home</A
  1239. ></TD
  1240. ><TD
  1241. WIDTH="33%"
  1242. ALIGN="right"
  1243. VALIGN="top"
  1244. ><A
  1245. HREF="function.socket-accept.html"
  1246. ACCESSKEY="N"
  1247. >Next</A
  1248. ></TD
  1249. ></TR
  1250. ><TR
  1251. ><TD
  1252. WIDTH="33%"
  1253. ALIGN="left"
  1254. VALIGN="top"
  1255. >snmpwalkoid</TD
  1256. ><TD
  1257. WIDTH="34%"
  1258. ALIGN="center"
  1259. VALIGN="top"
  1260. ><A
  1261. HREF="funcref.html"
  1262. ACCESSKEY="U"
  1263. >Up</A
  1264. ></TD
  1265. ><TD
  1266. WIDTH="33%"
  1267. ALIGN="right"
  1268. VALIGN="top"
  1269. >socket_accept</TD
  1270. ></TR
  1271. ></TABLE
  1272. ></DIV
  1273. ></BODY
  1274. ></HTML
  1275. >