我有一个函数可以创建套接字连接并监听实验室机器通过 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/