The fsockopen function can be used. First, you must turn on allow_url_open=on in php.ini;
fsockopen is an encapsulation of the socket client code. This function encapsulates socket_create and socket_connect.
Server-side code: server.php
Copy code The code is as follows:
< ?php
error_reporting(E_ALL);
set_time_limit(0);
$address = '127.0.0.1';
$port = 10008;
//Create port
if ( ($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed:reason:" . socket_strerror(socket_last_error()) . "n";
}
//Bind
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed :reason:" . socket_strerror(socket_last_error($sock)) . "n";
}
//Listening
if (socket_listen($sock, 5) === false) {
echo "socket_bind() failed :reason:" . socket_strerror(socket_last_error( $sock)) . "n";
}
while (true) {
//Get a link
if (($msgsock = socket_accept($sock)) === false) {
echo "socket_accepty() failed :reason:".socket_strerror(socket_last_error($sock)) . "n";
break;
}
//welcome sent to client
$ msg = "1.server send:welcome
";
socket_write($msgsock, $msg, strlen($msg)); / /Return information to the client
echo 'read client messagen';
$buf = socket_read($msgsock, 8192); //Get the information sent by the client
$talkback = "2.received message :$bufn";
echo $talkback;
if (false === socket_write($msgsock, $talkback, strlen($talkback))) { //Return information to the client
echo "socket_write () failed reason:" . socket_strerror(socket_last_error($sock)) ."n";
} else {
echo 'send success';
}
socket_close($msgsock);
}
socket_close($sock);
Client code: fsocket.php
Copy code The code is as follows:
$fp = fsockopen("127.0.0.1", 10008, &$errno, &$errstr, 10);
if (!$fp) {
echo $errstr . " (". $errno . ")
n";
} else {
$in = "HEAD / http/1.1rn";
$in . = "HOST: localhost rn";
$in .= "Connection: closernrn";
fputs($fp, $in);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
http://www.bkjia.com/PHPjc/327077.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327077.htmlTechArticleThe fsockopen function can be used. First, you must turn on allow_url_open=on in php.ini; fsockopen is the socket client code Encapsulation, socket_create, socket_connect are encapsulated in this function. Service...