petesull has asked for the wisdom of the Perl Monks concerning the following question:

Hello first of all note that I am new to perl :) i am writing a program that opens a socket on port 1010, then waits for client connection. Once the connection is sensed, it waits for a file to be sent through the socket from the client to the server (xml file). I need some help with the "waiting for file" part.... any ideas? I have looked at sysread() and recv but i found with recv that it just checked and then says cannot connect. sysread i looked at but i could understand it as i have not created the socket using the "easy way" so to speak. If any needs to have a look at my existing code, thats fine. Thanks, Pete

Replies are listed 'Best First'.
Re: File/Data Streaming over a socket
by Joost (Canon) on Mar 17, 2005 at 14:40 UTC
    Without any code it's hard to see what you're doing wrong, but how about using IO::All? Following code taken more or less straight from the docs:
    #!perl -w use strict; use IO::All; # create server my $socket = io('localhost:1080')->fork->accept; print "Connection opened\n"; # slurp incoming data my $xml = $socket->slurp; print "Data recieved:\n"; print $xml;
    Update: I used port number to 1080, so you don't need to run this as root.

Re: File/Data Streaming over a socket
by zentara (Cardinal) on Mar 17, 2005 at 20:57 UTC
    I usually use Net::EasyTCP for this, but this is the simplest code you could probably find. If you are wanting to use this for multiple clients, you will have to go to a "forking design", unless you files are very small. With code as simple as this, you are bound to run into problems, for which you will see the benefit of using Net::Easy::TCP. For instance, is this open to the net?, you should have a port-password at the very least.
    ####Server########### #!/usr/bin/perl use IO::Socket; my $server = IO::Socket::INET->new( Listen => 5, LocalAddr => 'localhost', LocalPort => 5050, Proto => 'tcp' ) or die "Can't create server socket: $!"; my $client = $server->accept; open FILE, ">out" or die "Can't open: $!"; while (<$client>) { print FILE $_; } close FILE; __END__ #and the Client ########## #!/usr/bin/perl use IO::Socket; my $server = IO::Socket::INET->new( PeerAddr => 'localhost', PeerPort => 5050, Proto => 'tcp' ) or die "Can't create client socket: $!"; open FILE, "in"; while (<FILE>) { print $server $_; } close FILE

    I'm not really a human, but I play one on earth. flash japh
      Please forgive my ignorance, but how do you implement a socket password? This will be open to the internet and files that it will receive will at most, 200Kb. Thanks all for your help to!
        It's alot easier to use Net::EasyTCP. Here are a couple of snippets that I posted awhile back Sockets-File-Upload with Net::EasyTCP. It has port passwords, usernames and logons, and is encrypted. Read the Net::EasyTCP docs for details. Now Net::EasyTCP uses IO::Select, so that means if a second client connects, while another is currently uploading, it will have to wait for the first upload to finish.(Remember the forking? This dosn't fork). Net::EasyTCP also has a "refuse connection" list, you can employ if out on the NET and some ipaddress is hacking, but not logging on. You can do something like count logon attempts, then after 10 failures, put them in a @banned-ips list.

        Also, when I wrote that snippet, there was a problem with RSA(as mentioned), but the module now has an option to handle this:

        my @nocrypt = qw(Crypt::RSA); #too slow, but more secure #my @nocompress = qw(Compress::LZF); #just testing this feature $server = new Net::EasyTCP( host => $host, mode => 'server', port => $port, password => $portpassword, # if asymmetric encryption # use a port password donotencryptwith => \@nocrypt, # donotencrypt => 1,
        I have another snippet that implements these ideas at ztk-enchat encrypted server client

        I'm not really a human, but I play one on earth. flash japh

        1) A socket is just an association between a protocol ( port, address ) and a descriptor ( filehandle ).

        2) An application connected to the public internet with only password protection offers only false security.

        3) Go to http://search.cpan.org and search for security.

        4) Posting a new question in an existing thread is a faux pax.

        s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}
Re: File/Data Streaming over a socket
by starbolin (Hermit) on Mar 17, 2005 at 16:43 UTC

    If this is going to be production code you should use one the many excellent CPAN modules. Consider that in error conditions your code could lockout the rest of the system or corrupt files.

    For education purposes Perl's <> operator may do what you want.

    while (defined($line = <SOCK>)) { print $line; }

    If not perlfaq8 has a full server example.

    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}