Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

Socket Connections

I have a php script that runs all the time.  

#!/usr/local/bin/php

<?php
 
/////////////////////////////////////
//Create connection to Oracle //
////////////////////////////////////
$connection = ocilogon("username","password","database");
 
/////////////////////////////////////////////////
//Define connection to valuelink varibles //
////////////////////////////////////////////////
$host = "XXX.XXX.XXX.XXX";
$port = "XXXX";
$running = "Y";

///////////////////////////////////
//Open a client connection   //
//////////////////////////////////
$fp = fsockopen($host, $port, $errno, $errstr);

while ($running == "Y"){
 
   //////////////////////////////////////////////////////////////////////////////
   //if connection to oracle or connection to the ip go down restart cue1.php  //
   //////////////////////////////////////////////////////////////////////////////
   if(!$fp){
     echo "bad connection";
   }

   sleep(2);

}

This is a script that is run at the command line of a linux machine.  I will run the scipt and after about 30 to test the script I will kill the connection to the port.  The script does not display my "bad connection" error message.  

How ever if i kill the connection prior to starting the script I will get the error message.  

Why would php  not understand the connection went down after the script had started.  Does the $fp = fsockopen($host, $port, $errno, $errstr);  Only check the first time it is initialized?

The idea of this script is to always run it in the background, if for some reason the connection to the ip port goes down I run another script that kill this one and restarts it at a late time.

I hope this all make sense,  let me know if it doesn't

Thanks,

jmsloan



0
jmsloan
Asked:
jmsloan
  • 6
  • 6
  • 3
1 Solution
 
snoyes_jwCommented:
$fp is assigned the socket resource number, so even if the socket closes, !$fp will return true.  Try feof($fp) instead of !$fp.
http://www.php.net/manual/en/function.feof.php
0
 
jmsloanAuthor Commented:
That's not what I am trying to get at.  I want the connection established all the time.  If it goes down, which what I am testing for then I want to do something special.  So for instance if the connection isn't down I will select some data and write to the port.  Then it will loop.  I then check for more data to send if it exist i send it.  

With all this going on i want to check and see if I am even still connected
0
 
snoyes_jwCommented:
That's what feof does.  Returns TRUE if the file pointer is at EOF or an error occurs.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
jmsloanAuthor Commented:
I tried that and got this error

feof(): supplied argument is not a valid stream resourc

   if(feof($fp)){
      echo "bad Connection";
    }
0
 
snoyes_jwCommented:
Interesting.  Does fsockopen return a valid resource before that?
0
 
basiclifeCommented:
Why not close the socket and then attempt to re-open it in the loop? ie

while ($running == "Y"){
 
   //////////////////////////////////////////////////////////////////////////////
   //if connection to oracle or connection to the ip go down restart cue1.php  //
   //////////////////////////////////////////////////////////////////////////////
   $fp = fsockopen($host, $port, $errno, $errstr);
   if(!$fp){
     echo "bad connection";
   } else {
     fclose($fp);
   }

   sleep(2);

}
0
 
basiclifeCommented:
As snoyes_jw said: Once the connection is estavlished, $fp is a standard fixed variable. It is set to either the resource number or null upon connect, after that it's just a number that is used tor efer to the connection.
0
 
jmsloanAuthor Commented:
$fp had a

Resource id #7
0
 
basiclifeCommented:
So all it's saying is that to talk to the file it's connected to it will try talking to connection #7 _BUT_ that doesn't mean that the link is still open, just that when it opened it was connection 7. The variable doesn't get updated automatically. You need to re-test the connection every time. The code I gave above should work fine
0
 
jmsloanAuthor Commented:
I never close the connection though.  I must stay connected the ip port at all time.  
0
 
basiclifeCommented:
-er- I thought this script just tested the connection?

If you're actually reading / writing to the connection FROM THIS SCRIPT, the when the read/write fails, echo your error message and re-connect. That's a different issue altogether

Is this script just for testing the connection or is it for processing as well?
0
 
jmsloanAuthor Commented:
It is a connection that will be connected all the time.  When new data comes into the database it will select it and write that data to the port, in order to write that data to the port the connection has to be established.  

Basically I am writing data to another companies computer that say our computer has to be connected to theirs at all time.  


Is that making more sense?
0
 
jmsloanAuthor Commented:
Here is my actual code.  When I disconnect the rounter it still thinks the connection is open and tries to send

#!/usr/local/bin/php
 
<?php
 
/////////////////////////////////////
//Create connection to Oracle //
////////////////////////////////////
$connection = ocilogon("username","passwd","dbname");
 
////////////////////////////////////////////////
//Define connection to valuelink varibles //
////////////////////////////////////////////////
$host = "xxx.xxx.xxx.xxx";
$port = xxxxx;
$running = "Y";
 
///////////////////////////////////
//Open a client connection   //
///////////////////////////////////
$fp = fsockopen($host, $port, $errno, $errstr);
 
 
/////////////////////////////////////////////
//Create loop that continuously runs loop  //
/////////////////////////////////////////////
while ($running == "Y"){
 
   ////////////////////////////////////////////////
   //Check to see if transactions exist in cue 1 //
   ////////////////////////////////////////////////
   $sql = "select count(*)COUNT from chgacct.credit_send
            where status = 'O'";
 
   $statement = OCIParse ($connection, $sql);
     OCIExecute ($statement);
 
   while (OCIFetchInto ($statement, $row, OCI_ASSOC)){
      $count = $row['COUNT'];
   }
 
   ///////////////////////////////////////
   //Process transactions in cue 1  //
   //////////////////////////////////////
   if($count != "0"){
 
      $sql1 = "select output
                from chgacct.credit_send
               where status = 'O'";
 
      $statement1 = OCIParse ($connection, $sql1);
        OCIExecute ($statement1);
 
      while (OCIFetchInto ($statement1, $row1, OCI_ASSOC)){
         $output = $row1['OUTPUT'];
 
            //////////////////////////////////
            //Check port1 connectivity  //
            //////////////////////////////////
            if($fp){
 
               //Write the user string to the socket
               fputs ($fp, $output, strlen($output));
 
               $ss = socket_set_timeout($fp, 15);
 
               if($ss){
 
                  fputs ($fp, $bodyheader, strlen($bodyheader));
                  //Get first five bytes to determine full size
                  $bodyresult = fread($fp, 5);
                  $bytes_left = socket_get_status($fp);
                  //Get the remaining results with known byte size
                  $bodyresult .= fread($fp, $bytes_left["unread_bytes"]);
 
               } //end no timeout
            }else{
               echo "bad connection";
            } //end good/bad connection
         } //end if sale or return
      } //end while select
 
   } //end if count !0
 
   sleep(2);
 
} // end of while = y loop
 
//close the connection
    fclose($fp);
 
?>
0
 
basiclifeCommented:
Have to go to work but I'll have a proper look when I get back. Should be fixable
0
 
basiclifeCommented:


The following will test whether or not the connection has timed out

while ($running == "Y"){
 
   ////////////////////////////////////////////////
   //Check to see if transactions exist in cue 1 //
   ////////////////////////////////////////////////
   $result = stream_get_meta_data($fp);
   if($result[0]==True || feof($fp)) {
      //Connection Timed out or died
      $fp = fsockopen($host, $port, $errno, $errstr);
   }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 6
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now