Home > Backend Development > PHP Tutorial > 关于php的socket

关于php的socket

WBOY
Release: 2016-06-23 14:11:08
Original
930 people have browsed it

最近在学php的socket,想通过一个功能的实现检验我对socket的理解:
1、client端发送数据到服务器端
2、server端接收client发送过来的数据,并会送一个响应给client端
3、client端显示server端发过来的响应信息

以下是代码

server.php:
// Set up our socket
$commonProtocol = getprotobyname("tcp");
$socket = socket_create(AF_INET, SOCK_STREAM, $commonProtocol);
socket_bind($socket, 'localhost', 1234);
socket_listen($socket);
// Initialize the buffer
$buffer = "NO DATA";
while(true) {
// Accept any connections coming in on this socket
$connection = socket_accept($socket);
printf("Socket connected\r\n");
// Check to see if there is anything in the buffer
$buffer=socket_read($connection, 2048, PHP_NORMAL_READ);
echo "recived from client ".$buffer;
$ResponseToClient=$buffer." is handled by server!\n";
socket_write($connection, $ResponseToClient);
echo "response to client!\n ";
}
?>

client.php:
// Create the socket and connect
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$connection = socket_connect($socket,'localhost', 65500);
$i=0;
$s=0;
while(true)
{
echo "sending data".$i++."\n";
$SendDataToServer=socket_write($socket, "server".$s++);
$ResponseFromServer=socket_read($socket, 2048, PHP_NORMAL_READ);
echo "received".$ResponseFromServer."from server \n";
}
?>

问题是:client端只显示sending data0
而server端只显示Socket connected
其他什么也不显示,没有达到我预想的client不断发送数据,server端不断处理数据并返回给client的目的,请高手指教下,万分感谢


回复讨论(解决方案)

哪位大侠回复下啊

不是很清楚,为什么两边的端口不一样啊?

楼上+1
另:就算你的程序顺利执行,也是个死循环

看来大都没怎么么搞过socket,失望 

看来大都没怎么么搞过socket,失望 
我是觉得php在socket方面不靠谱啊。。。

没有需求干嘛弄这个?
不过可以给你个早年写的测试例
服务端

<?php// Server// 设置错误处理error_reporting (E_ALL);// 设置运行时间set_time_limit (0);// 起用缓冲ob_implicit_flush ();$ip = "127.0.0.1";        // IP地址$port = 1000;             // 端口号$socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);       // 创建一个SOCKETif ($socket)    echo "socket_create() successed!\n";else    echo "socket_create() failed:".socket_strerror ($socket)."\n";$bind = socket_bind ($socket, $ip, $port);              // 绑定一个SOCKETif ($bind)    echo "socket_bind() successed!\n";else    echo "socket_bind() failed:".socket_strerror ($bind)."\n";$listen = socket_listen ($socket);                 // 间听SOCKETif ($listen)    echo "socket_listen() successed!\n";else    echo "socket_listen() failed:".socket_strerror ($listen)."\n";while (true){    $msg = socket_accept ($socket);               // 接受一个SOCKET    if (!$msg)    {        echo "socket_accept() failed:".socket_strerror ($msg)."\n";        break;    }    $welcome = "服务端收到:Welcome to PHP Server!\n";    socket_write ($msg, $welcome, strlen ($welcome));    while (true)    {        $command = strtoupper (trim (socket_read ($msg, 1024)));        if (!$command)            break;        switch ($command)        {            case "HELLO":                $writer = "Hello Everybody!";                break;            case "QUIT":                $writer = "Bye-Bye";                break;            case "HELP":                $writer = "HELLO\tQUIT\tHELP";                break;            default:                $writer = "Error Command!";        }        socket_write ($msg, $writer, strlen ($writer));        if ($command == "QUIT")            break;    }    socket_close ($msg);    if ($command == "QUIT")        break;}socket_close ($socket);                   // 关闭SOCKET
Copy after login

客户端
<?php// Client // 设置错误处理error_reporting (E_ALL);// 设置处理时间set_time_limit (0);$ip = "127.0.0.1";       // IP 地址$port = 1000;            // 端口号$socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);   // 创建一个SOCKETif ($socket)    echo "socket_create() 成功!<br>\n";else    echo "socket_create() 失败:".socket_strerror ($socket)."<br>\n";$conn = socket_connect ($socket, $ip, $port);       // 建立SOCKET的连接if ($conn)    echo "成功连接到[".$ip.":".$port."]<br>\n";else    echo "socket_connect() 失败:".socket_strerror ($conn)."<br>\n";echo socket_read ($socket, 1024)."<br>";$stdin = fopen ('php://stdin', 'r');$ar = array("HELLO","HELP","test","QUIT");$i = 0;while (true){//    $command = trim (fgets ($stdin, 1024));//    socket_write ($socket, $command, strlen ($command));echo "发送 $ar[$i]<br>";    socket_write ($socket, $ar[$i], strlen($ar[$i]));$i++;    $msg = trim (socket_read ($socket, 1024));    echo "收到:$msg<br>\n";    if ($msg == "Bye-Bye")        break;}fclose ($stdin);socket_close ($socket);
Copy after login

正常的话应该显示:
socket_create() 成功!
成功连接到[127.0.0.1:1000]
服务端收到:Welcome to PHP Server! 
发送 HELLO
收到:Hello Everybody!
发送 HELP
收到:HELLO QUIT HELP
发送 test
收到:Error Command!
发送 QUIT
收到:Bye-Bye

是死循环 

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template