基于PHP Socket配置以及实例的详细介绍
程序员文章站
2022-06-29 17:23:51
2个php测试文件server.php复制代码 代码如下:
2个php测试文件
server.php
<?php
//phpinfo();
//确保在连接客户端时不会超时
set_time_limit(0);
$ip = '127.0.0.1';
$port = 1935;
/*
+-------------------------------
* @socket通信整个过程
+-------------------------------
* @socket_create
* @socket_bind
* @socket_listen
* @socket_accept
* @socket_read
* @socket_write
* @socket_close
+--------------------------------
*/
/*---------------- 以下操作都是手册上的 -------------------*/
if(($sock = socket_create(af_inet,sock_stream,sol_tcp)) < 0) {
echo "socket_create() 失败的原因是:".socket_strerror($sock)."\n";
}
if(($ret = socket_bind($sock,$ip,$port)) < 0) {
echo "socket_bind() 失败的原因是:".socket_strerror($ret)."\n";
}
if(($ret = socket_listen($sock,4)) < 0) {
echo "socket_listen() 失败的原因是:".socket_strerror($ret)."\n";
}
$count = 0;
do {
if (($msgsock = socket_accept($sock)) < 0) {
echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
break;
} else {
//发到客户端
$msg ="测试成功!\n";
socket_write($msgsock, $msg, strlen($msg));
echo "测试成功了啊\n";
$buf = socket_read($msgsock,8192);
$talkback = "收到的信息:$buf\n";
echo $talkback;
if(++$count >= 5){
break;
};
}
//echo $buf;
socket_close($msgsock);
} while (true);
socket_close($sock);
?>
client.php
<?php
error_reporting(e_all);
set_time_limit(0);
echo "<h2>tcp/ip connection</h2>\n";
$port = 1935;
$ip = "127.0.0.1";
/*
+-------------------------------
* @socket连接整个过程
+-------------------------------
* @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 "试图连接 '$ip' 端口 '$port'...\n";
$result = socket_connect($socket, $ip, $port);
if ($result < 0) {
echo "socket_connect() failed.\nreason: ($result) " . socket_strerror($result) . "\n";
}else {
echo "连接ok\n";
}
$in = "ho\r\n";
$in .= "first blood\r\n";
$out = '';
if(!socket_write($socket, $in, strlen($in))) {
echo "socket_write() failed: reason: " . socket_strerror($socket) . "\n";
}else {
echo "发送到服务器信息成功!\n";
echo "发送的内容为:<font color='red'>$in</font> <br>";
}
while($out = socket_read($socket, 8192)) {
echo "接收服务器回传信息成功!\n";
echo "接受的内容为:",$out;
}
echo "关闭socket...\n";
socket_close($socket);
echo "关闭ok\n";
?>
侦听
# /usr/local/php/bin/php /usr/local/apache2/htdocs/server.php
#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修改配置
增加
extension=sockets.so
重启apache
# /usr/local/apache2/bin/apachectl restart
===================
windows下的配置
修改php.ini
extension=php_sockets.dll
重启apache
cmd下起2个
php的安装目录
c:/php/php.exe f:web/server.php
c:/php/php.exe f:web/client.php
server.php
复制代码 代码如下:
<?php
//phpinfo();
//确保在连接客户端时不会超时
set_time_limit(0);
$ip = '127.0.0.1';
$port = 1935;
/*
+-------------------------------
* @socket通信整个过程
+-------------------------------
* @socket_create
* @socket_bind
* @socket_listen
* @socket_accept
* @socket_read
* @socket_write
* @socket_close
+--------------------------------
*/
/*---------------- 以下操作都是手册上的 -------------------*/
if(($sock = socket_create(af_inet,sock_stream,sol_tcp)) < 0) {
echo "socket_create() 失败的原因是:".socket_strerror($sock)."\n";
}
if(($ret = socket_bind($sock,$ip,$port)) < 0) {
echo "socket_bind() 失败的原因是:".socket_strerror($ret)."\n";
}
if(($ret = socket_listen($sock,4)) < 0) {
echo "socket_listen() 失败的原因是:".socket_strerror($ret)."\n";
}
$count = 0;
do {
if (($msgsock = socket_accept($sock)) < 0) {
echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
break;
} else {
//发到客户端
$msg ="测试成功!\n";
socket_write($msgsock, $msg, strlen($msg));
echo "测试成功了啊\n";
$buf = socket_read($msgsock,8192);
$talkback = "收到的信息:$buf\n";
echo $talkback;
if(++$count >= 5){
break;
};
}
//echo $buf;
socket_close($msgsock);
} while (true);
socket_close($sock);
?>
client.php
复制代码 代码如下:
<?php
error_reporting(e_all);
set_time_limit(0);
echo "<h2>tcp/ip connection</h2>\n";
$port = 1935;
$ip = "127.0.0.1";
/*
+-------------------------------
* @socket连接整个过程
+-------------------------------
* @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 "试图连接 '$ip' 端口 '$port'...\n";
$result = socket_connect($socket, $ip, $port);
if ($result < 0) {
echo "socket_connect() failed.\nreason: ($result) " . socket_strerror($result) . "\n";
}else {
echo "连接ok\n";
}
$in = "ho\r\n";
$in .= "first blood\r\n";
$out = '';
if(!socket_write($socket, $in, strlen($in))) {
echo "socket_write() failed: reason: " . socket_strerror($socket) . "\n";
}else {
echo "发送到服务器信息成功!\n";
echo "发送的内容为:<font color='red'>$in</font> <br>";
}
while($out = socket_read($socket, 8192)) {
echo "接收服务器回传信息成功!\n";
echo "接受的内容为:",$out;
}
echo "关闭socket...\n";
socket_close($socket);
echo "关闭ok\n";
?>
侦听
# /usr/local/php/bin/php /usr/local/apache2/htdocs/server.php
请求
# /usr/local/php/bin/php /usr/local/apache2/htdocs/client.php
#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修改配置
增加
extension=sockets.so
重启apache
# /usr/local/apache2/bin/apachectl restart
===================
windows下的配置
修改php.ini
extension=php_sockets.dll
重启apache
cmd下起2个
php的安装目录
c:/php/php.exe f:web/server.php
c:/php/php.exe f:web/client.php
上一篇: 喝茶拉肚子,这些冷知识你知道吗
下一篇: PHP代码审核的详细介绍
推荐阅读
-
基于php上传图片重命名的6种解决方法的详细介绍
-
php class中self,parent,this的区别以及实例介绍
-
jquery中each和push详细介绍以及ajax的简单实例
-
基于php伪静态的实现详细介绍
-
基于PHP Socket配置以及实例的详细介绍
-
php class中self,parent,this的区别以及实例介绍_PHP
-
基于php socket(fsockopen)的应用实例分析
-
php class中self,parent,this的区别以及实例介绍_PHP
-
基于php socket(fsockopen)的应用实例分析_PHP
-
PHP实现多关键字加亮功能的详细介绍和实例代码