Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using fsockopen to connect to a server 1

Status
Not open for further replies.

lexer

Programmer
Jun 13, 2006
436
VE
Hi

I have PHP script that uses fsockopen for sending and receiving data from a server (VB program acting as server on a PC), I can send and receive data, I want that my PHP script sends a message when the connection with the server can't be establish. This is my PHP code:
Code:
<script type="text/JavaScript">
<!--
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
//-->
</script>
<style type="text/css">
<!--
.style1 {
	color: #0000FF;
	font-weight: bold;
}
-->
</style>

<label></label>
<form id="form1" name="form1" method="post" action="">
  <p><?php
$server="localhost";
$port=21;
$data="Hi";
//Connection
$conec= fsockopen($server,$port);
//If not connect
if(!$conec) {
	echo ("Cannot Connect");
}
else {

	fputs($conec,"$data");
	$var=fgets($conec,256);
	fclose($conec);
}
		echo "<tr>
				<td> $var </td>					
			</tr>";
		
?>
</table>
  </p>
  <p>
    <input name="Submit" type="submit" onclick="MM_goToURL('parent','menucssn.php');return document.MM_returnValue" value="Home" />
  </p>
</form>

When the server is down I have to receive the message "Cannot Connect" from PHP, I just receive the PHP timeout message at 30 seconds:

Warning: fsockopen() [function.fsockopen]: unable to connect to localhost:21

Fatal error: Maximum execution time of 30 seconds exceeded in C:\wamp\ on line 25


Any Ideas

Thanks!!
 
that means that your server is not listening for socket connections on port 21 on the localhost host name. if you're sure that the port is open then try using 127.0.0.0 instead of localhost.
 
Thanks jpadie, But I was confused.
The VB server program and the PHP script are in the same PC, just that PHP acts as client and the VB program as Sever. If the VB server program is closed (For example) and I'm trying to send data from my PHP script, PHP has to send me the message "Cannot Conect
 
sure. but there is a difference between 'localhost' and the loopback address.

you need to make sure that port 21 is configured to listen on the local server and that it is configured to accept connections from the loopback address.
 
Thanks jpadie, I'm sure that the port 21 is listening because I can send and receive data from the PHP client script to VB server program. But If the VB server program is closed I want that PHP sends the message that cannot connect.
Do I have to used the loop ip address (127.0.0.0) instead of localhost
 
set the timeout value in the fsockopen call.
Code:
$fs = fsockopen($url, 21, $errNo, $errString, 30); //30 sec timeout
then test the errString to see whether there has been a timeout.

Code:
if (!$fs){
  echo $errString;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top