socket script dies

L

Larry

Hi guys!

below is the code I've been working on lately...it acts as if he can
handle multiple connections and sends an mp3 file to the browser (IE5)...

#!/perl

use strict;
use warnings;
use IO::Socket;
use IO::Select;

my $socket = IO::Socket::INET->new(LocalPort => '81', Proto => 'tcp',
Type => SOCK_STREAM, Reuse => 1, Listen => 10) || die "$!\n";
my $sel = new IO::Select();

$sel->add( $socket );

my ($fh,$rh_set,$ns,$content,@ready,$file);

while (1)
{

@ready = $sel->can_read;

foreach $fh ( @ready ) {

if ($fh == $socket)
{

$ns = $socket->accept;
$sel->add($ns);

} else {

recv($fh,$content,10000,0);
print $content;

syswrite $fh, "HTTP/1.1 200 OK\n";
syswrite $fh, "Apache/1.3.0 (unix)\n";
syswrite $fh, "Cache-Control: no-cache\n";
syswrite $fh, "Content-type: application/octet-stream\n";
syswrite $fh, "Content-Disposition: attachment;
filename=stuck.mp3\n";
syswrite $fh, "\n";

open(BYNA, "<stuck.mp3") || die "$!\n\n";
binmode(BYNA);
while( read(BYNA,$file,1024) )
{
syswrite $fh, $file;
}
close(BYNA);


$sel->remove($fh);
$fh->close;
}

}

}

__END__;

unfortunately when i close the connection at the browser side (by
pressing X) or when i refuse the download (by pressing "cancel") the
script dies...(without any error) or i'd say the script exits (so i've
got to run it again...how boring)

I can not really sort it out...

can anyone help with this?

thanks
 
L

Larry

Larry said:
Hi guys!

below is the code I've been working on lately...it acts as if he can
handle multiple connections and sends an mp3 file to the browser (IE5)...

#!/perl

use strict;
use warnings;
use IO::Socket;
use IO::Select;

my $socket = IO::Socket::INET->new(LocalPort => '81', Proto => 'tcp',
Type => SOCK_STREAM, Reuse => 1, Listen => 10) || die "$!\n";
my $sel = new IO::Select();

$sel->add( $socket );

my ($fh,$rh_set,$ns,$content,@ready,$file);

while (1)
{

@ready = $sel->can_read;

foreach $fh ( @ready ) {

if ($fh == $socket)
{

$ns = $socket->accept;
$sel->add($ns);

} else {

recv($fh,$content,10000,0);
print $content;

syswrite $fh, "HTTP/1.1 200 OK\n";
syswrite $fh, "Apache/1.3.0 (unix)\n";
syswrite $fh, "Cache-Control: no-cache\n";
syswrite $fh, "Content-type: application/octet-stream\n";
syswrite $fh, "Content-Disposition: attachment;
filename=stuck.mp3\n";
syswrite $fh, "\n";

open(BYNA, "<stuck.mp3") || die "$!\n\n";
binmode(BYNA);
while( read(BYNA,$file,1024) )
{
syswrite $fh, $file;
}
close(BYNA);


$sel->remove($fh);
$fh->close;
}

}

}

__END__;

unfortunately when i close the connection at the browser side (by
pressing X) or when i refuse the download (by pressing "cancel") the
script dies...(without any error) or i'd say the script exits (so i've
got to run it again...how boring)

I can not really sort it out...

can anyone help with this?

thanks

why!?!??!?!
 
A

AaronJSherman

Your script may be responding to a signal like SIGPIPE. You need to
trap it and deal with it as appropriate. See %SIG in the perlvar
manpage.
 
A

A. Sinan Unur

why!?!??!?!

Why did you post an exact copy of your previous post with this appended?

On the other hand, it does summarize my feelings at this moment.

Hmmm ...

Sinan.
 
L

Larry

Your script may be responding to a signal like SIGPIPE. You need to
trap it and deal with it as appropriate. See %SIG in the perlvar
manpage.

how could I trap it?
 
C

Christopher Nehren

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

how could I trap it?

'perldoc -q signal' gives a good start; follow that with 'perldoc
perlipc'.

Best Regards,
Christopher Nehren
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (FreeBSD)

iD8DBQFBvhW/k/lo7zvzJioRAt3EAJ9V8Q8VjoQMkfe5fUvpeRE5Np70GACdGrRn
q8NvArh8HvlFB3R/AI/rEbM=
=NuRp
-----END PGP SIGNATURE-----
 
L

Larry

Your script may be responding to a signal like SIGPIPE. You need to
trap it and deal with it as appropriate. See %SIG in the perlvar
manpage.

well...i read up on %SIG a little I couldn't understand much about it
however.

anyway I discovered and put this at the very bottom of my script:

END {
print "wow!\n";
}

when i stop the browser the script exits nad write wow!
 
L

Larry

Larry said:
well...i read up on %SIG a little I couldn't understand much about it
however.

anyway I discovered and put this at the very bottom of my script:

END {
print "wow!\n";
}

when i stop the browser the script exits nad write wow!

I'm doing this right now...but it doesn't work at all!!!

SERVER: while ($client = $socket->accept)
{

# do some stuff...

}

END {
print "wow\n";
goto SERVER;
}

this is what I get:

# END failed--call queue aborted.
wow
 
L

Larry

I also tried with this:

foreach $var (sort(keys(%SIG))) {
$val = $SIG{$var};
${var}='IGNORE';
}

but it didn't work...

I'M HAD!
 
L

Larry

eureka!

it seems like i've sorted it out!

at first, I tried this:

#!/perl

@nomi = keys(%SIG);
for my $nomesig (@nomi) {$SIG{$nomesig} = 'IGNORE';}

but i found that this would be better:

#!/perl

sub handleruniversale
{
print "\ngot a signal $_[0]\n";
$SIG{$_[0]} = \&handleruniversale;
}

@nomi = keys(%SIG);
for my $nomesig (@nomi) {$SIG{$nomesig} = \&handleruniversale;}

so i discovered that i gotta put this on my script:

local $SIG{NUM13} = 'IGNORE';

what the hell is $SIG{NUM13} ???

see you guys!

Gaetano
 
B

Ben Morrow

Quoth Larry said:
eureka!

it seems like i've sorted it out!

Do you usually talk to yourself? :)
at first, I tried this:

#!/perl

@nomi = keys(%SIG);
for my $nomesig (@nomi) {$SIG{$nomesig} = 'IGNORE';}

YEUCH! You'll ignore all sorts of signals that you really shouldn't
(like SIGTERM, for example).
but i found that this would be better:

#!/perl

sub handleruniversale
{
print "\ngot a signal $_[0]\n";
$SIG{$_[0]} = \&handleruniversale;
}

@nomi = keys(%SIG);
for my $nomesig (@nomi) {$SIG{$nomesig} = \&handleruniversale;}

Hmmm... this is little better, except for testing.
so i discovered that i gotta put this on my script:

local $SIG{NUM13} = 'IGNORE';

what the hell is $SIG{NUM13} ???

On my system (linux 2.6/i686) signal number 13 is SIGPIPE, so it looks
like when your perl was built it couldn't get hold of the signal names.
I would suggest rebuilding perl, and making sure you watch for any
errors of that sort.

Ben
 

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,059
Latest member
cryptoseoagencies

Latest Threads

Top