Jump to content

Too many open sockets causes timeouts?


soadlink

Recommended Posts

I work with sockets a lot on various languages, such as PHP and VB. I've always been looking for a way to have a bunch of sockets connecting and disconnecting to certain servers because some things I work with require this, but whenever I have too many sockets connecting and disconnecting it always gets very laggy and starts to time out, and even stop all together. This happens with php and VB both.

 

However when I upload this certain php script that is causes me lag on my home pc to my webserver (that is professionally hosted), and have it running in multiple instances there, it never seems to have problems.

 

But when I have too many instances of it running on my machine at home, I can only have a few instances going or it will timeout and give errors.

 

Is this just my machine? It's a decent machine (2.8GHz AMD, 2GB ram). Or my internet connection? (8000/384). Or something in my OS (windows xp pro) limiting the amount of open sockets?

 

The script I use opens/closes about 2-3 sockets a second to the server I am working with. And I can have about 4-5 instances running before it starts to get laggy and timeout. But on my webhost, where the script has also ran before, can handle multiple instances just fine, so its nothing on the server's end that I'm working with.

 

Any ideas?  ???

Link to comment
Share on other sites

By default, Windows XP limits the number of half-open TCP connections to 10 at any one time.  These 10 are shared between ALL processes on the system.  You can check if this is your problem by looking for event id 4226 in your system log.  If the event is present (and occurred recently), look on google for the event id 4226 patcher :)

 

If it's not present, then the problem is elsewhere..

Link to comment
Share on other sites

Ok it seems I can only keep 1 socket going with my script. Even having 2 instances going causes it to freeze. Maybe it has to do with my code:

 

<?php

$count = 0;
$filepath = 'list.txt';
$lines = array_map('rtrim',file($filepath));

foreach ($lines as $line) {
           $count = $count + 1;
$word = $line;
$socket = fsockopen('server.com', 12345, $err, $str, 10);
$first = ''.fread($socket, 512);
fwrite($socket, $word);
$done = ''.fread($socket, 512);
           echo $done . " (" . $count . ")" . "\n";
}
?> 

 

Anyone?  :( It works fine with 1 instance, but multiple instances causes it to just stop shortly after it starts

Link to comment
Share on other sites

By default, Windows XP limits the number of half-open TCP connections to 10 at any one time.  These 10 are shared between ALL processes on the system.  You can check if this is your problem by looking for event id 4226 in your system log.  If the event is present (and occurred recently), look on google for the event id 4226 patcher :)

 

If it's not present, then the problem is elsewhere..

 

Thanks, sorry was posting when you wrote that. I'll give it a look

Link to comment
Share on other sites

The foreach loop is opening a new socket every time it loops back through, so if list.txt is really long, then you could end up with like 100 socket requests... Try

<?php

$count = 0;
$filepath = 'list.txt';
$lines = array_map('rtrim',file($filepath));
$socket = fsockopen('server.com', 12345, $err, $str, 10);
foreach ($lines as $line) {
           $count = $count + 1;



$word = $line;





$first = ''.fread($socket, 512);



fwrite($socket, $word);



$done = ''.fread($socket, 512);
           echo $done . " (" . $count . ")" . "\n";
}
?> 

Link to comment
Share on other sites

The foreach loop is opening a new socket every time it loops back through, so if list.txt is really long, then you could end up with like 100 socket requests... Try

<?php

$count = 0;
$filepath = 'list.txt';
$lines = array_map('rtrim',file($filepath));
$socket = fsockopen('server.com', 12345, $err, $str, 10);
foreach ($lines as $line) {
           $count = $count + 1;



$word = $line;





$first = ''.fread($socket, 512);



fwrite($socket, $word);



$done = ''.fread($socket, 512);
           echo $done . " (" . $count . ")" . "\n";
}
?> 

 

Tried that and it connects 1 time, and the rest of the time I get the error:

 

PHP Notice:  fwrite(): send of 199 bytes failed with errno=10053 An established

connection was aborted by the software in your host machine.

in C:\2.php on line 39

 

The server closes the socket, so I have to re-open it in the loop, or I can't keep writing to the same socket.

 

Also btherl, I didn't see those errors in my logs but I tried the tool anyway and upped the max connections to 40000 and restarted. After rebooting, it still times out the same as before.... and still only lets me have 1 at a time.  :(

 

Any other ideas?

Link to comment
Share on other sites

Hmm.. other likely culprits are your pc-based firewall (if any) and router (if any).  To see if it's the firewall, just turn off the firewall for a while.  Testing the router is more difficult.  But I think it's unlikely to be caused by a router, unless it's REALLY dodgy.

 

The Linksys WRT54G routers have a reputation for choking on large numbers of connections, but that's usually in the hundreds, not in the tens.

 

I notice your script doesn't close the socket.  Might this cause problems?  Even though the other end is closing the connection, maybe you need to close the socket yourself too to free up some resource somewhere.  Seems unlikely but it might help.

Link to comment
Share on other sites

Hmm.. other likely culprits are your pc-based firewall (if any) and router (if any).  To see if it's the firewall, just turn off the firewall for a while.  Testing the router is more difficult.  But I think it's unlikely to be caused by a router, unless it's REALLY dodgy.

 

The Linksys WRT54G routers have a reputation for choking on large numbers of connections, but that's usually in the hundreds, not in the tens.

 

I notice your script doesn't close the socket.  Might this cause problems?  Even though the other end is closing the connection, maybe you need to close the socket yourself too to free up some resource somewhere.  Seems unlikely but it might help.

 

Wow thank you very much, it was my linksys router causing the problem.  8) 8) I was also suspecting this since:

- Multiple languages I've worked with freeze, not just PHP

- It worked on my webserver (they're running higher end gear that's not stopping my connections, and linux)

- Last night before bed I tried 1 instance running on each of my pcs (to see if it was the connection, or the pcs), and they both paused. So it didn't seem to be a pc deal

- I've worked with this and had the sockets connecting on port 80 to a webserver, and it worked, but probably because the router is lenient with that port.

 

I just never physically removed the router and directly connected to test it  :-\. So I directly connected to my cable modem and opened 5 instances of the script, and it ran perfectly. So I will have to tink around with this router ;D. It's a WRT54GX (I use the wired portion though, not wireless). Anything you recommend I check/change in this router? Only think I can see is the Enable or disable the SPI firewall.. The rest of the settings are port forwarding settings/wireless settings (which I have disabled at the moment)/isp settings/etc.. and don't look like they would affect my issue. Or if you can recommend a good SOHO router that isn't so picky  8)

 

But anyways thanks again guys for the help, this makes me sort of like VB again (since it wasn't ever VBs fault)  :P

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.