$ip = "127.0.0.1";
/*
+---------------- ---------------
* @socket connection entire 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 "Attempting to connect to '$ip' port '$port'...n";
$result = socket_connect($socket, $ip, $port);
if ($result < 0) {
echo "socket_connect() failed.nReason: ($result) " . socket_strerror($result) . "n";
}else {
echo "Connect OKn";
}
$in = "Horn";
$in .= "first bloodrn";
$out = '';
if(!socket_write($socket, $in, strlen($in) )) {
echo "socket_write() failed: reason: " . socket_strerror($socket) . "n";
}else {
echo "Information sent to server successfully! n";
echo "The content sent is:
$in ";
}
while($out = socket_read($ socket, 8192)) {
echo "Received server response information successfully! n";
echo "The accepted content is:",$out;
}
echo "Close SOCKET...n";
socket_close($socket);
echo "Close OKn";
?>
Listening
# /usr/local/php/bin/php /usr/local/apache2/htdocs/server.php
Request
# /usr/local/php/bin/php /usr/local/apache2/htdocs/client.php
Socket is not enabled in PHP by default
#cd ./ext/sockets/
# /usr/local/php/bin/phpize
# ./configure --enable-sockets --with-php-config=/usr/local/php/bin/php-config
# make
# make install
php .ini modify configuration
Add
extension=sockets.so
Restart apache
# /usr/local/apache2/bin/apachectl restart
===================
Configuration under Windows
Modify php.ini
extension =php_sockets.dll
Restart apache
Cmd and start 2 places
php installation directory
C:/php/php.exe F:web/server.php
C: /php/php.exe F:web/client.php
http://www.bkjia.com/PHPjc/327567.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327567.htmlTechArticle2 php test files server.php Copy the code code as follows: ?php //phpinfo(); //Ensure There will be no timeout when connecting to the client set_time_limit(0); $ip = '127.0.0.1'; $port = 1935; /* +------...