Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Socket Connections

Posted on 2004-08-17
17
Medium Priority
?
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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 500 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

670 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