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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help using Sockets in perl 2

Status
Not open for further replies.

pdawlall

Programmer
Mar 6, 2003
5
ZA
Hi guys, can anyone help me. Im actually writing a script to distribute processing amoung 4 workstations.
The problem is that i have been having a problem sending and receiving data between sockets. For the time being im just trying it out on one workstation.
My server code is as follows:

 #!/usr/bin/perl -w
 use IO::Socket;
 use Net::hostent;           
foreach $argnum (0 .. $#ARGV) {
    if ($ARGV[$argnum] eq "-s") {
    $startframe = $ARGV[$argnum + 1];
   }
     if ($ARGV[$argnum] eq "-e") {
     $endframe = $ARGV[$argnum + 1];  }

      if ($ARGV[$argnum] eq "-proj") {
      $projdir = $ARG[$argnum + 1] ; }

    if ($ARGV[$argnum] eq "-rd") {
    $renderdir = $ARG[$argnum + 1];
   }
   if ($ARGV[$argnum] = $#ARGV - 1){
   $filename = $ARG[$argnum];
  }

}

 $PORT = 7700;                  # pick something not in use


 $server = IO::Socket::INET->new( Proto     => 'tcp',
                                  LocalPort => $PORT,
                                  Listen    => SOMAXCONN,
                                  Reuse     => 1,
                                  Type      => SOCK_STREAM,
                                  Family    => AF_UNIX );
   die "can't setup server" unless $server;
 print "[Server $0 accepting clients]\n";


 while ($client = $server->accept()) {
   $client->autoflush(1);
   print $client "Welcome to $0; Are you ready to Render \n";
   $hostinfo = gethostbyaddr($client->peeraddr);
   printf "\n[Connect from %s]\n", $hostinfo->name || $client->peerhost;

  # $server->send($client, $startframe,18)
    or die "Can't send: $!\n";
# THIS IS WHERE THE PROBLEM IS

      printf "Sending Message $startframe";
      # write ($client,$startframe,50);
      # write ($client,$endframe,50);
    close $client;
    }

# My client code is as follows
  #!/usr/bin/perl -w
    use IO::Socket;
    $remote = IO::Socket::INET->new(
                        Proto    => "tcp",
                        PeerAddr => "localhost",
                        PeerPort => "7700",
                    )
                  or die "cannot connect to port at localhost";

      $remote->autoflush(1);

    while ( <$remote> ) {
     read ($remote,$buf,18);
       printf $remote &quot;$buf \n&quot;;
   #  print

       }

    # print &quot;This is the startframe &quot;,$startframe;

     kill(&quot;TERM&quot;,$remote);
 
Not enough time to analyze your code, but:

Try to look here:

And there is another example, with the use the Socket module:
Code:
#--------------------- Server: -----------------------#
#!perl -w
use strict;

use Socket;

my $this_host = 'localhost';
my $port = '9999';

if ($#ARGV > 0) {
   $this_host = $ARGV[0];
   $port   = $ARGV[1];
}

my $server_addr = (gethostbyname($this_host))[4];
my $server_struct = pack(&quot;S n a4 x8&quot;, AF_INET, $port, $server_addr);
my $proto = (getprotobyname('tcp'))[2];
socket(SOCK, PF_INET, SOCK_STREAM, $proto)||  die &quot;Failed to initialize socket: $!\n&quot;;

setsockopt(SOCK, SOL_SOCKET, SO_REUSEADDR,1) || die &quot;setsockopt() failed: $!\n&quot;;
bind(SOCK, $server_struct) || die &quot;bind() failed: $!\n&quot;;
listen(SOCK, SOMAXCONN) || die &quot;listen() failed: $!\n&quot;;

print &quot;Server $this_host is listening on $port $#ARGV ...\n&quot;;

for (;;) {
  my $remote_host = accept(NEWSOCK, SOCK);
  die &quot;accept() error: $!\n&quot; unless ($remote_host);

  my $finish = 0;
  while (<NEWSOCK>) {
    if ($_ eq &quot;stop!\n&quot;) {
      $finish = 1;
      last;
    }
    print;
  }

  close(NEWSOCK);
  last if $finish;
}
#--------------------- Server. -----------------------#
#--------------------- Client: -----------------------#
#!perl -w
use strict;

use Socket;

my $server = 'localhost';
my $port   = '9999';

if ($#ARGV > 0) {
   $server = $ARGV[0];
   $port   = $ARGV[1];
}


my $server_addr =(gethostbyname($server))[4];
my $server_struct = pack(&quot;S n a4 x8&quot;, AF_INET, $port, $server_addr);
my $proto = (getprotobyname('tcp'))[2];
socket(MYSOCK, PF_INET, SOCK_STREAM, $proto)|| die &quot;Failed to initialize socket: $!\n&quot;;
connect(MYSOCK, $server_struct) || die &quot;Failed to connect() to server: $!\n&quot;;

print &quot;Connected to $server $port\n&quot;;

select(MYSOCK);
$| = 1;
select(STDOUT);

my $Input;

for (;;) {
 print &quot;>>&quot;;
 last unless defined ($Input = <STDIN>);
 last if $Input eq &quot;quit\n&quot;;

 print MYSOCK $Input;
 last if $Input eq &quot;stop!\n&quot;;
}
close(MYSOCK);
#--------------------- Client. -----------------------#

Hope this helps (somehow)
Grzegorz
 
I used to do a lot of this sort of coding but now I almost exclusively use PlRPC or XMLRPC; two packages which make coding client-server apps in perl absolute child's play. Ask yourself whether you really need access at the socket level. &quot;As soon as we started programming, we found to our surprise that it wasn't as
easy to get programs right as we had thought. Debugging had to be discovered.
I can remember the exact instant when I realized that a large part of my life
from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top