php - 非阻塞套接字 : Are messages queued?

标签 php sockets php-5.3 asyncsocket

据我所知,可以创建一个 nonblocking网络socket在 PHP 5.x 中。

但是,如果脚本使用如下相同的非阻塞套接字发送多条长消息,会发生什么情况:

socket_write($socket, $string1, $length);
socket_write($socket, $string2, $length);
socket_write($socket, $string3, $length);
socket_write($socket, $string4, $length);

这些消息是否排队(在发送方/接收方一侧?)或者接收方是否可能因为“并行”发送而获得不同消息的部分?

例如:接收方是否有可能获得 10 个字节的 $string1,然后是 30 个字节的 $string2,然后是另外 25 个字节的 $string1 ... 依此类推...

最佳答案

这取决于套接字使用的协议(protocol)。参见 socket_create对于可能的套接字类型。主要类型有UDP和TCP:

udp The User Datagram Protocol is a connectionless, unreliable, protocol with fixed record lengths. Due to these aspects, UDP requires a minimum amount of protocol overhead.

tcp The Transmission Control Protocol is a reliable, connection based, stream oriented, full duplex protocol. TCP guarantees that all data packets will be received in the order in which they were sent. If any packet is somehow lost during communication, TCP will automatically retransmit the packet until the destination host acknowledges that packet. For reliability and performance reasons, the TCP implementation itself decides the appropriate octet boundaries of the underlying datagram communication layer. Therefore, TCP applications must allow for the possibility of partial record transmission.

直接回答您的问题,TCP 套接字将保证按顺序传递,而 UDP 套接字则不会。

关于php - 非阻塞套接字 : Are messages queued?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9739774/

相关文章:

php - 登录 Joomla 平台 11 JLog

c++ - 文件在套接字传输中继续接收无限数据

php - 在 PHP 命令行上显示完整的堆栈跟踪

php - 从 MYSQL 中获取数据并显示在 JSON 文件中

php - 使用数据列填充下拉列表并获取用户选择的项目

javascript - PHP 后的 Javascript 中缺少变量

c# - NetworkStream.Write 与 Socket.Send

c - 使用 fork、execvp 传输两个命令,然后将输出重定向到套接字

php - 将字符串解析为数组 {{navigation( {"class": "navigation", "id": "navigation"})}} 第 2 部分

php - 模拟当前测试类的内部函数调用