sample code example for bin file

sample code example for bin file

am 26.10.2005 09:49:33 von prem kumar

Hi All,

I am writing an socket program for transferring file over network
through perl sockets.

I like to know the method of transferring the binary files over the
socket between client and server.

A sample code would be a great help !

especially I like to transfer file of type *.zip fie & *.tar files

Thanks
- Prem

Re: sample code example for bin file

am 26.10.2005 16:21:03 von Purl Gurl

prem kumar wrote:

(snipped)

> I am writing an socket program for transferring file over network
> through perl sockets.

Wide Area Network or Local Area Network?

Purl Gurl

Re: sample code example for bin file

am 28.10.2005 10:20:57 von prem kumar

Local Area Network !

Purl Gurl wrote:

> prem kumar wrote:
>
> (snipped)
>
>> I am writing an socket program for transferring file over network
>> through perl sockets.
>
>
> Wide Area Network or Local Area Network?
>
> Purl Gurl
>

Re: sample code example for bin file

am 28.10.2005 16:52:22 von Purl Gurl

prem kumar wrote:
> Purl Gurl wrote:
>> prem kumar wrote:

(snipped)


>>> I am writing an socket program for transferring file over network
>>> through perl sockets.

>> Wide Area Network or Local Area Network?

> Local Area Network !

You don't need a socket to do this. Simply issue a system
command to copy your file to a selected mapped network drive.

Purl Gurl

Re: sample code example for bin file

am 31.10.2005 14:02:10 von chris-usenet

prem kumar wrote:
> I like to know the method of transferring the binary files over the
> socket between client and server.

It's all there in the documentation: perldoc perlipc
Chris

Re: sample code example for bin file

am 07.11.2005 12:34:37 von prem kumar

Hi

I wanted to implement through socket. So that this can also be worked
any network.
I appreciate your help.

thanks
- Prem


Purl Gurl wrote:
> prem kumar wrote:
>
>> Purl Gurl wrote:
>>
>>> prem kumar wrote:
>
>
> (snipped)
>
>
>>>> I am writing an socket program for transferring file over network
>>>> through perl sockets.
>
>
>>> Wide Area Network or Local Area Network?
>
>
> > Local Area Network !
>
> You don't need a socket to do this. Simply issue a system
> command to copy your file to a selected mapped network drive.
>
> Purl Gurl
>

Re: sample code example for bin file

am 09.11.2005 07:50:57 von prem kumar

Hi Chris,

I have written the code like for transferring the binary file from the
client to server.

client code
===========

#elsif ($type eq "BFILE")
# {
# open (SRC_TEMP,"$src_file");
# binmode SRC_TEMP;
# while (read SRC_TEMP, $buffer, 65536)
# {
# print $sock1 "$type+$dest_file+$buffer";
# }
# close SRC_TEMP;
# }


server code
===========
elsif($input[0] eq "BFILE")
# {
# shift(@input);
# $dest_file = $input[0];
# shift(@input);
# $dest_file_temp = &dir_check($dest_file) ;
# open(DEST_TEMP,">$dest_file_temp");
# binmode DEST_TEMP;
# print DEST_TEMP "@input";
# close DEST_TEMP;
# open (LOG_FILE, $log_file);
# print LOG_FILE scalar(localtime),"BFILE copied to $dest_file_temp
on host $host_name!\n";
# close LOG_FILE;
# }

I always get errors. I do not get the size of file the way it is. There
will always a break in the file and will not be of original size.

Any help would be appreciated.

Thanks
- Prem


chris-usenet@roaima.co.uk wrote:
> prem kumar wrote:
>
>>I like to know the method of transferring the binary files over the
>>socket between client and server.
>
>
> It's all there in the documentation: perldoc perlipc
> Chris

Re: sample code example for bin file

am 09.11.2005 09:51:58 von chris-usenet

prem kumar wrote:
> I always get errors.

1. The code you've provided doesn't compile, let alone run. You should
provide a smallest possible example of working code, which you have cut
and pasted (NOT retyped) for us to try.

2. What does "I always get errors" really mean? Does perl complain
about your syntax? Do you get a quotable error when the program runs
(somehow), and if so, what is it? Does your program do something you
don't expect? What do you expect it to do?

Chris

PS. Don't email as well as post; it's bad form.

Re: sample code example for bin file

am 10.11.2005 08:23:06 von prem kumar

Hi chris,

here is snippet :

the command I run is " perl client_sock.pl -h -t BFILE -f
-d "

client code
============
#!/usr/bin/perl

use Socket;
use IO::Socket;
use Getopt::Std;
use Sys::Hostname;

