Here is the code. I am new to perl programming. I have to spawn four sub processes but initially trying to get 1 process to send the files. So the same socket connection is used to send the data and wait till they give us response and receive the response and write to a file.
my $BUFSIZE = 32000;
my $varhex3= "\x03";
$SIG{USR1} = "doneit";
# I added the below for trial and error not sure what this #does.
my $nonblocking = 1;
select(S);
$| = 1;
select(STDOUT);
for( my $childcounter = 1; $childcounter <= 1; $childcounter++ ) {
my $pid = fork();
if ($pid) {
# parent
push(@childs, $pid);
} elsif ($pid == 0) {
# child
if ($childcounter == 1) {
@mylist1 = @{$hash1{$childcounter}};
my $socket1 = &opensock();
$myport = $socket1->sockport();
foreach $File (@mylist1){
$BaseFName = basename($File);
my $outputdir = "directory goes here";
my $outputfile = "$outputdir\/$BaseFName".".res";
open(INFILE,"$File");
$data=<INFILE>;
close INFILE;
# print $sock "$data";
$socket1->send ($data);
open (RESPONSEFILE, ">$outputfile") or die $!;
my $lastchar = "";
my $Stop_Read = 0;
while (! $Stop_Read) {
$lastchar = "";
$data1 = "";
recv($socket1,$data1,$BUFSIZE,0);
sleep 2;
my $mylen = length($data1);
$lastchar = substr ($data1, $mylen-1, 1);
# print STDOUT $data1;
print RESPONSEFILE $data1;
if ($lastchar == $varhex3){
$Stop_Read = 1;
}
}
close RESPONSEFILE;
}
close($socket1);
}
}
sub doneit {
$gotone = 1;
}
sub opensock{
my $sock = new IO::Socket::INET (
PeerAddr => 'IP address',
PeerPort => 'Port number',
Proto => 'tcp',
Listen => SOMAXCONN,
Reuse => 1,
Timeout => 60,
);
die "Could not create socket: $!\n" unless $sock;
#select (sock); $| = 1;
}
|