Solved

Socket Connections

Posted on 2004-08-17
17
214 Views
Last Modified: 2011-09-20
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
Comment
Question by:jmsloan
  • 6
  • 6
  • 3
17 Comments
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 11822952
$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
 
LVL 3

Author Comment

by:jmsloan
ID: 11823028
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
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 11823126
That's what feof does.  Returns TRUE if the file pointer is at EOF or an error occurs.
0
 
LVL 3

Author Comment

by:jmsloan
ID: 11823272
I tried that and got this error

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

   if(feof($fp)){
      echo "bad Connection";
    }
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 11823413
Interesting.  Does fsockopen return a valid resource before that?
0
 
LVL 5

Expert Comment

by:basiclife
ID: 11823718
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
 
LVL 5

Expert Comment

by:basiclife
ID: 11823735
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Author Comment

by:jmsloan
ID: 11825616
$fp had a

Resource id #7
0
 
LVL 5

Expert Comment

by:basiclife
ID: 11825679
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
 
LVL 3

Author Comment

by:jmsloan
ID: 11825717
I never close the connection though.  I must stay connected the ip port at all time.  
0
 
LVL 5

Expert Comment

by:basiclife
ID: 11826107
-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
 
LVL 3

Author Comment

by:jmsloan
ID: 11830022
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
 
LVL 3

Author Comment

by:jmsloan
ID: 11830270
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
 
LVL 5

Expert Comment

by:basiclife
ID: 11831915
Have to go to work but I'll have a proper look when I get back. Should be fixable
0
 
LVL 5

Accepted Solution

by:
basiclife earned 125 total points
ID: 11838537


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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now