Home >Backend Development >PHP Tutorial >Detailed introduction based on PHP Socket configuration and examples_PHP tutorial

Detailed introduction based on PHP Socket configuration and examples_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-21 15:07:08879browse

2 php test files
server.php

Copy code The code is as follows:

//phpinfo();
//Ensure 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 It's all 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() failed because: ".socket_strerror($ret)."n";
}
$count = 0;
do {
if (($msgsock = socket_accept($sock)) < 0) {
          echo "socket_accept() failed: reason: " . socket_strerror($msgsock)       "n";
                                                      else {

//Send to client
$msg="Test successful! n";
socket_write($msgsock, $msg, strlen($msg));

echo "The test was successfuln";
$buf = socket_read($msgsock,8192);

                                                          break;
};

 
}
//echo $buf;
socket_close($msgsock);
} while (true);
socket_close($ sock);
?>




client.php


Copy code
The code is as follows:
error_reporting(E_ALL);set_time_limit(0);echo "

TCP/IP Connectionn";

$port = 1935;
$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

www.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; /* +------...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn