php7 获取数据流,stream_socket_accept()
stream_socket_accept()
(PHP 5, PHP 7)
接受由stream_socket_server()创建的套接字连接
说明stream_socket_accept(resource$server_socket[,float$timeout= ini_get("default_socket_timeout")[,string&$peername]]):resource
接受由stream_socket_server()创建的套接字连接。
参数$server_socket需要接受的服务器创建的套接字连接。$timeout覆盖默认的套接字接受的超时时限。输入的时间需以秒为单位。$peername如果包含该参数并且是可以从选中的传输数据中获取到,则将被设置给连接中的客户端主机的名称(地址)(怕出入很大,附带上原文:Will be set to the name (address) of the client which connected, if included and available from the selected transport.)Note:
也可以之后通过stream_socket_get_name()来确定。
返回值
返回接受套接之后的资源流或者在失败时返回FALSE。
注释Warning
该函数不能被用于 UDP 套接字。可以使用stream_socket_recvfrom()和stream_socket_sendto()来取而代之。
参见cURL 函数This code could be very helpfull...
The following code is for the "server". It listen for a message until CTRL-C
while (true)
{
// disconnected every 5 seconds...
receive_message('127.0.0.1','85',5);
}
function receive_message($ipServer,$portNumber,$nbSecondsIdle)
{
// creating the socket...
$socket = stream_socket_server('tcp://'.$ipServer.':'.$portNumber, $errno, $errstr);
if (!$socket)
{
echo "$errstr($errno)
\n";
}
else
{
// while there is connection, i'll receive it... if I didn't receive a message within $nbSecondsIdle seconds, the following function will stop.
while ($conn = @stream_socket_accept($socket,$nbSecondsIdle))
{
$message= fread($conn, 1024);
echo 'I have received that : '.$message;
fputs ($conn, "OK\n");
fclose ($conn);
}
fclose($socket);
}
}
?>
The following code is for the "client". It send a message, and read the respons...
send_message('127.0.0.1','85','Message to send...');
function send_message($ipServer,$portServer,$message)
{
$fp = stream_socket_client("tcp://$ipServer:$portServer", $errno, $errstr);
if (!$fp)
{
echo "ERREUR :$errno-$errstr
\n";
}
else
{
fwrite($fp,"$message\n");
$response = fread($fp, 4);
if ($response != "OK\n")
{echo 'The command couldn\'t be executed...\ncause :'.$response;}
else
{echo 'Execution successfull...';}
fclose($fp);
}
}
?>To check if there's a new connection waiting, without blocking, or (when using non-blocking mode) without notices), you can use stream_accept (as opposed to socket_select).
class GenericClass {
protected $resSocket=null;
function acceptConnections() {
# check that we still have a resource
if(is_resource($this->resSocket)) {
$arrRead=array($this->resSocket);
$arrWrite=array();
/** @warning Passing $arrRead,$arrWrite by reference */
if(stream_select($arrRead,$arrWrite,$arrWrite,0)) {
$resConnection=stream_socket_accept($this->resSocket,0);
# ... other stuff here
}
}
}
}
?>this function, compared to the function socket_accept, got an extra argument "timeout".
To make this function wait indefinitelly to incoming connections, just as in socket_accept, set timeout to -1. It works for me with PHP 5.0.4.Note that if you use 0 as timeout, the connection will timeout right away.To whom it may concern, and it may concern you greatly, stream_set_blocking has no effect on stream_socket_accept.
If you want it to return right away, connection or not, use 0 for the timeout parameter.
php7 获取数据流,stream_socket_accept()相关推荐
- java inputstream长度_Java InputStream.available获取数据流字节长度大小
首页 > 基础教程 > IO流 > InputStream类 Java InputStream.available获取数据流字节长度大小 定义 public int availabl ...
- USB扫码枪获取数据流的实现方式
硬件条件: OTG接口转换器(或者自带usb接口的设备(大头)) USB扫码枪(支持USB虚拟串口) 安卓设备 实现方式: 串口方式 USB方式 使用场景: 在扫码枪连接机器的时候,当扫码枪扫描到内容 ...
- 页面从后台接口获取数据流显示照片
JSP页面: 页面上接收数据流可以用<a>标签的"href"属性接收,也可以用<img>标签的"src"属性接收. <a href ...
- php7 获取文件类型,太简单了!PHP获取文件扩展名的7中方法
PHP中获取文件扩展名的方法 第一种:$file = 'x.y.z.png'; echo substr(strrchr($file, '.'), 1); 解析:strrchr($file, '.') ...
- php7 获取客户端 ip 地址
function getIp(){$ip='未知IP';if(!empty($_SERVER['HTTP_CLIENT_IP'])){return is_ip($_SERVER['HTTP_CLIEN ...
- android camera 预览 数据流,camera,SurfaceView实时预览获取帧数据,在手机上总是闪退...
请教各位关于用camera类做一个关于预览SurfaceView,然后通过 public void onPreviewFrame(byte[] data, Camera camera)获取data转换 ...
- android—获取网络数据
取网络数据主要靠发交易(或者说请求,接口等),而这些交易由java中的网络通信,HttpURLConnection和HttpClient实现,以下是具体例子. 大家都知道,网络通信,发送请求有两种方式 ...
- 《剑指offer》数据流中的中位数
题目:如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值.如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值. 解析: ...
- ASP.NET Web API 应用教程(一) ——数据流使用
相信已经有很多文章来介绍ASP.Net Web API 技术,本系列文章主要介绍如何使用数据流,HTTPS,以及可扩展的Web API 方面的技术,系列文章主要有三篇内容. 主要内容如下: I 数据 ...
最新文章
- xgboost调参指南
- zend studio常见问题解答
- python列表是顺序表还是链表_顺序表与链表
- 查看linux可用磁盘空间_如何在Linux中检查可用磁盘空间
- 【算法】斐波那契数列
- html怎么根据控IE制台找错误,IE调试网页之三:控制台查看错误和状态
- JZOJ 1036. 【SCOI2009】迷路
- 抽象类与接口的区别(
- jBPM4.3+ssh+会签 整合配置及完整实例
- kubernetes kubeadm init kube-apiserver.yaml already exists
- 在Javascript中,获取到数字超出长度问题
- 从零开始学python必看,最强“Python编程三剑客(pdf)”,你值得拥有!
- linux服务器上的cad病毒,LibreCAD_LibreCAD下载[2021官方最新版]LibreCAD安全下载_
极速下载...
- OpenCV读取图片
- WebDriverException: Message: unknown error: cannot find Chrome binary(ChromeDriver及谷歌官网下载地址)
- github python100天_GitHub - 1814931012/Python-100-Days: Python - 100天从新手到大师
- ORA-00955: name is already used by an existing object
- 采用DotAsterisk(点星PBX)系统组建跨地区总公司和分公司之间的免费VoIP电话呼叫
- Android中的Activity
- 关于antd Tree组件修改图标
热门文章
- Neo4j:找到两个纬度/经度之间的中点
- java中缓冲区和缓存_Java中的Google协议缓冲区
- 避免在ConcurrentHashMap.computeIfAbsent()中进行递归
- Java IO基准测试:Quasar与异步ForkJoinPool与ManagedBlock
- 关于Java的常见误解
- Java,Scala,Guava和Trove集合-它们可以容纳多少数据?
- Gradle接口:Gradle构建元数据
- 编写自动调试器以在测试执行期间捕获异常
- 使用Google Guava创建收藏和实现不变性
- 在Intellij IDEA中运行Vaadin应用