length is 0 with unpack_socketaddr_in
- From: bsder <snort_sam@xxxxxxxxx>
- Date: Tue, 29 Nov 2005 22:29:35 +1100
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 .
- Follow-Ups:
- Re: length is 0 with unpack_socketaddr_in
- From: xhoster
- Re: length is 0 with unpack_socketaddr_in
- Prev by Date: Re: Upgrade Perl5.8.7
- Next by Date: Re: evaluate sine series.
- Previous by thread: performance and timing issues udp socket
- Next by thread: Re: length is 0 with unpack_socketaddr_in
- Index(es):
Relevant Pages
|