Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

socket

Status
Not open for further replies.

preethib

Programmer
Jul 20, 2000
39
IN
Hi,

I have a small socket program that basically opens a socket executes a command and gets results. The problem I have is my program just prints the first line of the results. I would like to see all lines in the result.

Here is my program:
---------------------------------------------
#!/usr/perl/bin -wc

use IO::Socket

$remote_host = "test.site.com";
$remote_port = 6666;

$socket = IO::Socket::INET->(PeerAddr =>$remote_host,
PeerPort =>$remote_port,
Proto =>"tcp",
Type => SOCK_STREAM)
or die 'couldn't connect to $remote_host:$remote_port : $@\n";

print $socket "getresults\n";

$answer = <$socket>;
print &quot;$answer&quot;;
close($socket);
---------------------------------
The $answer prints only one line while I want it to print all the lines.

Would help if an expert could answer this,


balji


 
I admittedly don't know a tremendous amount about sockets, but this may work. If not, maybe it should be
@socket = IO::Socket...
and something like
foreach $value (@socket){
print $value;
}
close($socket);

But, I'd try this first.

#!/usr/perl/bin -wc

use IO::Socket

$remote_host = &quot;test.site.com&quot;;
$remote_port = 6666;

$socket = IO::Socket::INET->(PeerAddr =>$remote_host,
PeerPort =>$remote_port,
Proto =>&quot;tcp&quot;,
Type => SOCK_STREAM)
or die 'couldn't connect to $remote_host:$remote_port : $@\n&quot;;

print $socket &quot;getresults\n&quot;;

while( <$socket>){
print; # $_ is implied here
}

close($socket); As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Hi mbaranski

your 3 suggestions:

1.
@socket = IO::Socket...
=---:mad:
2.
foreach $value (@socket){
print $value;
}
close($socket);
=---:mad:
3.
But, I'd try this first.

#!/usr/perl/bin -wc

use IO::Socket

$remote_host = &quot;test.site.com&quot;;
$remote_port = 6666;

$socket = IO::Socket::INET->(PeerAddr =>$remote_host,
PeerPort =>$remote_port,
Proto =>&quot;tcp&quot;,
Type => SOCK_STREAM)
or die 'couldn't connect to $remote_host:$remote_port : $@\n&quot;;

print $socket &quot;getresults\n&quot;;

while( <$socket>){
print; # $_ is implied here
}

close($socket);

=--->works :))

but for some reason I was unable to write to a result file.
...............
open(O,&quot;>result.txt&quot;) ||die .....;

while( <$socket>){
print O $_ ;
}

close($socket);

Also tried got no results for:

...............
$answer =&quot;&quot;;
while( <$socket>){
$answer .= $_ ;
}
print $answer;
close($socket);




balji
 
Hi FOlks,

I'm trying to use socket communication for between server machine and client machine. The client will send 1 line of data and the server will get that data and call the shell script and executes it.
I'm using SCO machine for the server.
The problem is I found the shell script don't work well.
The sample input that I got is like this:
applicant3 &quot;MIke&quot; &quot;&quot; &quot;address1&quot; &quot;mike@a.b&quot; &quot;&quot; &quot;&quot; &quot;&quot; ...

Here is my perl script for the server:

#!/usr/local/bin/perl

use Socket;
use Shell;
use IO::Handle;

sub my_lock
{
socket(SOCK, AF_INET, SOCK_STREAM, getprotobyname('tcp'))
or die &quot;Cannot create socket: $!&quot;;
return(1) if
bind(SOCK, sockaddr_in(9999, inet_aton(&quot;localhost&quot;)));
if ($! !~ /already/i)
{
return;
}
0;
}
sub my_unlock
{
close(SOCK);
}

socket(SOCK, AF_INET, SOCK_STREAM, getprotobyname('tcp')) || die;
bind(SOCK, sockaddr_in(9998, INADDR_ANY)) || die &quot;$!&quot;;
listen(SOCK, SOMAXCONN) || die &quot;Listen failed: $!&quot;;

print &quot;Waiting ... \n&quot;;
while ( accept(NEWSOCK, SOCK) )
{
shutdown(NEWSOCK, 1);
SOCK->autoflush(1);
$pid = fork();
die &quot;Cannot fork: $!&quot; if (!defined $pid);

if($pid) {
close(PARENT);
print &quot;I'm the parent, process ID $$.
My child is $pid\n&quot;;
} else {
close(CHILD);
print &quot;I'm the child. My PID is $$.\n&quot;;
}

$a=<NEWSOCK>;
my_lock();

close(NEWSOCK);

exec $a;
print &quot;Done ... \n&quot;;
my_unlock();
}



Here is my shell script:

# applicant1.sh

AGHOME=`/u/user2/appgen5`; export AGHOME
PATH=$PATH:$AGHOME/bin; export PATH
cd $AGHOME/TP

applicant1 &quot;${1}&quot; &quot;${2}&quot; &quot;${3}&quot; &quot;${4}&quot; &quot;${5}&quot; &quot;${6}&quot; &quot;${7}&quot; &quot;${8}&quot; &quot;${9}&quot; &quot;${10}&quot; &quot;${11}&quot; &quot;${12}&quot; &quot;${13}&quot; &quot;${14}&quot; &quot;${15}&quot; &quot;${16}&quot; &quot;${17}&quot; &quot;${18}&quot; &quot;${19}&quot; &quot;${20}&quot; &quot;${21}&quot; &quot;${22}&quot; &quot;${23}&quot; &quot;${24}&quot; &quot;${25}&quot;


Is there another way to call shell script beside use exec function? Can anyone help me?Thanks.

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top