PHP Socket Example

Source: Internet
Author: User
PHP Socket Instance
Here is an example of a PHP socket programming, of course, you can also use Fsockopen
 TCP/IP Connection\ n ";/* Get the port for the WWW service. */$service _port = getservbyname (' www ', ' tcp ');/* Get the IP address for the target host. */$address = gethostbyname (' www.example.com ');/* Create a TCP/IP socket.  */$socket = Socket_create (Af_inet, Sock_stream, sol_tcp), if ($socket = = = False) {echo "socket_create () Failed:reason: " . Socket_strerror (Socket_last_error ()). "\ n";} else {echo "ok.\n";} echo "Attempting to connect to ' $address ' on port ' $service _port ' ..."; $result = Socket_connect ($socket, $address, $service _port), if ($result = = = False) {echo "Socket_connect () Failed.\nreason: ($result)". Socket_strerror (Socket_last_error ($socket)). "\ n";} else {echo "ok.\n";} $in = "head/http/1.1\r\n"; $in. = "host:www.example.com\r\n"; $in. = "connection:close\r\n\r\n"; $out = "; echo" sending HTTP HEAD request ... "; Socket_write ($socket, $in, strlen ($in)); echo" ok.\n "; echo" Reading response:\n\n "; $buf = ' This is M Y buffer. '; if (false!== ($bytes = Socket_recv ($socket, $buf, 2048, MSg_waitall)) {echo "Read $bytes bytes from Socket_recv (). Closing socket ... ";} else {echo "socket_recv () failed; Reason: ". Socket_strerror (Socket_last_error ($socket)). "\ n";} Socket_close ($socket); Echo $buf. "\ n"; echo "ok.\n\n";?  >
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.