<?xml version="1.0" encoding="windows-1252"?>
<node id="777204" title="TCP socket and fork" created="2009-07-04 06:57:20" updated="2009-07-04 06:57:20">
<type id="115">
perlquestion</type>
<author id="765974">
adismaug</author>
<data>
<field name="doctext">
Dear Monks,&lt;br&gt;
I am trying to write a script that will open a TCP socket, perform an HTTP get and display the server replay.&lt;br&gt;
The script should fork and all the children should use the same socket (using HTTP version 1.1 and keep alive).

The script below works fine but each process open a new socket which adds unnecessary delay (I am using Time::HiRes to calculate the total time to get 10 pages and save the content).&lt;br&gt;

How can I improve the script so all the forked process wil use the same TCP socket?&lt;br&gt;
Your help please…&lt;br&gt;&lt;br&gt;

&lt;code&gt;
#!/usr/bin/perl
use LWP::Simple;
use Time::HiRes qw(time);
use Parallel::ForkManager;

my $filename = "/tmp/result.log";

while ($z &lt;=200) {
	$z++;
	my $i = 1;

	my $pm = new Parallel::ForkManager(20); 
	my $t0 = time;
	my $num = 10;

	unlink("$filename");
	open FH, "&gt;:utf8", $filename;

	while ($i &lt; $num) {
		$i++;
		$pm-&gt;start and next;
		$contents = get("http://test.com/b.php?page=$i");
		print FH "$contents\?";
		$pm-&gt;finish;
	}
	$pm-&gt;wait_all_children;
	$elapsed = time - $t0;

	print "$elapsed elapsed\n";
}
&lt;/code&gt;

Regards,&lt;br&gt;
Adi.&lt;br&gt;

</field>
</data>
</node>
