Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Fsockopen help needed

    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?

  • #2
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

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

  • #4
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can you please give me some sample code i should try. Thanks in advance

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by shakeelmscw View Post
    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?

  • #6
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    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.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    $iStartTime time();
    $aValue = array();
    while (!
    feof($fp))
    {
        if (
    time() - $iStartTime >= 30)
        {
            break;
        }
        
    $aValue[] = fgets($fp128);
    }
    fclose($fp); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •