Re: Code Socket Port... Read In and sent out to perl script

From: David (dzhuo_at_looksmart.net)
Date: 03/05/04


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$;


Relevant Pages

  • Re: Formatting ASCII to be read by Windows NotePad
    ... No need to shout - neither Perl nor Cisco are acronyms. ... I've had issues in the past uploading to a MacOS based ftp server ... I've been using a Mac for ten years, ... different ftp clients, including the command line ftp client, so I ...
    (comp.lang.perl.misc)
  • Re: How to embed javascript functionality into a Perl CGI script?
    ... Perl code does not run on clients, ... >> browser itself...such as JavaScript. ... JSP executes on server side and Javascript on the client. ...
    (comp.lang.perl.misc)
  • Re: Starting with SOAP
    ... and I'm not even sure it is Perl - likely PHP. ... It doesn't mean the client will work with another ... language's server, or the server will work with another language's client. ... I'm trying to get something working before I tackle passing the form data to a third-party script. ...
    (comp.lang.perl.misc)
  • Re: Soap Server in perl
    ... responce from the server. ... Sorry Ian I donot mean to say like that but actually I am new to perl ... and Perl in which you can install SOAP::Lite then you'll have to implement the SOAP support some other way. ... Move your module unchanged to your web server and write a generic SOAP dispatcher (designated by the proxy in the client) that hands off ...
    (comp.lang.perl.misc)
  • sysread(socket..) problem on perl 5.8.0 linux
    ... I've been debugging a socket read problem using perl 5.8.0 on RH8.0. ... The problem is that the client sysread returns 0 when the server has ... sub server ...
    (comp.lang.perl.misc)