Jump to content

Multiple socket connections at once?


soadlink

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.  :-[

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.