douchuang4181 2019-04-12 06:26
浏览 114

如何在php socket中连续运行server.php

The overall code works but the problem is I need to refresh the server.php every single time just to work the entire code. How am I going to make the server run continuously without the hassle of refreshing the server.php?

Client.php

<?php
$host    = "localhost";
$port    = 1024;
$message = "Hello Server";
echo "Message To server :".$message;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket
");
// connect to server
$result = socket_connect($socket, $host, $port) or die("Could not connect to  server
");  
// send string to server
socket_write($socket, $message, strlen($message)) or die("Could not send data to server
");
// get server response
$result = socket_read ($socket, 1024) or die("Could not read server response
");
echo "<br>"."Reply From Server  :".$result;
// close socket
socket_close($socket) ?>

Server.php

<?php
// set some variables
$host = "localhost";
$port = 1024;
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket
");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket
");
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener
");

// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection
");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input
");
// clean up input string
$input = trim($input);
echo "Client Message : ".$input;
// reverse client input and send back
$output = strrev($input) . "
";
socket_write($spawn, $output, strlen ($output)) or die("Could not write  output
");
// close sockets
socket_close($spawn);
socket_close($socket);
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!