Jump to content

[SOLVED] fsockopen timeout problems


soadlink

Recommended Posts

Hello,

 

I am working on a script that is having problems using the timeout feature for fsockopen. I want it to work well in the event that there is a connection error. The generic script I have provided loads a LONG list of words (1 word per line), and then connects to a server for every instance of the word. After connecting, it simply echos I connected just fine and moves on to the next word to try and connect again to this same server. But if the socket timed out when connecting to the server (which I have set to 5 seconds), I want it to echo Sorry it timed out. Skipping the current word and then skip the word in the list... and do another fresh connection attempt on the next word in the list.

 

My script does it's job for the most part, except when I physically pull the plug to test the timeout feature. It will usually sit there............. and then finally echo that it timed out.... but then right after that it will keep echoing that message REPEATEDLY without waiting another 5 seconds. It's like the script is not even trying to connect again. It just floods the console with the echo until it is done going through the words. It should wait 5 seconds for EACH attempt, and actually keep trying to make a connection attempt. And if it fails, just skip the word and move on to the next word... waiting 5 seconds again if needed..

 

If you try the script out on your own machine and pull the plug, you will see it detects the timeout ok... but then floods the console with the timeout message until the list is done. If the list is long enough, and you plug the cable back in while it's still flooding, the script will resume as normal once the machine gets back online.

 

<?php
//Path to list
$filepath_to_list = 'c:\\list.txt';

// Load the list of passwords
$loadlist = array_map('rtrim',file($filepath_to_list));

// Start the loop

foreach ($loadlist as $words) {

	$socket = @fsockopen('gpcm.gamespy.com', 29900, $errno, $errstr, 5);

		if (!$socket) {
   				echo 'Sorry it timed out. Skipping the current word' . "\n";
			fclose($socket);

		} else {
			echo "I connected just fine." . "\n";
		}
}
?>

 

Also, I have the @ because on timeout it generates a warning which I'd prefer to hide. Didn't now of a better way to catch this, and would just rather have my own echo warning.

Link to comment
Share on other sites

Nevermind, the problem was when I was physically unplugging the cable from my switch. Windows would release my IP since there was no connection... and php would go nuts because there is no connection.

 

But when I pulled my modem offline (which was not affecting my local IP at all), the timeouts work perfectly because it appears there was just a problem up ahead, but I still had my IP leased to me in windows.

 

Anyways...is there a way to check for an active internet connection in windows, or a local IP... or something before continuing... so php doesn't go nuts like this again?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.