$host_name = hostname;

getopt('hpctfda');

my $host = $opt_h;
my $cmd = $opt_c;
my $type = $opt_t;
my $src_file = $opt_f;
my $dest_file = $opt_d;
my $port = 54321;
my $cus_port = $opt_p;
my $rslt_port;
my $file = $opt_a;
if ($cus_port ne "")
{
$rslt_port = $opt_p;
}
else
{
$rslt_port = $port;
}

my $sock1 = new IO::Socket::INET (PeerAddr => "$host", PeerPort =>
$rslt_port, Proto => 'tcp',Timeout => 0);
die "Cnot create socket: $!\n" unless $sock1;

elsif ($type eq "BFILE")
{
open (SRC_TEMP,"$src_file");
binmode SRC_TEMP;
while (read SRC_TEMP, $buffer, 65536)
{
print $sock1 "$type\n";
print $sock1 "$dest_file\n";
print $sock1 "$buffer\n";
print $sock1 "ZYXZYXZYX\n";
}
close SRC_TEMP;
}
close($sock1);


Server Code
============
#!/usr/bin/perl

# use warnings;
use Socket;
use IO::Socket;
use Getopt::Std;
use Sys::Hostname;
use Cwd;

$dir_current = cwd;

$host_name = hostname;

getopt('p');

$log_file = ">>perl_listener.log";

print "CWD - $dir_current\n";
$home = $ENV{"HOME"};


my $proto = getprotobyname('tcp');
my $port = 54321;

my $cus_port = "$opt_p";
my $rslt_port;

if ($cus_port ne "")
{
$rslt_port = $cus_port;
}
else
{
$rslt_port = $port;
}

sub hostname1 {

my (@bytes, @octets,
$packedaddr,
$raw_addr,
$host_name,
$ip
);

if($_[0] =~ /[a-zA-Z]/g)
{
$raw_addr = (gethostbyname($_[0]))[4];
@octets = unpack("C4", $raw_addr);
$host_name = join(".", @octets);
}
else{
@bytes = split(/\./, $_[0]);
$packedaddr = pack("C4",@bytes);
$host_name = (gethostbyaddr($packedaddr, 2))[0];
}

return($host_name);
}



$host_name_ip = &hostname1($host_name);


# Create 'sockaddr_in' structure to listen to the given port
# on any locally available IP address

my $servaddr = sockaddr_in($rslt_port, INADDR_ANY);

# Create a socket for listening on
socket SERVER, PF_INET, SOCK_STREAM, $proto or die "Unable to create
socket: $!";

# bind the socket to the local port and address
bind SERVER, $servaddr or die "Unable to bind: $!";

# listen to the socket to allow it to receive connection requests
# allow up to 10 requests to queue up at once.
listen SERVER, 10;
# now accept connections
print "Server running on port $rslt_port...\n";


sub sample_cmd
{

@input = split(/<>/,$_[0]);

if($input[0] eq "BFILE")
{
shift(@input);
$dest_file = $input[0];
shift(@input);
$dest_file_temp = &dir_check($dest_file) ;
open(DEST_TEMP,">$dest_file_temp");
binmode DEST_TEMP;
print DEST_TEMP "@input";
close DEST_TEMP;
open (LOG_FILE, $log_file);
print LOG_FILE scalar(localtime)," BFILE copied to $dest_file_temp on
host $host_name!\n";
close LOG_FILE;
}
}

while (accept CONNECTION, SERVER)
{
# select CONNECTION; $| = 1; select STDOUT;
select CONNECTION; $| = 1; select CONNECTION;
$fc = "";

while ()
{
if($_ =~ /ZYXZYXZYX/)
{
last;
}
else
{
$fc = $fc."$_";
}
}
$status = &sample_cmd($fc);
print "The value of the status is : $status\n";
close CONNECTION;
}

here is the problem, with the same logic, I am able to transfer the text
file. but when I transfer the binary file, the destination file is
broken. The size of binary file at the destination location is not the
same as the one from the source.

thanks
- Prem

chris-usenet@roaima.co.uk wrote:
> prem kumar wrote:
>
>>I always get errors.
>
>
> 1. The code you've provided doesn't compile, let alone run. You should
> provide a smallest possible example of working code, which you have cut
> and pasted (NOT retyped) for us to try.
>
> 2. What does "I always get errors" really mean? Does perl complain
> about your syntax? Do you get a quotable error when the program runs
> (somehow), and if so, what is it? Does your program do something you
> don't expect? What do you expect it to do?
>
> Chris
>
> PS. Don't email as well as post; it's bad form.