soadlink Posted February 9, 2007 Share Posted February 9, 2007 Hello, I'm wondering if it's possible to have multiple open socket connections at a single time within a loop, and have them each echo a "conected" or "timed out message" on a new line as they get them. The script would be connecting to the same server:port. I would like to work this into this script that utilizes only 1 socket at a time: <?php //Path to list $filepath_to_list = 'c:\\list.txt'; // Load the list of words $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 after 5 seconds. Skipping the current word' . "\n"; fclose($socket); } else { echo "I connected just fine." . "\n"; } } ?> You will notice that list.txt is loaded as an array, and for each word in that list a connection to a new socket is created. Well if it is waiting for each socket to get done, that could take a while. So it would be nice to set a number of simultaneous sockets that can be used at once, to make the script work faster. I would prefer this, rather than having to open multiple instances of the script. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 9, 2007 Share Posted February 9, 2007 Why do you need to created a new connection for each entry in the file? Why can't you open the connection, loop through the file and then close the connection? Ken Quote Link to comment Share on other sites More sharing options...
soadlink Posted February 9, 2007 Author Share Posted February 9, 2007 Because the server I am working with closes the socket at a certain point in my script. This is just a stripped down version of it. You have to open new sockets for each word, I'm just looking for a way to get more sockets open at 1 time. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 9, 2007 Share Posted February 9, 2007 If you want more that one socket open at a time, you need to used a different variable to hold each one. One way would be to use an array to store the sockets using the words read from the file as keys: <?php //Path to list $filepath_to_list = 'c:\\list.txt'; // Load the list of words $loadlist = array_map('rtrim',file($filepath_to_list)); $sockets = array(); // Start the loop foreach ($loadlist as $words) { $sockets[$words] = @fsockopen('gpcm.gamespy.com', 29900, $errno, $errstr, 5); if (!$sockets[$words]) { echo 'Sorry it timed out after 5 seconds. Skipping the current word' . "\n"; fclose($sockets[$words]); } else { echo "I connected just fine." . "\n"; } } ?> Ken Quote Link to comment Share on other sites More sharing options...
soadlink Posted February 9, 2007 Author Share Posted February 9, 2007 Wow works great, much faster too. Thanks a lot. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 9, 2007 Share Posted February 9, 2007 Be sure to mark this topic "Solved". Ken Quote Link to comment Share on other sites More sharing options...
soadlink Posted February 9, 2007 Author Share Posted February 9, 2007 Sorry, I jumped ahead of myself. I realized, it's the same speed. Its just using different variables for each socket each time around, but not actually connecting to multiple sockets at once. Maybe it would work out if it split the array of words into chunks of X, and then connect to all those sockets at once, and then loop it around and do the next chunk of words. That way we are connecting to X sockets at once, and X can be whatever I specify. But yes sorry I jumped ahead because we were only working with the initial connect (which is fast). When you add in my code to it, its same speed as my original. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.