第一次连接后如何保持php Web套接字连接打开 [英] How to keep a php web socket connection open after first connection

查看:81
本文介绍了第一次连接后如何保持php Web套接字连接打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试学习套接字时,我正在使用以下代码作为套接字的服务器,但是客户端代码只能使用一次,我必须在每次运行clint代码之前都运行服务器脚本.

I'm using the following code as the server for sockets while i'm trying to learn sockets, but the client code will work once, i have to run the server script every time before i run the clint code.

那么我什么时候运行此server.php来继续监听客户端请求?

And so when do i ran this server.php to keep listening for client requests?

SERVER.PHP

$host = "127.0.0.1";
$port = 25003;
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
$input = socket_read($spawn, 1024) or die("Could not read input\n");
if ($input == "Hey"){
    $input = "Hey you don't shout me. Talk properly...";
    } else if ($input == "Vetra"){
        $input = "Hello how are you there, whats your name?";
        } else { $input = "Well, what can i say, you must be a human being.";
}
echo $input;
$output = $input . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
//socket_close($spawn);
//socket_close($socket);

CLIENT.PHP

$host    = "127.0.0.1";
$port    = 25003;
$message = $_POST['data'];
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");  
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
$result = socket_read ($socket, 1024) or die("Could not read server response\n");
echo $result;
//socket_close($socket);

推荐答案

您应该等待无限循环中的新连接:

You should waiting for new connections in infinite loop:

$host = "127.0.0.1";
$port = 25003;
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
while(true) {
  $spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
  $input = socket_read($spawn, 1024) or die("Could not read input\n");
  if ($input == "Hey"){
    $input = "Hey you don't shout me. Talk properly...";
  } else if ($input == "Vetra"){
    $input = "Hello how are you there, whats your name?";
  } else {
    $input = "Well, what can i say, you must be a human being.";
  }
  echo $input.PHP_EOL;
  $output = $input . "\n";
  socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
  socket_close($spawn);
}

这篇关于第一次连接后如何保持php Web套接字连接打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