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

Hello,
I've been trying to send a file via sockets and I quickly came up with a small server and client script:
Server: #!c:\perl\bin\perl.exe use strict; use IO::Socket; my $server = IO::Socket::INET->new( Listen => 5, LocalAddr => 'localhost', LocalPort => 5498, Proto => 'tcp' ) or die "Can't create server socket: $!"; my $client = $server->accept; open FILE, ">out.txt" or die "Can't open: $!"; while (<$client>) { print $_; } close FILE; Client: #!c:\perl\bin\perl.exe use strict; use IO::Socket; my $server = IO::Socket::INET->new( PeerAddr => 'localhost', PeerPort => 5498, Proto => 'tcp' ) or die "Can't create client socket: $!"; open FILE, "labsearch.txt"; while (<FILE>) { print $server $_; } close FILE;
What I would like to do now is:
-> send many files from differrent machines at varying times (sometimes two computers can send a file at the same time).
(Only one server has to listen indefenitely for one or many clients). -> get the name of the file from the client, send the file and save the file at the server with the same name.

Thanks for your time!

Replies are listed 'Best First'.
Re: Send file(s) via socket
by Roger (Parson) on Jan 22, 2004 at 05:28 UTC
Re: Send file(s) via socket
by Fletch (Bishop) on Jan 22, 2004 at 13:51 UTC
Re: Send file(s) via socket
by pg (Canon) on Jan 22, 2004 at 06:50 UTC

    I think you will need multiplexing, so that when multiple connection co-exist, they will all appear to get a timely response. Take a look at IO::Select.

    Also, better have timeout defined for your socket, so that you can transfer files, and at the same time, periodically check for new connections through accept(), and accept() will no longer block the process (infinitely).

      Yes.. I was thinking of IO::Select as well, though I don't have much experience with it, Ill give it a shot.
      Are you suggesting me to use IO::Select with Net::TCP::Server as Roger had suggested? If so, could you please give me an example?
      Thanks...
Re: Send file(s) via socket
by rupesh (Hermit) on Jan 23, 2004 at 06:39 UTC

    Thanks to Roger, pg and Fletch... I got the thing working and am working to collaborate it with another application..
    I thought it as a cool use for perl and I 've posted it here.