Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使server.php持续运行?_Php_Sockets_Client - Fatal编程技术网

如何使server.php持续运行?

如何使server.php持续运行?,php,sockets,client,Php,Sockets,Client,所以我有这个服务器代码,它与我的客户机一起工作。但它从客户机收到一条消息,然后将一条消息发回。 代码如下: SERVER.php <?php $host = "127.0.0.1"; $port = 1234; // don't timeout! set_time_limit(0); // create socket $socket = socket_create(AF_INET, SOCK_STREAM, 0) or di

所以我有这个服务器代码,它与我的客户机一起工作。但它从客户机收到一条消息,然后将一条消息发回。 代码如下: SERVER.php

<?php 

    $host = "127.0.0.1"; 
    $port = 1234; 

    // don't timeout! 
    set_time_limit(0); 

    // create socket 
    $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n"); 

    // bind socket to port 
    $result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n"); 

    // start listening for connections 
    $result = socket_listen($socket, 3) or die("Could not set up socket listener\n"); 

    // accept incoming connections 
    // spawn another socket to handle communication 
    $spawn = socket_accept($socket) or die("Could not accept incoming connection\n"); 

    // read client input 
    $input = socket_read($spawn, 1024) or die("Could not read input\n"); 

    // clean up input string 
    $input = trim($input); 

    // reverse client input and send back 
    $output = strrev($input) . "\n"; 
    socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n"); 

    // close sockets 
    socket_close($spawn); 
    socket_close($socket); 

?>
最“野蛮”的方法是将代码插入无限循环中

While(1){
     .
     .
 }

另一种方式是,在收到消息后,您可以递归调用脚本。

接受后,您需要fork-请参见此处

accept需要在一个循环中。在接受fork之后,需要一个处理该客户机的进程

i、 e


您需要使用来整理僵尸进程。

我找到了答案。你们中的大多数人都很接近解决这个问题,就像我使用while()循环一样。
  while( TRUE ) {

      // your code


  }
但是,您不能只是将代码放在while中,然后期望它工作。正确的做法如下:

<?php 
$host = "127.0.0.1"; 
$port = 1234; 

// don't timeout! 
set_time_limit(0); 

// create socket 
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n"); 

// bind socket to port 
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n"); 
    while(true) {
    // start listening for connections 
    $result = socket_listen($socket, 3) or die("Could not set up socket listener\n"); 

    // accept incoming connections 
    // spawn another socket to handle communication 
    $spawn = socket_accept($socket) or die("Could not accept incoming connection\n"); 

    // read client input 
    $input = socket_read($spawn, 1024) or die("Could not read input\n"); 

    // clean up input string 
    $input = trim($input); 

    // reverse client input and send back 
    $output = strrev($input) . "\n"; 
    socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n"); 
    }
// close sockets 
socket_close($spawn); 
socket_close($socket); 

?>

等等。。。它“发回一条信息”?你能在问题中包含这个相反的信息吗?此外,听起来您正在进行UDP连接,而不是TCP连接……您需要一个连续的while循环。可能需要一些分叉来处理一个以上的请求。等等。看看这个问题,这难道不是Apache已经在为你做的事情吗?@Gabe:它发送消息的事实与我的问题无关。想象一下,它只是将确认信息发送回客户机。另外,“socket\u create(AF\u INET,SOCK\u STREAM,0)”应该是指TCP,我认为它是TCP,不用担心。然而,这类代码并不是要在Web服务器上运行,只是创建一个deamon。它不会工作,你会得到“socket_bind():无法绑定地址[…]:地址已在使用”作为Roma,penso siamo connazionali:)Sempliconecene cambia la porta effettiva utilizata dal socket and ogni invocazione e e e e e funzioneráHAHA有趣,因为罗马是一家意大利足球俱乐部,这并不意味着我是意大利人。请讲英语;)虽然谷歌翻译有神奇的功能…哦,对不起:你有这个问题,因为你在同一个端口上重新绑定了一个现有的套接字。你不能使用同一个插座吗?打开套接字是一个非常昂贵的操作。它将不起作用,您将得到“socket_bind():无法绑定地址[…]:地址已在使用”
<?php 
$host = "127.0.0.1"; 
$port = 1234; 

// don't timeout! 
set_time_limit(0); 

// create socket 
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n"); 

// bind socket to port 
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n"); 
    while(true) {
    // start listening for connections 
    $result = socket_listen($socket, 3) or die("Could not set up socket listener\n"); 

    // accept incoming connections 
    // spawn another socket to handle communication 
    $spawn = socket_accept($socket) or die("Could not accept incoming connection\n"); 

    // read client input 
    $input = socket_read($spawn, 1024) or die("Could not read input\n"); 

    // clean up input string 
    $input = trim($input); 

    // reverse client input and send back 
    $output = strrev($input) . "\n"; 
    socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n"); 
    }
// close sockets 
socket_close($spawn); 
socket_close($socket); 

?>