Home Backend Development PHP Tutorial php socket 处理不过来数据流,该如何处理(好像是阻塞了)

php socket 处理不过来数据流,该如何处理(好像是阻塞了)

Jun 13, 2016 pm 12:22 PM
input quot socket

php socket 处理不过来数据流,该如何避免(好像是阻塞了)
php socket 处理不过来数据流,该如何处理(好像是阻塞了)
需求:php接受一个硬件往8888端口上发送数据,如果收到后,应socket_send函数返回“\xFA\x01\x01\xFF\xAA\xAA\x00\x01\x00\x00\x00\x00\x00\x01”,硬件再接收到socket_send发送的数据后,会“滴”一声,但是问题出现了,一个硬件还好,但是当多个硬件同时连接并同时发送数据时,会出现硬件不能连续的回应(即发出“滴”的声音),也就是说能连续发出“滴”声后,便不在响了,大概几秒钟后,又开始响应了,过一会又不行了,几个连接上的硬件都是这样,我已经用了非阻塞模式,还是会这样,求解决方法,下面贴出代码

PHP code
<?phperror_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
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1671
14
PHP Tutorial
1276
29
C# Tutorial
1256
24
IO multiplexing of PHP+Socket series and implementation of web server IO multiplexing of PHP+Socket series and implementation of web server Feb 02, 2023 pm 01:43 PM

This article brings you relevant knowledge about php+socket, which mainly introduces IO multiplexing and how php+socket implements web server? Friends who are interested can take a look below. I hope it will be helpful to everyone.

How to use Spring Boot+Vue to implement Socket notification push How to use Spring Boot+Vue to implement Socket notification push May 27, 2023 am 08:47 AM

The first step on the SpringBoot side is to introduce dependencies. First we need to introduce the dependencies required for WebSocket, as well as the dependencies for processing the output format com.alibabafastjson1.2.73org.springframework.bootspring-boot-starter-websocket. The second step is to create the WebSocket configuration class importorg. springframework.context.annotation.Bean;importorg.springframework.context.annotation.Config

How to use Python's socket and socketserver How to use Python's socket and socketserver May 28, 2023 pm 08:10 PM

1. Socket programming based on TCP protocol 1. The socket workflow starts with the server side. The server first initializes the Socket, then binds to the port, listens to the port, calls accept to block, and waits for the client to connect. At this time, if a client initializes a Socket and then connects to the server (connect), if the connection is successful, the connection between the client and the server is established. The client sends a data request, the server receives the request and processes the request, then sends the response data to the client, the client reads the data, and finally closes the connection. An interaction ends. Use the following Python code to implement it: importso

What to do if php socket cannot connect What to do if php socket cannot connect Nov 09, 2022 am 10:34 AM

Solution to the problem that the php socket cannot be connected: 1. Check whether the socket extension is enabled in php; 2. Open the php.ini file and check whether "php_sockets.dll" is loaded; 3. Uncomment "php_sockets.dll".

Methods and techniques for implementing Socket communication in PHP Methods and techniques for implementing Socket communication in PHP Mar 07, 2024 pm 02:06 PM

PHP is a commonly used development language that can be used to develop various web applications. In addition to common HTTP requests and responses, PHP also supports network communication through Sockets to achieve more flexible and efficient data interaction. This article will introduce the methods and techniques of how to implement Socket communication in PHP, and attach specific code examples. What is Socket Communication Socket is a method of communication in a network that can transfer data between different computers. by S

How to implement laravel input hidden field How to implement laravel input hidden field Dec 12, 2022 am 10:07 AM

How to implement the laravel input hidden field: 1. Find and open the Blade template file; 2. Use the method_field method in the Blade template to create a hidden field. The creation syntax is "{{ method_field('DELETE') }}".

How to encapsulate input components and unified form data in vue3 How to encapsulate input components and unified form data in vue3 May 12, 2023 pm 03:58 PM

Preparation Use vuecreateexample to create a project. The parameters are roughly as follows: use native input. Native input is mainly value and change. The data needs to be synchronized when changing. App.tsx is as follows: import{ref}from'vue';exportdefault{setup(){//username is the data constusername=ref('Zhang San');//When the input box changes, synchronize the data constonInput=;return( )=>({

PHP+Socket series realizes data transmission between client and server PHP+Socket series realizes data transmission between client and server Feb 02, 2023 am 11:35 AM

This article brings you relevant knowledge about php+socket. It mainly introduces what is socket? How does php+socket realize client-server data transmission? Friends who are interested can take a look below. I hope it will be helpful to everyone.

See all articles