Operating System - HP-UX
1825882 Members
2853 Online
109689 Solutions
New Discussion

Perl code to transfer text strings. Need to transfer file.

 
SOLVED
Go to solution
jerry1
Super Advisor

Perl code to transfer text strings. Need to transfer file.

Does anyone know how to change this perl code
below to transfer a file and not just text
strings. Two parts. A client socket part and
a server socket(daemon) part.

socket.client.pl

#!/usr/bin/perl
#===============================================
# Client -- using object interface
# Support Windows and UNIX
#===============================================
use IO::Socket;
my $sock = new IO::Socket::INET (
PeerAddr => '128.166.11.28',
PeerPort => '9999',
Proto => 'tcp',
);
die "Could not create socket: $!\n" unless $sock;
print $sock "Hi there!\n";
$sock->send("Hi again!\n");

close($sock);



----------------------------------------------
socket.server.pl - enclosed also.

#!/usr/bin/perl
#===============================================
# Server -- using object interface
# Support Windows and UNIX
#===============================================

#use Proc::Daemon;
use IO::Socket;
#Proc::Daemon::Init();

use POSIX qw(setsid);

sub daemonize {
die "Can't fork" unless defined (my $child = fork());
exit 0 if $child;
setsid();
open(STDIN, "open(STDOUT, ">/dev/null");
open(STDERR, ">&STDOUT");
chdir '/';
umask(0);
$ENV{PATH} = '/bin:/sbin:/usr/bin:/usr/sbin';
return $$;
};

&daemonize;
while() {
my $sock = new IO::Socket::INET (
LocalAddr => '128.166.11.28',
LocalPort => '9999',
Proto => 'tcp',
Listen => 5,
Reuse => 1,
);

die "Could not create socket: $!\n" unless $sock;

my $new_sock = $sock->accept();

####################################
# Put a message to file. #
####################################

while(defined($Line = <$new_sock>)) {
open (F,"+>>/var/adm/syslog/socket.server.perl.log");
print F $Line;
system($Line);
close F;
};
close($sock);
}; #End of while cycle

7 REPLIES 7
Peter Godron
Honored Contributor
Solution

Re: Perl code to transfer text strings. Need to transfer file.

Jerry,
not quite sure what you mean. YOu can not "transfer" a file via a socket, but you can transfer the file contents via the socket.
If that is what you want to do, you should be able to enclose your
print $sock "Hi there!\n";
$sock->send("Hi again!\n");
statements with a loop, which read the input file line by line.
jerry1
Super Advisor

Re: Perl code to transfer text strings. Need to transfer file.

I did not know it was not possible to
just transfer a file. If only the contents
of the file, line by line can be transfered, then I would not know how to do that since
my perl coding sucks.
A. Clay Stephenson
Acclaimed Contributor

Re: Perl code to transfer text strings. Need to transfer file.

It's even a little more complicated than that because not all files are textfiles so the concept of a "line" may not exist for some files. Moreover, reading a "line" of text and sending it via a pipe is trivially simple but the receiving process also has to acknowledge that it received that line (or more commonly a block of data) and is ready for another or it has not fully received the data or the data are garbled and need to be re-sent. None of this is very difficult.

The good news is that it really isn't necessary to reinvent the wheel; just use the Net::FTP module and everything will be done for you. If you like, you can choose a non-standard port. Also (and I have not bothered to search because I do not reward laziness), if there is a need for some task there is likely already a module to do that task (or something very close to it). Search the modules at www.perl.org/CPAN and you may very well find exactly what you are looking for.
If it ain't broke, I can fix that.
Ralph Grothe
Honored Contributor

Re: Perl code to transfer text strings. Need to transfer file.

First, if you can afford the time
I would suggest to have a deko at "perldoc perlipc".
It explains the basics of inet socket IPC with lots of valuable code snippets in Perl.
I'm sure it will spark an idea.

Basically it doesn't matter what kind of data you send over a socket pair wire, be it ascii or binary.
Only does your client need to know how to handle it.
For instance the client could even read per single byte via a sysread() in a while loop,
though this is not terribly efficient,
as is also constated in the above mentioned POD.
But as Clay mentioned, why do you want to burden yourself with typical socket coding morass like establishing a proper protocol,
preventing deadlocks, struggling with buffering issues (n.b. don't forgot to autoflush either via $|, or the autoflush method of IO::Handle) while there are lots of higher tcp level modules available whose authors already have gone through all this?
Well, maybe self-education, or very specific needs would justify it?
Madness, thy name is system administration
jerry1
Super Advisor

Re: Perl code to transfer text strings. Need to transfer file.

I could use Expect or the Perl module
Net::FTP or even just a shell script, which
I use all of them today, which I just pass
parms to. But they all require a login id
and password. Which they do not want
hardcoded in any script. Guess I could
just use Openssh scp .

Thanks for you inputs.
A. Clay Stephenson
Acclaimed Contributor

Re: Perl code to transfer text strings. Need to transfer file.

Actually, Net::FTP does not require a hardcoded password. It is perfectly capable of reading a .netrc file just as any FTP client can.
If it ain't broke, I can fix that.
Steven E. Protter
Exalted Contributor

Re: Perl code to transfer text strings. Need to transfer file.

Shalom,

Security note: ftp authenticates in clear text.

.netrc files contents can become known compromising security on the server.

Always disable root ftp on your servers.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com