Implement client-to-server communication with PHP sockets

Source: Internet
Author: User

Service side

<?phperror_reporting (E_all); set_time_limit (0); Ob_implicit_flush ();//local ip$address = ' localhost ';//Set up with 111 port for communication $port = 111;//Create socketif (($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 for 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"; $msg = "\nphp Test Server. \ n "." With Quit,shutdown,sun ... Wait for the command to test. \ n "; @socket_write ($msgsock, $msg, strlen ($msg));d o {if (false = = = ($buf = @socket_read ($msgsock, 2048, Php_normal_ READ)) {echo "Socket_read () Failed:reason:". 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);? >

Client code

<?phperror_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) {echo "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
client.php
server.php
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 ....

  

Implement client-to-server communication with PHP sockets

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.