


PHP socket cannot handle the data flow, how to avoid it (it seems to be blocked)_PHP tutorial
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]); } ?>
Copy after login

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In the era of big data, data management and processing have become an important part of enterprise development. For data stream processing, ApacheNiFi is a leading open source data stream processing tool. The PHP language is very familiar to the development of websites and applications, so how to integrate PHP and Apache NiFi to achieve data flow management and processing? 1. Introduction to ApacheNiFi ApacheNiFi is a powerful, visual data stream processing tool. It can visualize data from various

React Data Flow Management Guide: How to handle front-end data flow gracefully Introduction: React is a very popular front-end development framework. It provides a component-based development method, making front-end development more modular and maintainable. However, when developing complex applications, managing data flow becomes important. This article will introduce some methods of elegantly handling data flow in React and demonstrate specific code examples. 1. One-way data flow React advocates the use of one-way data flow to manage data flow. one-way number

With the increasing number of Internet applications, data processing has become increasingly important. In order to better process data and improve system efficiency and reliability, data flow model design has become an important method. This article will introduce how to design a data flow model in Go language, including flow pipelines, grouping, filters, etc. Stream Pipes Stream pipes are a fundamental component of the data flow model and can pass data from one processing unit to another. In Go language, you can use channel as a pipeline, and channel supports data heterogeneity.

Swoole and Workerman's optimization method for data transmission and data encryption between PHP and MySQL. With the rapid development of the Internet, PHP, as a commonly used server-side programming language, is widely used in the field of Web development. In PHP applications, data transmission and data security have always been the focus of developers. In order to improve the efficiency of data transmission and protect data security, developers usually use some optimization methods. This article will focus on Swoole and Workerman, two commonly used

Title: PHPStream Data Stream Operation Guide In Web development, data stream operations are very common operations and can be used to read file content, send HTTP requests, process network data and other functions. PHP provides a powerful Stream function, making data flow operations easier and more convenient. This article will introduce how to use PHPStream to implement data flow operations and provide specific code examples for reference. 1. Basic concept In PHP, Stream is an abstract data stream that can

With the continuous growth of Internet applications and data volume, the speed requirements for data processing are also increasing. In the field of PHP development, high-performance data stream processing technology has become a necessary solution. This article will introduce and analyze high-performance data stream processing technology in PHP. 1. Principle of data stream processing In traditional data processing methods, the method of caching data into memory and then performing read and write operations is often used. However, when the amount of data is too large, it often causes problems such as memory overflow. Data stream processing technology is different, it will

FlumevsKafka: Which tool is better for your data stream processing? Overview Flume and Kafka are both popular data stream processing tools for collecting, aggregating and transmitting large amounts of real-time data. Both have the characteristics of high throughput, low latency, and reliability, but they have some differences in functionality, architecture, and applicable scenarios. FlumeFlume is a distributed, reliable and highly available data collection, aggregation and transmission system. It can collect data from various sources and then store it in HDFS,

How to use network programming functions in Java for network communication and data transmission Network communication is one of the most important applications in the modern computer field. In Java, we can use network programming functions to implement network communication and data transmission. This article will introduce how to use Java's network programming functions, including establishing TCP and UDP connections, and provide specific code examples. 1. Use TCP for network communication TCP (TransmissionControlProtocol) is a reliable
