length is 0 with unpack_socketaddr_in



Hi,

I got the following error message when the client drops its connection after I pressed the Ctl-C.

Bad arg length for Socket::unpack_sockaddr_in, length is 0, should be 16 at Echoer1.pl line 39.

Here is the Server code:
#!/usr/bin/perl

   use strict;
   use Socket;
   # forward declaration
   sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" }

   my $proto = getprotobyname('tcp');
   socket(SOCK,PF_INET,SOCK_STREAM,$proto);
   my $host = INADDR_ANY;
   my $port = 7888;
   my $address = pack_sockaddr_in($port, $host);
   bind(SOCK, $address);
   while (1) {
      listen(SOCK, SOMAXCONN);
      my $hostName = inet_ntoa($host);
      logmsg "server started on port $port, $hostName";

      my $waitedpid = 0;
      my $paddr;

      sub REAPER {
          $waitedpid = wait;
          $SIG{CHLD} = \&REAPER;  # loathe sysV
          logmsg "reaped $waitedpid" . ($? ? " with exit $?" : '');
      }

      $SIG{CHLD} = \&REAPER;

      print STDOUT "Server host: $hostName\n";
      print STDOUT "Server port: $port\n";
      my $cAddress = accept(NEWSOCK,SOCK);

      my $kidpid;
      if (!defined($kidpid = fork())) {
         die "cannot fork: $!";
      }
      elsif ($kidpid == 0) { # child
         my ($cPort, $cHost) = unpack_sockaddr_in($cAddress);
         my $cHostName = inet_ntoa($cHost);
         print STDOUT "Client host: $cHostName\n";
         print STDOUT "Client port: $cPort\n";
         select(NEWSOCK); $| = 1; select(STDOUT);
         print NEWSOCK "Welcome to Code Generator.\r\n";
         do "dbm_lib.pm";
         my $dbm = new dbm_lib();
         $dbm->exec_code();
         while (my $m=<NEWSOCK>) {
            $m =~ s/\n|\r//g;
            print NEWSOCK "Server received $m\r\n";
         }
         close(NEWSOCK);
         exit;
      }
   }

Here is the client code:
#!/usr/bin/perl

   $domain = 2; # Internet domain
   $type = 1; # Sequenced, reliable, two-way connection, byte streams
   $proto = 6; # Transmission Control Protocol (TCP)
   socket(SOCK,$domain,$type,$proto);
   $host = pack('C4', 127,0,0,1); # localhost = 127.0.0.1
   $port = 1024;
   $address = pack('S n a4 x8', $domain, $port, $host);
   bind(SOCK, $address);
   print STDOUT "Client host: ",join('.',unpack('C4', $host)),"\n";
   print STDOUT "Client port: $port\n";
   $sHost = pack('C4', 127,0,0,1); # localhost = 127.0.0.1
   $sPort = 7888;
   $sAddress = pack('S n a4 x8', $domain, $sPort, $sHost);
   connect(SOCK, $sAddress);
   print STDOUT "Server host: ",join('.',unpack('C4', $sHost)),"\n";
   print STDOUT "Server port: $sPort\n";
   select(SOCK); $| = 1; select(STDOUT);
   while ($m=<SOCK>) {
      print STDOUT $m;
      $m = <STDIN>;
      print SOCK $m;
   }
   close(SOCK);
   exit;

Thanks
Sam
.



Relevant Pages

  • Re: setting up RD without a VPN connection ?
    ... Remote Desktop only needs TCP Port 3389. ... > The PC in Brazil (the host) is connected via radio internet connection. ... The client cannot connect to the host. ...
    (microsoft.public.windowsxp.work_remotely)
  • Re: UDP Client Problem
    ... If library you use don't return you peer host IP/port that mean that you ... On return it doesn't automaticaly has the source ip and port (that what i ... mentioning the client port and ip in my server code but still i am getting ...
    (microsoft.public.win32.programmer.networks)
  • RE: application for an employment
    ... scanned a publicly available host. ... First off, I put "port scan" in quotes, meaning that, in concept, the two ... attempt to create a 1:1 equality between client connects and a port scan. ... Rights are rights. ...
    (Security-Basics)
  • Re: Complicated routing/SSH-FTP tunneling problem
    ... # client$ ssh -L port:host:hostport server ... # Talking to 'port' on client, ... # everything to 'hostport' on host: ...
    (freebsd-questions)
  • Tunneling to a third host
    ... i am on a windows box using ssh.com's ssh client. ... my linux box (host) at work does not have a open port to the internet ...
    (comp.security.ssh)