length is 0 with unpack_socketaddr_in

B

bsder

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
 
X

xhoster

bsder said:
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.

You should check the success of your system calls, like "bind" and "accept"

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top