I had a situation where i have to transfer one or many files from 30 differrent clients to one server.
The problem I was facing with is multiplexing while at the same time sending the file name and then the contents of the file ...
I came up with this code with the help of a colleague... it is helpful for files which are in KBs... , but if you were to transfer files whose size were in MBs, then the server might not listen , if its busy listening to other clients...(considering the number of clients are many..)
Please feel free to make any changes and do let me know in case you have a better one!
Thanks!
Server...
#!c:\perl\bin\perl.exe
use strict;
use IO::Select;
use IO::Socket;
my ($filename,$new, $fh, @ready);
print "Listening to sockets on port 8080\n";
my $lsn = IO::Socket::INET->new(
Listen => 1,
LocalAddr => 'localhost',
LocalPort => 8080,
) or die "Can't create server socket: $!";
my $sel = new IO::Select( $lsn );
#can_read -> Return an array of handles that are ready for reading.
while(@ready = $sel->can_read)
{
foreach $fh (@ready)
{
if($fh == $lsn)
{
# Create a new socket
$new = $lsn->accept;
$sel->add($new);
print "Created a new socket $new\n";
}
else
{
# Process socket
print "processing socket $lsn\n";
$filename=<$fh>;
print "trying to open file $filename";
open FILE, ">$filename" or die "Can't open: $!";
while (<$fh>)
{
print FILE $_;
}
close FILE;
# Maybe we have finished with the socket
$sel->remove($fh);
$fh->close;
}
}
}
Client:
#!c:\perl\bin\perl.exe
use strict;
use IO::Socket;
use Cwd;
my $pwdr=cwd;
my $appchoice;
my ($res, $sockpass);
sub client_connect ($$)
{
my $peer_addr=shift;
my $server = IO::Socket::INET->new(Proto => "tcp",
PeerPort => 8080,
PeerAddr => "$peer_addr",
Timeout => 2000)
|| die "failed to connect to the server\n";
open FILE, "<full path of filename>" or die"Error: $!\n";
print $server "ivr\n";
while (<FILE>)
{
print $_;
print $server $_;
}
close FILE;
}
my $message="hello";
client_connect("localhost","$message");
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.