Home >Backend Development >PHP Tutorial >Socket request and receive examples
Client
error_reporting(E_ALL);
set_time_limit(0);
echo "
?>
Server side
//Ensure that there will be no timeout when connecting to the client
set_time_limit(0);
$ip = '127.0.0.1';
$port = 1935;
/*
+--- ----------------------------
* @The whole process of socket communication
+------------- ------------------
* * @socket_create
* * @socket_bind
* * @socket_listen
* * @socket_accept
* * @socket_read
* * @socket_write
* * @socket_close
+-- ----------------------------------
*/
/*-------------- -- The following operations are in the manual -------------------*/
if(($sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP)) < 0) {
echo "socket_create() failed because: ".socket_strerror($sock)."n";
}
if(($ret = socket_bind($sock,$ip,$port)) < 0) {
echo "socket_bind() failed because: ".socket_strerror($ret)."n";
}
if(($ret = socket_listen($sock,4)) < 0) {
echo "socket_listen( ) The reason for failure is: ".socket_strerror($ret)."n";
}
$count = 0;
do {
if (($msgsock = socket_accept($sock)) < 0) {
echo " Socket_accept () failed: release: ". Socket_Strerror ($ msgsock)." n ";
Break;
} else {
// See it to the client
$ msg =" See, your name is! n";
socket_write($msgsock, $msg, strlen($msg)); bufn";
echo $talkback;
_close($msgsock);
} while (true);
socket_close ($sock);
?>
The above introduces socket request and receive examples, including relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.