pro_linux_programming thread: combination Perl and Shell for socket communication
I'm trying to make a communication between 2 computers with different
operating systems, for example the server uses SCO and the client uses Red
Hat.
I want the client send some data information to the server and after the
sending process is done, then the server executes the data. So, I combine
Perl and Shell script to executes the data.
However, I got problem with this combination. Everytime after the data is
sent, then the server executes my shell codes twice and this causes an
error.
I don't know much about socket programming, so please correct me if I'm
wrong.
Here is my perl script:
#!/usr/local/bin/perl
use Socket;
use Shell;
use IO::Handle;
sub my_lock
{
socket(SOCK, AF_INET, SOCK_STREAM, getprotobyname('tcp'))
or die "Cannot create socket: $!";
return(1) if
bind(SOCK, sockaddr_in(9999, inet_aton("localhost")));
if ($! !~ /already/i)
{
return;
}
0;
}
sub my_unlock
{
close(SOCK);
}
socket(SOCK, AF_INET, SOCK_STREAM, getprotobyname('tcp')) || die;
bind(SOCK, sockaddr_in(9999, INADDR_ANY)) || die "$!";
listen(SOCK, SOMAXCONN) || die "Listen failed: $!";
print "Waiting ... \n";
while ( accept(NEWSOCK, SOCK) )
{
shutdown(NEWSOCK, 1);
SOCK->autoflush(1);
$pid = fork();
die "Cannot fork: $!" if (!defined $pid);
if($pid) {
close(PARENT);
print "I'm the parent, process ID $$.
My child is $pid\n";
} else {
close(CHILD);
print "I'm the child. My PID is $$.\n";
}
$a=<NEWSOCK>;
my_lock();
close(NEWSOCK);
# Run the shell script applicant3.c
print "GOTO /usr/mike ... \n";
cd ("/usr/mike");
exec $a;
print "Done ... \n";
my_unlock();
}
and here is my shell script:
PATH=$PATH:/u/appgen5/bin; export PATH
AGHOME=/u/appgen5; export AGHOME
cd /u/appgen5/TP
applicant3 $*
Can anyone help me?
Thanks.