PHP Socket Network Programming Example 
At the beginning of the socket is not very understanding, do things very vague, and then do the positioning of the request of the project, the socket to understand more clearly.
Paste the sample code:
Server-side PHP code <?php//ensures that the client is not timed out set_time_limit (0);    Set IP and port number $address = ' 127.0.0.1 ';    $port = 2009;       When debugging, you can change the port to test the program. Create a socketif(($sock =socket_create (af_inet,sock_stream,sol_tcp)) <0) {echo ' socket_create () failed because of: ". Socket_strerror ($sock)."    <br> "; //bind to socket portif(($ret =socket_bind ($sock, $address, $port)) <0) {echo ' Socket_bind () failed because of: ". Socket_strerror ($ret)."    <br> "; }//Start listeningif(($ret =socket_listen ($sock, 4)) <0) {echo ' Socket_listen () failed because of: ". Socket_strerror ($ret)."    <br> "; }Todo{if(($msgsock = socket_accept ($sock)) < 0) {echo "socket_accept () Failed:reason:". Socket_strerror ($msgsock). "/n"; Break;   }      //Send to client    $msg  = "<font color=red> Welcome to the server. </font><br> ";    socket_write ($msgsock,  $msg,  strlen ($msg));       echo  "read the information from the client <br>";    $buf  = socket_read ($msgsock, 8192);          $talkback  =  "received information: $buf <br>";    echo  $talkback;   /* if (Socket_write ($msgsock,  $talkback,  strlen ($talkback)) <0)         {        echo  "Socket_write ()  failed:  reason:  " . socket_strerror ($msgsock)  . "/n ";                else         {        echo  "send Success";        }*/         //ECHO&NBSP; $buf;    Socket_close ($msgsock);   }  while(true);    Socket_close ($sock); ?>
 
<?php//ensures that the client is not timed out set_time_limit (0);
Set IP and port number $address = ' 127.0.0.1 ';    $port = 2009;
When debugging, you can change the port to test the program. Create a socket if (($sock =socket_create (af_inet,sock_stream,sol_tcp)) <0) {echo socket_create () The reason for the failure is: ". socket_ Strerror ($sock). "
<br> "; }//binding to the Socket port if (($ret =socket_bind ($sock, $address, $port)) <0 {echo "socket_bind () the reason for the failure is:". Socket_strerror ($ ret). "
<br> "; ($ret =socket_listen ($sock, 4)) <0 {echo "Socket_listen () failed because of:". Socket_strerror ($ret). "
<br> "; do {if ($msgsock = socket_accept ($sock)) < 0) {echo socket_accept () Failed:reason: ". Socket_strerror ($msgsock).
"/n";
Break //Send to client $msg = "<font color=red> Welcome to the server.
</font><br> ";
Socket_write ($msgsock, $msg, strlen ($msg));
echo "reads the information from the client <br>";
$buf = Socket_read ($msgsock, 8192);
$talkback = "Received information: $buf <br>";
Echo $talkback; /* IF (Socket_write ($msgsock, $talkback, strlen ($talkback)) <0) {echo socket_write () Failed:reason: " . Socket_strerror ($msgsock).
     "/n";
     else {echo "sent successfully";
}*///echo $buf;
Socket_close ($msgsock);
} while (true); Socket_close ($sock);?> 
 
Client-side PHP code <?php error_reporting (E_all);    Set_time_limit (0);       echo "if($socket < 0) {echo "socket_create () Failed:reason:". Socket_strerror ($socket).    "/n"; }Else{echo "ok./n";    echo "Attempts to connect the ' $address ' Port ' $service _port ' ...<br> '; $result = Socket_connect ($socket, $address, $service _port);if($result < 0) {echo "Socket_connect () Failed./nreason: ($result)". Socket_strerror ($result).    "/n"; }Else{echo "Connect ok<br>";    } $in = "hello/r/n";    $in. = "I am chester/r/n";    $out = '; $out 1 = ';if(!socket_write ($socket, $in, strlen ($in)) {echo "Socket_write () Failed:reason:". Socket_strerror ($socket).    "/n"; }Else{echo sent the server information successfully.    <br> ";    echo "Sends the content: <font color= ' red ' > $in </font> <br>"; } while($out = Socket_read ($socket, 8192)) {Echo received the server to return information successfully.    <br> ";    echo "accepted the content as:", $out;    echo "Close socket...<br>";    Socket_close ($socket);    echo "Close ok<br>"; ?>
 
<?php error_reporting (E_all);
Set_time_limit (0);
echo " 
Run server.php First
Run client.php again