【发布时间】:2013-08-30 16:24:53
【问题描述】:
我想在 PHP 中实现一个客户端套接字。
套接字类型为 TCP/IP 并运行全双工通道。 客户端必须每秒向服务器发送请求,所以必须循环运行。
我是否必须为每个请求重新创建并连接到套接字? 还是通过循环创建并连接到外部的套接字然后管理通信更好?
<?php
while (true) {
//management of the pid here
//create the socket
$socket = socket_create (AF_INET, SOCK_STREAM, 0);
//send the request
$sent = socket_write ($socket, <query>, <length>);
//get the answer
$response = socket_read ($socket, 1024);
//close the socket
socket_shutdown ($socket, 2);
usleep (500);/ / wait remote host
socket_close ($socket);
sleep (1);
}
?>
我已经实现了循环外的socket连接和里面的请求管理。
但连接会保持活动几分钟,然后在端点连接上返回错误。
这种情况下是否需要使用socket_bind?
谢谢。
【问题讨论】: