How to prevent maximum execution time on my code

My batch file stop receiving data,i think it getz error.

Did you ever fix the while loop??

this is the final code I have now…it throws error in my curl but if i remove the senddata();,the error will throws in $file…

Thank you in advance.





        for(;;){
        $addip = 'xxx.xxx.xx.xx';
        $port = xxxx;
        $cond = true;
            mylistener($addip ,$port ,$cond);
        }



        function   mylistener($addip ,$port ,$cond){

        $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);



        socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);


        socket_bind($sock, $address, $port);


        socket_listen($sock);



        $clients = array($sock);

        while ($cond) {
            $file = fopen('txt.log','a');//error will pointing here if I remove the senddata.

            $read = $clients;
            $write = NULL;
            $except = NULL;
            $tv_sec = 0;

            if (socket_select($read, $write , $except,  $tv_sec) < 1)
                continue;



            if (in_array($sock, $read)) {

                $clients[] = $newsock = socket_accept($sock);

                $key = array_search($sock, $read);

                unset($read[$key]);

            }


            foreach ($read as $read_sock) {


                $data = @socket_read($read_sock, 1024, PHP_NORMAL_READ);


                if ($data === false) {

                    $key = array_search($read_sock, $clients);
                    unset($clients[$key]);
                    break;
                }


                $data = trim($data);


                if (!empty($data)) {

                    fwrite($file,$data."\
");
		   senddata($data);
                }

            } // end of reading foreach

            fclose($file);
        }


        socket_close($sock);
        }
		
		
		function senddata($data){
		      $curl = curl_init();

            curl_setopt_array($curl, array(
                CURLOPT_RETURNTRANSFER => 1,
                CURLOPT_URL => "http://mysite/datareceive.php?$data1=$data1&data2=$data2"

            ));

            $datareturn = curl_exec($curl);//it's pointing here Fatal error: Maximum execution time of 120 seconds exceeded
		
		}
		

        ?>







You still haven’t fixed the infinite loop, and telling us it gets an error without letting us know what the error is doesn’t really help us help you.

I alreardy change it the while loope as you can see i pass parameter on it.the error that i encounter

is maximum execution time limit excedeed.

What happens if you put the curl url into a browser? Does it return any results or does it time out?

Sent from my XT316 using Tapatalk 2

$cond = true;

while ($cond)

It’s exactly the same as before; you are telling the loop " while(TRUE) "

and that is the cause of the script timing out.

At no point the script is $cond unset. You need to unset it at some point to let the loop end.

My curl can send data,but aftr few minuetes the errror maximum execution time will point to my curl_exec.

K, let’s back up here. What are you trying to accomplish??? Why are you creating a perpetual loop?

Having skimmed the code it seems like you are wanting to open a socket periodically to read in a file - a batch process. Hence you should set this up to run once and then use the OS crontab to control invocation frequency. That is, if you need to have this check once every minute set a cron to do so.

Actually I have this in my code


   if ($data === false) {

                    $key = array_search($read_sock, $clients);
                    unset($clients[$key]);
                    $cond= false;//I did not notice in my post this was not included.
                    break;
                } 


@Michael Morris,I am writing the data to a file that the devices sent to our server via socket connection.,I would like to run this script always because it will listen and receive the data that was sent by the devices,…

This is the very first block of code in the script. I assume the purpose of this is to create a makeshift persistent connection:


for( ; ; ){
        $addip = 'xxx.xxx.xx.xx';
        $port = xxxx;
        $cond = true;
            mylistener($addip ,$port ,$cond);
        }

And this is why I also said that $cond needs to be unset at some point, not set to false, because either way, whether $cond is true or false, the while loop inside the mylistener() function below that is infinite. It is also why I suggested to try using break 2 instead of break…but your code has changed a lot since then, and I have no idea where this new snippet belongs, or if we even have the full code, so I’m at a loss here.

As was suggested, this really should be run as a cron, rather than an ad-hoc “always on” connection.

My socket server is continuously running, listening for connections from GPS device
This is the code that I have,my code runs works… ,sometimes it runs 10 hours before it hangs the batch file,sometimes 4 hours then it hangs…sometimes there is no hangs in batch files.I don’t know how can I log the error if my php runs in CLI…and I don’t know if my code is capture again in maximum execution time.

I hope someone can help me on this.


<?php

error_reporting(-1);
ini_set('display_errors', 1);

set_time_limit (0);


for(;;){
    $address_server = 'xxx.xxx.xx.xxx';
    $port_server = xxxx;
    $cond = true;
    mySocket($address_server,$port_server,$cond);
}



function  mySocket($address,$port,$cond){


    $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  

    socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);


    socket_bind($sock, $address, $port);


    socket_listen($sock);

     

    $clients = array($sock);
   



    while ($cond ) {



	    $file = fopen('txt.log','a');
	    $read = $clients;
		
        $write = NULL;
        $except = NULL;
        $tv_sec = 0;
          
       
        
        
        if (socket_select($read, $write , $except,  $tv_sec) < 1){
            continue;
        }

        
        if (in_array($sock, $read)) {


            echo "accepting...\\r\
";
            $clients[] = $newsock = socket_accept($sock);
           
            echo  "There are ".(count($clients) - 1)."client(s) connected\\r\
";
            $key = array_search($sock, $read);
            unset($read[$key]);
			

        }


        foreach ($read as $read_sock) {


            $data = @socket_read($read_sock, 1024, PHP_NORMAL_READ);


            if ($data === false) {

                $key = array_search($read_sock, $clients);



                unset($clients[$key]);

                echo "client disconnected.\
";
     
                $cond = false;
				
                break 2;
            }
			
		
            $data = trim($data);


            if (!empty($data)) {
            
                fwrite($file,$data."\
");
               
            }

        } // end of reading foreach

        fclose($file);
       
    }//end while

    
    socket_close($sock);


}



?>


By the way also what if our server internet connection is interrupted,then I have my server socket that continuously running is this will not causes to hang to my server socket?

Is it okay to use


set_time_limit (0);

I need to run my socket server continuously because it is listening for connections from GPS device…can you help me on my code to work properly.

Thank you in advance.

Not sure how I’d go about doing that in PHP. Seems like a task better dealt with using node.js

@Michael Morris,I have no idea yet for node.js if I switched my code to node.js I will spent a lot of time to get this work…But I have another idea since our production server is windows…I created task scheduler every 10 minutes I will stop the running socket and re-run it again…Is my idea okay for this ?


 <?php

error_reporting(-1);
ini_set('display_errors', 1);

set_time_limit (0);



    $address_server = 'xxx.xxx.xx.xxx';
    $port_server = xxxx;
    $cond = true;
    mySocket($address_server,$port_server,$cond);




function  mySocket($address,$port,$cond){


    $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);


    socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);


    socket_bind($sock, $address, $port);


    socket_listen($sock);



    $clients = array($sock);




    while ($cond ) {



        $file = fopen('txt.log','a');
        $read = $clients;

        $write = NULL;
        $except = NULL;
        $tv_sec = 0;




        if (socket_select($read, $write , $except,  $tv_sec) < 1){
            continue;
        }


        if (in_array($sock, $read)) {

            $clients[] = $newsock = socket_accept($sock);

            echo  "There are ".(count($clients) - 1)."client(s) connected\\r\
";
            $key = array_search($sock, $read);
            unset($read[$key]);


        }


        foreach ($read as $read_sock) {


            $data = @socket_read($read_sock, 1024, PHP_NORMAL_READ);


            if ($data === false) {

                $key = array_search($read_sock, $clients);



                unset($clients[$key]);

                echo "client disconnected.\
";

                continue;// I replace "break 2" to "continue" because all client will be disconnected.if I used break 2.
            }


            $data = trim($data);


            if (!empty($data)) {

                fwrite($file,$data."\
");

            }

        }

        fclose($file);

    }//end while


    socket_close($sock);


}



?>

Thank you in advance