在阻塞和非阻塞套接字上

在阻塞和非阻塞套接字上

本文介绍了php socket_write 在阻塞和非阻塞套接字上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了通过 Unix 本地套接字与服务器通信的 php 脚本.1)连接2) 使用 socket_write() 发送 msg(90% 的时间是小块,10% 是大块)3)得到答案4) 关闭连接

I writing php script that communicates with the server over unix local sockets.1) connect2) send msg with socket_write() (90% of time low-size blocks, 10% big size)3) get the answer4) close connection

现在,我有简单的 socket_write() 实现,就像 php 手册中的评论中建议的那样Jean at briskula dot si 03-Feb-2011 03:00"

Now, I have simple socket_write() implementation like suggested at comments from php manual "jean at briskula dot si 03-Feb-2011 03:00"

似乎它在阻塞模式下工作正常,但我想为 socket_write 实现超时,因此我认为(需要)在调用 socket_write() 时使用非阻塞模式.

Seems its work fine on blocking mode, but I want to implement timeouts for socket_write, therefore I think (need) to use non-blocking mode, when socket_write() called.

阻塞模式比非阻塞模式有什么优势吗?是否有理由实现超时?如何正确认识它们?有什么例子吗?

尝试在while循环上写,但得到===FALSE,可能每次调用socket_write()之前都需要socket_select()?

Tried to write on a while circle, but got ===FALSE, maybe need to socket_select() every time before socket_write() called?

ps.当从服务器读取答案时,这个问题对于 socket_read() 也是实际的

ps. this question also actual for socket_read() when reading answer from server

推荐答案

对于 PHP(面向 Web)脚本,通过套接字向后端系统发送数据/从后端系统接收数据,同时没有机会进行其他处理(这是基本上,您首先要使用非阻塞模式的原因),处理非阻塞模式导致的复杂情况是没有意义的.

For a PHP (web-facing) script sending data to/receiving data from a backend system through sockets without the chance for other processing in the meantime (which is basically why you'd want to use non-blocking mode in the first place), there's no sense in handling the complications that non-blocking mode causes.

所以,坚持使用阻塞模式.

So, stick with blocking mode.

这篇关于php socket_write 在阻塞和非阻塞套接字上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 21:34