The socket in PHP implements a client-to-server communication example

Source: Internet
Author: User
This article mainly introduces the use of PHP socket to implement client-to-server communication instances of the detailed, with a certain reference value, interested in small partners can refer to.

First, server.php service side:

<?php error_reporting (E_all); Set_time_limit (0); Ob_implicit_flush (); Local IP $address = ' localhost '; Set up 111 port for communication $port = 111; Create socket if ($sock = Socket_create (Af_inet, Sock_stream, sol_tcp) < 0) {echo "Socket creation failure Reason". Socket_strerror ($sock). "\ n"; if ($ret = Socket_bind ($sock, $address, $port)) < 0) {echo "Create socket Failure Reason". Socket_strerror ($ret). "\ n"; }//Listen if (($ret = Socket_listen ($sock, 5)) < 0) {echo "Listener failure Reason". Socket_strerror ($ret). "\ n"; } do {//Receive command if (($msgsock = @socket_accept ($sock)) < 0) {echo "command received failure reason:". Socket_strerror ($msgsock).     "\ n";   Break } $msg = "\nphp Test Server. \ n "." With Quit,shutdown,sun ...    Wait for the command to test. \ n ";    @socket_write ($msgsock, $msg, strlen ($msg)); do {if (false = = = ($buf = @socket_read ($msgsock, 2048, Php_normal_read)) {echo "Socket_read () Failed:reaso N: ". Socket_strerror ($ret).         "\ n";     Break 2;     } if (! $buf = Trim ($buf)) {continue; } if ($buf == ' quit ') {break;         } if ($buf = = ' shutdown ') {socket_close ($msgsock);     Break 2;     } if ($buf = = ' Sun ') {echo ' What is you doing? ';     } $talkback = "backinformation: ' $buf '. \ n";     Socket_write ($msgsock, $talkback, strlen ($talkback));   echo "$BUF \ n";    } while (true);  Socket_close ($msgsock);  } while (true); Socket_close ($sock);?>

Second, client.php client:

<?php error_reporting (E_all); Port 111 $service _port = 111; Local $address = ' localhost '; Create TCP/IP socket $socket = socket_create (Af_inet, Sock_stream, sol_tcp); if ($socket < 0) {     echo "Socket Creation failure Reason:". Socket_strerror ($socket). "\ n"; } else {     echo "Ok,he he.\n";} $result = Socket_connect ($socket, $address, $service _port); if ($result < 0) {     E Cho "Socket Connection failure Reason: ($result)". Socket_strerror ($result). "\ n";  } else {     echo "ok.\n";}//Send command $in = "head/http/1.1\r\n"; $in. = "connection:close\r\n\r\n"; $out = "; echo" send Command ... ";"; $in = "sun\n"; Socket_write ($socket, $in, strlen ($in)); echo "ok.\n"; echo "Reading backinformatin:\n\n"; while ($out = Socket_read ($socket, 2048)) {     echo $out;} echo "Close socket ..."; Socket_close ($socket); echo "Ok,he he.\n\n";?>

These two pieces of code is a simple introduction to the use of PHP to implement client and server communication

Specific operation, under DOS with the PHP command to open the server, in the listening state.

Open a DOS window, open the client with PHP command, will get the response from the server ....

The above is the whole content of this article, I hope that everyone's study has helped.


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.