Paul Kraus wrote:
> I hate to ask for code but I am kind of in a crunch to finish a
> project. 

See below.

> 
> I have the network programming with perl but it's just too much to
> read by tomorrow night :)

Buy the Perl Cookbook. It has good examples for this kind of thing.

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

With this, the server won't know what to name the file, so I've just used an
incrementing number. You could extend the protocol to pass the filename.

(why not use FTP or similar?)

> 
> 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.

I'm forking a process to handle each client.

> 
> If you do end up helping me out I appreciate, deeply!
> 
> I am guessing this is easy if it's really involved then I apologize
> now for even asking :)

OK, you owe me a beer :~) Note that this has zilch security or anything to
prevent DoS type attacks, so caveat emptor...

server.pl
---------
  #!/usr/bin/perl

  use strict;
  use IO::Socket::INET;

  my $n = 0;

  my $sock = IO::Socket::INET->new(
      LocalPort => 12000,
      Proto => 'tcp',
      Listen => 5)
      or die "Couldn't create listen socket: $!";

  warn "Waiting for connections\n";
  while (my $client = $sock->accept) {
      $n++;
      defined(my $pid = fork) or die "Couldn't fork: $!";
      next if $pid;

      my $host = $client->peerhost;
      my $file = "file.$n";
      warn "Received connection from $host, writing to $file\n";
      open F, ">$file";
      print F while <$client>;
      close F;
      warn "Connection from $host closed\n";
      exit;
  }

client.pl
---------
  #!/usr/bin/perl

  use strict;
  use IO::Socket::INET;

  my $n = 0;

  @ARGV or die "usage: $0 host [port]\n";
  my $host = shift;
  my $port = shift || 12000;

  my $sock = IO::Socket::INET->new(
      PeerAddr => $host,
      PeerPort => $port,
      Proto => 'tcp')
      or die "Couldn't connect to $host:$port: $!";

  print $sock $_ while <>;

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to