正确使用socket_select()

发布于 2024-08-27 14:15:47 字数 1898 浏览 5 评论 0原文

在 PHP 中使用 socket_select 发送和接收数据的正确方法是什么?

我有一个到服务器的连接,该连接允许 TCP 和 TCP 协议。 UDP 数据包连接,我正在使用两者。在这些连接中,我在同一端口上发送和接收数据包,但 TCP 数据包将在一个端口 (29999) 上发送,UDP 将在另一个端口 (30000< /代码>)。传输类型将是AF_INET。 IP 地址将为环回 127.0.0.1

我对如何在这种情况下创建套接字连接有很多疑问。例如,使用 socket_create_pair 建立连接是否更好,或者仅使用 socket_create 后跟 socket_connect,然后实现socket_select?

有可能没有数据从服务器发送到客户端,由客户端来维持连接。这将通过利用 socket_select 调用中的超时函数来完成。如果在时限内没有发送数据,socket_select 函数将中断,然后可以发送保活数据包。以下脚本是客户端的。

// Create
$TCP = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$UDP = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);

// Misc
$isAlive = TRUE;
$UDPPort = 30000;
define('ISP_ISI', 1);

// Connect
socket_connect($TCP, '127.0.0.1', 29999);
socket_connect($UDP, '127.0.0.1', $UDPPort);

// Construct Parameters
$recv = array($TCP, $UDP);
$null = NULL;

// Make The Packet to Send.
$packet = pack('CCCxSSxCSa16a16', 44, ISP_ISI, 1, $UDPPort, 0, '!', 0, 'AdminPass', 'SocketSelect');

// Send ISI (InSim Init) Packet
socket_write($TCP, $packet);

/* Main Program Loop */
while ($isAlive == TRUE)
{
    // Socket Select
    $sock = socket_select($recv, $null, $null, 5);

    // Check Status
    if ($sock === FALSE)
        $isAlive = FALSE; # Error
    else if ($sock > 0)
        # How does one check to find what socket changed?
    else
        # Something else happed, don't know what as it's not in the documentation, Could this be our timeout getting tripped?
}

What is the correct way to use socket_select within PHP to send and receive data?

I have a connection to the server that allows for both TCP & UDP packet connections, I am utilizing both. Within these connections I'm both sending and receiving packets on the same port, but the TCP packet will be sent on one port (29999) and UDP will be sent on another port (30000). The transmission type will be that of AF_INET. The IP address will be loopback 127.0.0.1.

I have many questions on how to create a socket connection within this scenario. For example, is it better to use socket_create_pair to make the connection, or use just socket_create followed by socket_connect, and then implement socket_select?

There is a chance that no data will be sent from the server to the client, and it is up to the client to maintain the connection. This will be done by utilizing the time out function within the socket_select call. Should no data be sent within the time limit, the socket_select function will break and a keep alive packet can then be sent. The following script is of the client.

// Create
$TCP = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$UDP = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);

// Misc
$isAlive = TRUE;
$UDPPort = 30000;
define('ISP_ISI', 1);

// Connect
socket_connect($TCP, '127.0.0.1', 29999);
socket_connect($UDP, '127.0.0.1', $UDPPort);

// Construct Parameters
$recv = array($TCP, $UDP);
$null = NULL;

// Make The Packet to Send.
$packet = pack('CCCxSSxCSa16a16', 44, ISP_ISI, 1, $UDPPort, 0, '!', 0, 'AdminPass', 'SocketSelect');

// Send ISI (InSim Init) Packet
socket_write($TCP, $packet);

/* Main Program Loop */
while ($isAlive == TRUE)
{
    // Socket Select
    $sock = socket_select($recv, $null, $null, 5);

    // Check Status
    if ($sock === FALSE)
        $isAlive = FALSE; # Error
    else if ($sock > 0)
        # How does one check to find what socket changed?
    else
        # Something else happed, don't know what as it's not in the documentation, Could this be our timeout getting tripped?
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〆凄凉。 2024-09-03 14:15:47

我有点困惑 - 您似乎正在尝试处理通过 2 个套接字传入的异步请求,但两者都充当客户端?这是一个非常不寻常的场景。尝试使用不同的协议(tcp 和 udp)来实现它们甚至更奇怪(H323 VOIP 是我所知道的唯一执行此操作的应用程序)。快速谷歌一下,你会发现你正在尝试为 LFS 编写一个客户端 - 但为什么你需要同时运行 TCP 和 UDP 客户端呢? 他们在其 Wiki 上发布了合适的 PHP 客户端代码 http://en.lfsmanual.net

(顺便说一句, 在调用socket_select()之后,有等待读取的数据将位于$recv数组中(即该数组被修剪并且需要在socket_select()的下一次迭代之前重新填充)。

如果socket_select返回0,则仅意味着套接字是非阻塞的,并且它们都没有任何可用数据。

HTH

C.

I'm a bit confused - you seem to be trying to deal with asynchronous requests coming in via 2 sockets but both are acting as clients? This is a very unusual scenario. To be trying to implement them using different protocols (tcp and udp) is even odder (H323 VOIP is the only applciation I know of which does this). A quick google suggests you are trying to write a client for LFS - but why do you need a TCP and UDP client running at the same time? (BTW they publish suitable PHP client code on their Wiki at http://en.lfsmanual.net )

The socket which has data waiting to be read will be in the $recv array after the call to socket_select() (i.e. the array is trimmed down and needs to be repopulated before the next iteration of socket_select()).

If socket_select returns 0 it just means that the sockets are non-blocking and none of them have any data available.

HTH

C.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文