...

View Full Version : Fsockopen help needed



shakeelmscw
11-25-2011, 06:15 AM
I am totally new to php coding and also fsockopen command.

Through Telnet i am able to access my host at 192.168.0.150 10001(port)

Now i need to access the device(same host) through php fsockopen. i need to store the output to a variable and want to apply some calculation on that variable.

below is the coding i am using and error i am getting


<?php
$fp = fsockopen("192.168.0.150", 10001, $errno, $errstr, 10);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: 192.168.0.150\r\n";
$out .= "Connection: Close\r\n\r\n";
echo $out;
echo $fp;
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>


when i open this php file in my xampp local server i am getting below error.

Error message in browser
GET / HTTP/1.1 Host: 192.168.0.150 Connection: Close Resource id #22.60 2.59 2.60 2.59 2.60 2.60 2.60 2.60 2.60 2.59 2.60 2.60 2.60 2.59 2.60 2.60 2.60 2.60 2.60 2.59 2.60 2.59 2.59 2.59 2.59 2.60 2.60 2.60 2.59 2.60 2.60 2.60 2.60 2.59 2.60 2.60 2.60 2.59 2.59 2.60 2.59 2.59 2.60 2.60 2.59 2.60 2.60 2.60 2.60 2.59 2.60 2.60 2.60 2.60 2.60 2.60 2.60 2.60 2.60 2.59 2.60 2.60 2.60 2.60 2.59 2.60 2.60 2.60 2.60 2.60 2.60 2.59 2.60 2.59 2.60 2.60 2.60 2.59 2.60 2.60 2.60 2.59 2.60 2.60 2.60 2.60 2.60 2.60 2.60 2.60 2.59 2.60 2.60 2.59 2.60 2.60 2.60 2.60 2.59 2.60 2.59 2.59 2.60 2.60 2.59 2.60 2.60 2.59 2.60 2.60 2.60
Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\c.php on line 13





mine data is a stream line and not saved in a file. Infect i need to read the data from my internal network device ip 192.168.0.150 port 10001
if i put 192.168.0.150:10001 in browser address bar i am receiving new value every .5 second. i need to sum all these values in a variable and also count the total values received in 30 seconds. so i will be able to get single average value by dividing the sum of values by count of values. Please help me what code should i use?

shakeelmscw
11-25-2011, 06:45 PM
I needs coding for below:-

The coding should start reading values from 192.168.0.150 port 10001 in a variable.

The coding also read the count of values received. then we divide the sum of values by count of values and show the result on screen.

Fou-Lu
11-25-2011, 06:59 PM
Counting totals is a matter of incrementing a variable within your looping structure. Sum is simply adding to another variable. Arrays are a more viable solution IMO.
Displaying them, nope. The problem here is whatever you are talking to doesn't close, so PHP is just running until its exceeded the maximum duration its allowed to run, and then terminates. There is no way to get around a fatal error in PHP (that is why its fatal :/). If these is a specific number you want to read then you can break the loop and close the connection.


Also, you can increase your time limit with the set_time_limit function. I wouldn't recommend it run infinitely though.

shakeelmscw
11-25-2011, 07:57 PM
Can you please give me some sample code i should try. Thanks in advance

Fou-Lu
11-25-2011, 08:06 PM
Can you please give me some sample code i should try. Thanks in advance

An example that does what? Increases the time or limits the number to wait for?

shakeelmscw
11-25-2011, 10:10 PM
An example that does what? Increases the time or limits the number to wait for?

fsockopen example which will read and store values from live stream at 192.168.0.150 port 10001 for 30 seconds.

Fou-Lu
11-26-2011, 04:36 AM
30 seconds is the default time limit. Increase it to 60 using set_time_limit(60), and then track the time pass within a loop.


$iStartTime = time();
$aValue = array();
while (!feof($fp))
{
if (time() - $iStartTime >= 30)
{
break;
}
$aValue[] = fgets($fp, 128);
}
fclose($fp);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum