jmsloan
asked on
Socket Connections
I have a php script that runs all the time.
#!/usr/local/bin/php
<?php
////////////////////////// ////////// /
//Create connection to Oracle //
////////////////////////// //////////
$connection = ocilogon("username","passw ord","data base");
////////////////////////// ////////// ////////// ///
//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
#!/usr/local/bin/php
<?php
//////////////////////////
//Create connection to Oracle //
//////////////////////////
$connection = ocilogon("username","passw
//////////////////////////
//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
ASKER
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
With all this going on i want to check and see if I am even still connected
That's what feof does. Returns TRUE if the file pointer is at EOF or an error occurs.
ASKER
I tried that and got this error
feof(): supplied argument is not a valid stream resourc
if(feof($fp)){
echo "bad Connection";
}
feof(): supplied argument is not a valid stream resourc
if(feof($fp)){
echo "bad Connection";
}
Interesting. Does fsockopen return a valid resource before that?
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);
}
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);
}
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.
ASKER
$fp had a
Resource id #7
Resource id #7
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
ASKER
I never close the connection though. I must stay connected the ip port at all time.
-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?
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?
ASKER
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?
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?
ASKER
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","passw d","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);
?>
#!/usr/local/bin/php
<?php
//////////////////////////
//Create connection to Oracle //
//////////////////////////
$connection = ocilogon("username","passw
//////////////////////////
//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);
?>
Have to go to work but I'll have a proper look when I get back. Should be fixable
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.php.net/manual/en/function.feof.php