php - 仅在固定时间执行 PHP 脚本

标签 php sockets tcp

我有一个函数可以创建套接字连接并监听实验室机器通过 TCP 发送的 HL7 消息的端口号。

如果实验室机器没有发送任何东西,我的监听函数就会继续监听。有没有办法指定它应该只听 10 秒,然后如果没有消息,应该抛出错误?

    $address = '0.0.0.0';
    $port = 5600;

    // Create a TCP Stream socket
    $sock = socket_create(AF_INET, SOCK_STREAM, 0);

    // Bind the socket to an address/port
    $bind = socket_bind($sock, $address, $port);

    // Start listening for connections
    socket_listen($sock);

    $client = socket_accept($sock);

    // Read the input from the client
    $input = socket_read($client, 2024);

    // Strip all white spaces from input
    $segs = explode("|",$input);

    // Close the master sockets
    $close = socket_close($sock);

最佳答案

这是解决方案:

socket_set_option($sock,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>10,"usec"=>0)); // after 10 seconds socket will destroy the connection. Also you can set and uses

关于php - 仅在固定时间执行 PHP 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50384463/

相关文章:

php - 如何获取字段数据类型和pdo?

php - 调用未定义函数 array_column()

php - 将变量从一个 php 文件安全地传递到 URL 中的另一个 php 文件

java - 如何使用java(套接字)连接/远程登录到SPOP3服务器?

C++ : TCP Server "bind" function failed (errno 98) if I do not wait enough time between two consecutive app launch

php - 使用 AJAX 将数据回显到下拉列表中

node.js - 网络服务器如何处理同一端口上的多个连接

python - 使用套接字创建原始 HTTP 请求

networking - TCP 连接持久状态

perl - 如何检查哪种检查端口状态的方法更有效?