Re: Code Socket Port... Read In and sent out to perl script
From: David (dzhuo_at_looksmart.net)
Date: 03/05/04
- Next message: Paul Kraus: "RE: Code Socket Port... Read In and sent out to perl script"
- Previous message: James Edward Gray II: "Re: Code Socket Port... Read In and sent out to perl script"
- In reply to: Paul Kraus: "Code Socket Port... Read In and sent out to perl script"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
To: beginners@perl.org Date: Fri, 05 Mar 2004 14:41:50 -0800
Paul Kraus wrote:
>
> I need a daemon listening on a port. I need a client side script that just
> takes piped input in and then dumps it out to a file on the other server.
>
> I need more then that but the rest I can handle in the allocated time.
>
> Example
> Files sendtextfile (arguments) server port
>
> cat mytextfile.txt | sendtextfile xxx.xxx.xxx.xxx 12000
>
how does the server know how to name the file?
> Then on the server this file would just be dumped to stdout or disk.
> I will be doing a lot with it actually but this framework will get me
> where I need to be to finish up this project.
>
> The server could be getting hit with more then one file at a time.
something similiar to the following should get you started:
#!/usr/bin/perl -w
use strict;
#--
#-- server.pl
#--
use IO::Socket::INET;
#--
#-- file will simply be named 1,2,3... etc
#--
my $file = 0;
my $server = IO::Socket::INET->new(Listen => 5,
LocalAddr => 'localhost',
LocalPort => 5050,
Proto => 'tcp') || die $!;
$SIG{CHLD} = 'IGNORE';
while(my $client = $server->accept){
$file++;
next if fork;
open(OUT,">$file") || exit;
select OUT;
print while(sysread($client,$_,1024));
close(OUT);
close($client);
exit;
}
__END__
#!/usr/bin/perl -w
use strict;
#--
#-- client.pl
#--
use IO::Socket::INET;
my $server = IO::Socket::INET->new(PeerAddr => 'localhost',
PeerPort => 5050,
Proto => 'tcp') || die $!;
while(<>){
print $server $_;
}
close($server);
__END__
[panda]# ls
file.1 file.2 file.3
[panda]# perl server.pl &
[panda]# perl client.pl file.1
[panda]# perl client.pl file.2
[panda]# perl client.pl file.3
[panda]# ls
1 2 3 file.1 file.2 file.3
notice the file 1, 2 and 3 are created (by the server) from file.1, file.2
and file.3 respectively. i have omitted some error checking code for
simplicity. you should add them when you are coding for production. you can
easily modify the client to accept the following calling convention that
you required:
cat file | client.p <server address> <port>
this is left as an exercise for you ;-)
david
-- s$s*$+/<tgmecJ"ntgR"tgjvqpC"vuwL$;$;=qq$ \x24\x5f\x3d\x72\x65\x76\x65\x72\x73\x65 \x24\x5f\x3b\x73\x2f\x2e\x2f\x63\x68\x72 \x28\x6f\x72\x64\x28\x24\x26\x29\x2d\x32 \x29\x2f\x67\x65\x3b\x70\x72\x69\x6e\x74 \x22\x24\x5f\x5c\x6e\x22\x3b\x3b$;eval$;
- Next message: Paul Kraus: "RE: Code Socket Port... Read In and sent out to perl script"
- Previous message: James Edward Gray II: "Re: Code Socket Port... Read In and sent out to perl script"
- In reply to: Paul Kraus: "Code Socket Port... Read In and sent out to perl script"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|