Introduction: This is a detailed page for PHP socket instances. It introduces the knowledge, skills, experience and some PHP Source Code related to PhP, PHP, socket, and code PHP socket instances.
Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 345994 'rolling = 'no'>
The following is an example of PHP socket programming. You can also use fsockopen.
<? Phperror_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 .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"; $ out = ''; echo" sending HTTP head request... "; socket_write ($ socket, $ in, strlen ($ in); echo" OK. \ n "; echo" reading response: \ n "; $ Buf = 'This is my buffer. '; If (false! = ($ Bytes = socket_recv ($ socket, $ Buf, 2048, msg_waitall) {echo "read $ 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 ";?>
Love J2EE follow Java Michael Jackson video station JSON online tools
Http://biancheng.dnbcw.info/php/345994.html pageno: 4.