- 相关推荐
PHP socket的配置
PHP独特的语法混合了C、Java、Perl以及PHP自创的语法。它可以比CGI或者Perl更快速地执行动态网页。以下是小编为大家搜索整理的PHP socket的配置及实例
//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)." ";
}
if(($ret = socket_bind($sock,$ip,$port)) < 0) {
echo "socket_bind() 失败的原因是:".socket_strerror($ret)." ";
}
if(($ret = socket_listen($sock,4)) < 0) {
echo "socket_listen() 失败的原因是:".socket_strerror($ret)." ";
}
$count = 0;
do {
if (($msgsock = socket_accept($sock)) < 0) {
echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . " ";
break;
} else {
//发到客户端
$msg ="测试成功! ";
socket_write($msgsock, $msg, strlen($msg));
echo "测试成功了啊 ";
$buf = socket_read($msgsock,8192);
$talkback = "收到的信息:$buf ";
echo $talkback;
if(++$count >= 5){
break;
};
}
//echo $buf;
socket_close($msgsock);
} while (true);
socket_close($sock);
?>
client.php
[php] view plaincopy
error_reporting(E_ALL);
set_time_limit(0);
echo "
TCP/IP Connection
";
$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) . " ";
}else {
echo "OK. ";
}
echo "试图连接 $ip 端口 $port... ";
$result = socket_connect($socket, $ip, $port);
if ($result < 0) {
echo "socket_connect() failed. Reason: ($result) " . socket_strerror($result) . " ";
}else {
echo "连接OK ";
&nbs
【PHP socket的配置】相关文章:
PHP Socket编程过程02-09
php实习心得12-01
php实习报告11-07
配置显示器颜色配置文件方法教程12-02
php工作总结11-11
php是什么格式?01-14
php开发主管的职责05-15
php开发主管的工作职责09-29
php实习报告(5篇)11-08
php实习心得7篇12-02