Endless loop

When using feof or fgets script will continue to run until it reaches the maximum mark of 1 minute.

 my $socket = new IO::Socket::INET( LocalPort => '5000', Proto => 'tcp', Listen => 5, ReuseAddr => 1, ); my $connection = $socket->accept(); $connection->send("\0"); # makes PHP send the contents my $data = <$connection>; if($data != "1234") { $connection->send("not accepted"); } else { $connection->send("accepted"); } $connection->close(); 

PHP will not send content until 60 seconds. When using fread it will transmit data (but will only receive \0 back) almost instantly.

 $socket = fsockopen('tcp://192.168.56.101', 5000); // virtualbox IP fwrite($socket, '1234'); echo fread($socket, 128); fclose($socket); 

The above script will execute almost instantly, but only get \0 .

 while(!feof($socket)) { echo fread($socket, 128); } 

Using the above script will not send any data until 60 seconds have been completed.

Question

How do I get PHP to send data and retrieve all the data from a Perl socket without a 60 second runtime?

+4
source share
1 answer

PHP actually sends data right away; the problem is that Perl does nothing with it. If you do this:

 my $data = <$connection>; print $data; #add this line if($data != "1234") { .... 

and then kill the PHP script with Ctrl-C (Windows - I don’t know what this command is if you are using Linux), the Perl script will immediately output β€œ1234”. It depends on the angle bracket operator because it is readline (i.e., it searches for a new line and never finds it). Any of the following made your code work for me:

 //in the PHP file fwrite($socket, "1234\r\n"); 

or

 #in the Perl file (instead of my $data = <$connection>) my $data; $connection->recv($data, 1024); 

Edit: See the comments below before just making a change. As hobbs mentions, recv is probably not the best choice, although it worked, as it is not looking for a new line, and the input is short. Probably the best option is to send a new line in some capacity (variant of the first sentence).

+2
source

Source: https://habr.com/ru/post/1499926/


All Articles