dongyo1818 2014-07-08 09:02
浏览 60

让socket服务器在CentOS上运行,以便在php中监听客户端请求

I have a client and server program i do not have an idea of how to start the server automatically on my CENTOS server and the server should keep on running irrespective of my browser is closed, and it keeps on listening to client request. Please Help

Server.php

<?php
// set some variables
$host = "XX.XX.XXX.XX";
$port = 25763;
// 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) . "
";
echo $spawn;
socket_write($spawn, $output, strlen ($output)) or die("Could not write output
");
// close sockets
socket_close($spawn);
socket_close($socket);
?>

Client.php

<?php
// where is the socket server?
$host    = "XX.XX.XXX.XX";
$port    = 25763;
$message = "Hello Server This is the first message to the 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 "Reply From Server  :".$result;
// close socket
socket_close($socket);
    // print result to browser
?>
  • 写回答

1条回答 默认 最新

  • drctyr2869 2014-07-08 09:35
    关注

    Generally, you should start .php file with exec(), for example:

    exec('/usr/bin/php /var/www/directory/server/event_server.php');
    

    Next thing, put your socket_accept() (althoug, I used different scheme, with socket_select()) in infinite loop, and dont forget about delay - this is very important, as you don't want to hang your CPU:

    $broadcast_start = microtime(true);
    
    while(TRUE) {
        socket_accept($socket);
    
        //Do stuff...
    
        if( round(microtime(true) - $broadcast_start, 1) < 0.5 ) {
            usleep(500000);
        }
    }
    

    But actually, your subbjects is really complex. You should first search the web for solutions, which are plenty.

    评论

报告相同问题?

悬赏问题

  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。