


php socket cannot handle the data flow, how to avoid it (it seems to be blocked)
PHP socket cannot handle the data stream, what should I do (it seems to be blocked)Requirement: PHP accepts a piece of hardware to send data to port 8888. If received, the socket_send function should return "xFAx01x01xFFxAAxAAx00x01x00x00x00x00x00x01". After the hardware receives the data sent by socket_send, it will make a "beep" sound, but a problem occurs. A piece of hardware still Okay, but when multiple hardware are connected at the same time and send data at the same time, the hardware will not respond continuously (that is, it will make a "beep" sound). That is to say, after it can make a "beep" sound continuously, it will no longer ring. Probably After a few seconds, it started to respond again. After a while, it stopped working again. This is the case for several connected hardware. I have used non-blocking mode, but it still happens. Looking for a solution, the code is posted below
-
PHP code
<!--?php error_reporting(E_ALL); set_time_limit(0); ini_set("allow_call_time_pass_reference",true); //监听端口 $PORT = 8888; //最大连接池 $MAX_USERS = 50; //创建监听端口 //$sock = socket_create_listen($PORT); $commonProtocol = getprotobyname("tcp"); $sock = socket_create(AF_INET, SOCK_STREAM, $commonProtocol); @socket_bind($sock, '192.168.1.112 @socket_listen($sock); if (!$sock) { exit(1); } //不阻塞 socket_set_nonblock($sock); $connections = array(); $input = array(); $close = array(); while (true) { //sleep(3); $readfds = array_merge($connections, array($sock)); $writefds = array(); //选择一个连接,获取读、写连接通道 if (socket_select($readfds, $writefds, $e = null, $t=60)) { foreach ($readfds as $rfd) { //如果是当前服务端的监听连接 if ($rfd == $sock) { //接受客户端连接 $newconn = socket_accept($sock); $i = (int)$newconn; $reject = ''; if (count($connections) -->= $MAX_USERS) { $reject = "Server full. Try again later.\n"; } //将当前客户端连接放如socket_select选择 $connections[$i] = $newconn; //输入的连接资源缓存容器 $writefds[$i] = $newconn; //连接不正常 if ($reject) { $close[$i] = true; } else { echo "Welcome to the PHP Chat Server!\n"; } //初始化当前连接读取内容的缓存容器 $input[$i] = ""; continue; } //客户端连接 $i = (int)$rfd; //读取 $tmp = @socket_read($rfd, 14, PHP_NORMAL_READ); if (!$tmp) { //读取不到内容 print "connection closed on socket $i\n"; close($i); continue; } $input[$i] .= $tmp; $tmp = substr($input[$i], -1); /*if ($tmp != "\r" && $tmp != "\n") { // no end of line, more data coming continue; }*/ $line = trim($input[$i]); $input[$i] = ""; echo 'Client >>'.$line."\r\n"; socket_getpeername($connections[$i],&$remoteIP,&$remotePort); echo $remoteIP."\r\n"; echo $remotePort."\r\n"; //$data=str_split($buffer); //print_r($data); $str="\xFA\x01\x01\xFF\xAA\xAA\x00\x01\x00\x00\x00\x00\x00\x01"; socket_send($connections[$i],$str,strlen($str),0); } foreach ($writefds as $wfd) { $i = (int)$wfd; $w = socket_write($wfd, "hello"); } } } function close($i) { global $connections, $input, $close; socket_shutdown($connections[$i]); socket_close($connections[$i]); unset($connections[$i]); unset($input[$i]); unset($close[$i]); } ?>

在大数据时代,数据管理和处理成为了企业发展的重要一环。而对于数据流的处理,ApacheNiFi是一种领先的开源数据流处理工具。PHP语言对于网站和应用的开发十分熟悉,那么如何将PHP和ApacheNiFi集成实现数据流管理和处理呢?一、ApacheNiFi简介ApacheNiFi是一个强大的、可视化的数据流处理工具。它可以通过可视化的方式,将数据从各

随着互联网应用的不断增多,数据的处理变得越来越重要。为了更好地处理数据,提高系统的效率和可靠性,数据流模型设计成为了一种重要的方法。本文将介绍如何在Go语言中设计数据流模型,包括流管道、分组、过滤器等。流管道流管道是数据流模型的基础组成部分,可以将数据从一个处理单元传递到另一个处理单元。在Go语言中,可以使用channel作为管道,channel支持数据的异

Swoole和Workerman对PHP与MySQL的数据传输和数据加密的优化方法随着互联网的迅速发展,PHP作为一种常用的服务器端编程语言,广泛应用于Web开发领域。在PHP的应用中,数据传输和数据安全性一直是开发者关注的焦点。为了提高数据传输的效率和保护数据的安全性,开发者通常会使用一些优化方法。本文将重点介绍Swoole和Workerman这两个常用

React数据流管理指南:如何优雅地处理前端数据流动引言:React是一种非常流行的前端开发框架,它提供了一种组件化的开发方式,使得前端开发更加模块化、可维护性更高。然而,在开发复杂的应用程序时,管理数据流动变得很重要。这篇文章将介绍一些React中优雅处理数据流动的方法,并演示具体的代码示例。一、单向数据流React倡导使用单向数据流来管理数据流动。单向数

标题:PHPStream数据流操作指南在Web开发中,数据流操作是非常常见的操作,可以用于读取文件内容、发送HTTP请求、处理网络数据等功能。PHP提供了强大的Stream功能,使得数据流操作变得更加简单和便捷。本文将介绍如何使用PHPStream实现数据流操作,并提供具体的代码示例供参考。1.基本概念在PHP中,Stream是一种抽象的数据流,可以

随着互联网应用及数据量的不断增长,对于数据处理的速度要求也日益提高。而在PHP开发领域,高性能数据流处理技术便成为了一种必备的解决方案。本文将对PHP中的高性能数据流处理技术进行介绍和分析。一、数据流处理的原理在传统的数据处理方式中,常常使用将数据缓存到内存中,再进行读写操作的方式。但是,当数据量过大时,往往会造成内存溢出等问题。而数据流处理技术则不同,它将

FlumevsKafka:哪种工具更适合您的数据流处理?概述Flume和Kafka都是流行的数据流处理工具,用于收集、聚合和传输大量实时数据。两者都具有高吞吐量、低延迟和可靠性等特点,但它们在功能、架构和适用场景方面存在一些差异。FlumeFlume是一个分布式、可靠且高可用的数据收集、聚合和传输系统,它可以将数据从各种来源收集起来,然后存储到HDFS、

实时数据处理:利用GoWaitGroup处理数据流引言:在当今大数据时代,实时数据处理已经成为了许多企业业务运营的重要组成部分。对于需要处理大量数据的应用程序来说,如何高效处理数据流成为了一个关键问题。在Go语言中,可以利用WaitGroup来实现多个goroutine之间的同步,将数据流分割并同时处理,提高数据处理的效率和并发性。本文将详细介绍如何利用G


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
