PHP sockent Communication

Source: Internet
Author: User

1.php Service side: server.php

<?PHP//Ensure that the client is not timed out when connectingSet_time_limit(0);$ip= ' 127.0.0.1 ';$port= 1935;/*+-------------------------------* @socket communication throughout the process +-------------------------------* @socket_create * @socke T_bind * @socket_listen * @socket_accept * @socket_read * @socket_write * @socket_close +------------------ -------------- *//*----------------The following actions are-------------------on the manual*/if(($sock= Socket_create (af_inet,sock_stream,sol_tcp)) < 0) {    EchoThe reason for the socket_create () failure is: ". Socket_strerror ($sock)." \ n ";}if(($ret= Socket_bind ($sock,$ip,$port)) < 0) {    EchoThe reason for the Socket_bind () failure is: ". Socket_strerror ($ret)." \ n ";}if(($ret= Socket_listen ($sock, 4)) < 0) {    EchoThe reason for the Socket_listen () failure is: ". Socket_strerror ($ret)." \ n ";}$count= 0; Do {    if(($msgsock= Socket_accept ($sock)) < 0) {        Echo"Socket_accept () Failed:reason:". Socket_strerror ($msgsock) . "\ n";  Break; } Else {                //Send to client        $msg= "Test succeeded!" \ n "; Socket_write ($msgsock,$msg,strlen($msg)); Echo"The test was successful. \ n"; $buf= Socket_read ($msgsock, 8192); $talkback= "Information Received:$buf\ n "; Echo $talkback; if(++$count>= 5){             Break;                }; }    //echo $buf;Socket_close ($msgsock);}  while(true); Socket_close ($sock);?>

2. Client:

client.php

<?PHPerror_reporting(E_all);Set_time_limit(0);Echo";$port= 1935;$ip= "127.0.0.1";/*+-------------------------------* @socket connect the whole process +-------------------------------* @socket_create * @socket _connect * @socket_write * @socket_read * @socket_close +--------------------------------*/$socket= Socket_create (Af_inet, Sock_stream,sol_tcp);if($socket< 0) {    Echo"Socket_create () Failed:reason:". Socket_strerror ($socket) . "\ n";}Else {    Echo"Ok.\n";}Echo"Trying to connect"$ipPort$port' ... \ n ';$result= Socket_connect ($socket,$ip,$port);if($result< 0) {    Echo"Socket_connect () Failed.\nreason: ($result) " . Socket_strerror ($result) . "\ n";}Else {    Echo"Connect ok\n";}$in= "ho\r\n";$in. = "First blood\r\n";$out= ' ';if(!socket_write ($socket,$in,strlen($in))) {    Echo"Socket_write () Failed:reason:". Socket_strerror ($socket) . "\ n";}Else {    Echo"Send message to server successfully!" \ n "; Echo"The content sent is: <font color= ' Red ' >$in</font> <br> ";} while($out= Socket_read ($socket, 8192)) {    Echo"The receiving server backhaul information is successful!" \ n "; Echo"The accepted content is:",$out;}Echo"Close socket...\n"; Socket_close ($socket);Echo"Close ok\n";?>

First run under cmd: PHP server.php file, he will be in a waiting state,

You can see the bound ports and run:

Run under cmd: PHP client.php

PHP sockent Communication

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.