rdbunch has asked for the wisdom of the Perl Monks concerning the following question:
Alright, so I successfully have written a server/client where the server can read from the client just fine. My issue is I want to have the server send a message back to the client once the file transfer is complete but am unable to do so. I am relatively new to Perl. Through some googling I saw some recommendations to use something with the select method but I am having a hard time incorporating it. All I wanna do is send a message back right when I close the file. Any and all help is appreciated!! :) Here is my code:
# Creating TCP socket for server my $server = IO::Socket::INET->new ( LocalPort => $port, Proto => $protocol, Listen => 5, Reuse => 1, ) or die "Socket could not be created, failed with error: $!\n"; print "Socket created using $protocol: $ipaddr\n"; # Adds the server socket as the first handle my $sel = IO::Select->new($server); # Inifinite loop for connecting clients print "Waiting for client connection on port $port\n"; while(1) { # Checks if a client is ready to connect # If so, adds client into an array if(my @can_read = $sel->can_read(1)) { foreach my $fh (@can_read) { my $cmd; if($fh == $server) { # Accept client and do things my $client = $server->accept; $sel->add($client); # Retrieve client information my $client_address = $client->peerhost(); my $client_port = $client->peerport(); print "Client accepted: $client_address, $client_port\ +n"; } else { my ($filename, $filesize); $cmd = <$fh>; if($cmd =~ /^filename/) { chomp($filename = <$fh>); print "Preparing to receive: $filename"; $cmd = <$fh>; } if($cmd =~ /^filesize/) { chomp($filesize = <$fh>); print "File size is: $filesize\n"; $cmd = <$fh>; } if($cmd =~ /^HIC/) { open(FILE, ">out/$filename") or die "File can not +be opened: $!"; # Receiving... while(<$fh>) { print FILE $_; } close FILE; if($filesize != -s "out/$filename") { print "File received is corrupt\n"; } } print "End of connection\n"; $sel->remove($fh); $fh->close; } } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Help writing to socket using IO::Select
by kcott (Archbishop) on Apr 12, 2014 at 23:58 UTC |