Re: coping txt files over a peer to peer.

2004-01-12 Thread William.Ampeh




Hello David,

Thanks for your the sockets code.  I have a few questions though.

1.  I realized you used TCP instead of UDP for the protocol.  Are there any
advantages of one over the other besides the fact the known drawback of
standard UDP protocol (i..e, no guarantee to sequencing and unreliable
delivery)?

2. Your sysread had a 1024 bytes.  What are the implications of either
increasing or decreasing this number?

Thanks again for your help.

__

William Ampeh (x3939)
Federal Reserve Board


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: coping txt files over a peer to peer.

2004-01-12 Thread david
William Ampeh wrote:

 1.  I realized you used TCP instead of UDP for the protocol.  Are there
 any advantages of one over the other besides the fact the known drawback
 of standard UDP protocol (i..e, no guarantee to sequencing and unreliable
 delivery)?
 

tcp is much more popular than udp as well... :-)


 2. Your sysread had a 1024 bytes.  What are the implications of either
 increasing or decreasing this number?
 

most os is able to handle 1k of memory much more faster than any other size. 
depending on your application, increasing or decreasing this number might 
not matter much. feel free to experiment with a different number.

david
-- 
sub'_{print@_ ;* \ = * __ ,\  \}
sub'__{print@_ ;* \ = * ___ ,\  \}
sub'___{print@_ ;* \ = *  ,\  \}
sub'{print@_,\n}{_+Just}(another)-(Perl)-(Hacker)

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: coping txt files over a peer to peer.

2004-01-09 Thread Andy
Hi,

An app with a ftp client  ferver builtin would do the job just grand.
RFC 959 fully descibes the mechanism, plus I'm sure you could find server
code all ready implemented in perl (a client is already available as part of
libnet).

In a nutshell you have two sockets - the protocol interpreter for commands
and the data transfer process for, well ...

Cheers,
Andy.



- Original Message - 
From: Tino Arellano [EMAIL PROTECTED]
Newsgroups: perl.beginners
To: [EMAIL PROTECTED]
Sent: Wednesday, January 07, 2004 10:31 PM
Subject: coping txt files over a peer to peer.


 Hello folks,

 How do I send the file name used by the client so that the server uses the
same file name
 When it is writing it own file.


 thank you.

 here's the client:

 use IO::Socket;

 my $server = IO::Socket::INET-ne­w(
PeerAddr = 'localhost',
PeerPort = 5050,
Proto= 'tcp'
 ) or die Can't create client socket: $!;


 open FILE, original_file_name;
 while (FILE) {
   print $server $_;
   }
 close FILE;


 Here's the server:

 use IO::Socket;

 my $server = IO::Socket::INET-ne­w(
Listen = 5,
LocalAddr = 'localhost',
LocalPort = 5050,
Proto = 'tcp'
 ) or die Can't create server socket: $!;

 my $client = $server-accept;


 open FILE, copy_file_name or die Can't open: $!;
  while ($client) {
print FILE $_;
}
 close FILE;


 --






 -- 
 __
 Check out the latest SMS services @ http://www.linuxmail.org
 This allows you to send and receive SMS through your mailbox.


 Powered by Outblaze



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: coping txt files over a peer to peer.

2004-01-09 Thread William.Ampeh




Thanks David.

__

William Ampeh (x3939)
Federal Reserve Board


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: coping txt files over a peer to peer.

2004-01-08 Thread William.Ampeh




I have implemented something similar using named pipes instead of sockets.
For send/receive type dialog, you may be better of trying EXPECT.  It is
very easy to learn, and so cool to use.


__

William Ampeh (x3939)
Federal Reserve Board


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: coping txt files over a peer to peer.

2004-01-08 Thread William.Ampeh




Hello Dzhuo,

Your server code will not be able to handle multiple clients.  You need to
undefine $file after closing the client connection.  That is:


close(FILE) if($file);
close($client);
$file = undef;#-- you omitted this line
}

__END__

__

William Ampeh (x3939)
Federal Reserve Board


   
  
  david
  
  [EMAIL PROTECTED] To:  [EMAIL PROTECTED]
   
  .netcc: 
  
   Subject: Re: coping txt files over a 
peer to peer.
  01/07/2004 07:01 
  
  PM   
  
  Please respond   
  
  to dzhuo 
  
   
  
   
  




Tino Arellano wrote:

 Hello folks,

 How do I send the file name used by the client so that the server uses
the
 same file name When it is writing it own file.

this can't be done without the client and server agree on how to retrive
the
file name. one reasonable approach is let the client send the filename
first before any newline and then file content, the server will handle the
task of separating the filename and file content. here is one simple
implementation:

#!/usr/bin/perl -w
use strict;

use IO::Socket::INET;

#--
#-- server.pl
#--
my $server = IO::Socket::INET-new(Listen= 5,
   LocalAddr = 'localhost',
   LocalPort = 5050,
   Proto = 'tcp') || die $!;

#-- have we receive the filename from the client yet?
my $file = undef;

while(my $client = $server-accept){

#--
#-- in your code, you have while($client){...}
#-- which is very dangeous because  will hang if
#-- client and server use a different newline encoding
#-- so please avoid using $client in your networking code
#--
#-- 1024 is also a hack which assume your filename will not
#-- be longer than 1K.
#--
while(sysread($client,$_,1024)){

#--
#-- look for a filename before the first newline
#--
if(/(.+?)\n(.*)/  !$file){

#--
#-- for demo, i will create a filename (sent in by
client)
#-- appending with .by_server. this helps you
#-- to run this demo code without worrying
overwriting
#-- the original file in the same machine
#--
$file = $1 . '.by_server';
open(FILE,$file) || last;
print FILE $2;
}else{
print FILE if($file);
}
}

close(FILE) if($file);
close($client);
}

__END__

#!/usr/bin/perl -w
use strict;

use IO::Socket::INET;

#--
#-- client.pl
#--
my $server = IO::Socket::INET-new(PeerAddr = 'localhost',
   PeerPort = 5050,
   Proto= 'tcp') || die $!;

my $file = 'tmp.txt';

#--
#-- send the file name to server.pl
#--
print $server $file\n;

#--
#-- and then the content
#--
open(FILE,$file) || die $!;
print $server $_ while(FILE);
close(FILE);

close($server);

__END__

[panda]$ perl server.pl 
[1] 14329
[panda]$ perl client.pl
[panda]$ ls
tmp.txt  tmp.txt.by_server

notice the 'tmp.txt.by_server' file created by the server. other than the
name, the file is identical to tmp.txt.

david
--
sub'_{print@_ ;* \ = * __ ,\  \}
sub'__{print@_ ;* \ = * ___ ,\  \}
sub'___{print@_ ;* \ = *  ,\  \}
sub'{print@_,\n}{_+Just}(another)-(Perl)-(Hacker)

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response






-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail

Re: coping txt files over a peer to peer.

2004-01-08 Thread david
William Ampeh wrote:

 Your server code will not be able to handle multiple clients.  You need to
 undefine $file after closing the client connection.  That is:
 
 
 close(FILE) if($file);
 close($client);
 $file = undef;#-- you omitted this line
 }

good catch. thanks.

david
-- 
sub'_{print@_ ;* \ = * __ ,\  \}
sub'__{print@_ ;* \ = * ___ ,\  \}
sub'___{print@_ ;* \ = *  ,\  \}
sub'{print@_,\n}{_+Just}(another)-(Perl)-(Hacker)

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: coping txt files over a peer to peer.

2004-01-08 Thread William.Ampeh




Hello David,


How would you convert your code to allow a bidirectional communication
between the clients and the server without the use of files or named pipes?

That is:
Client send a request to server, waits for server to respond,
Server processes client's request, and send response back to client,
Client views server responds and sends another request to server.
Server responds,
.
.



Very similar to send/receive , send/receive in expect.

Thank you.


__

William Ampeh (x3939)
Federal Reserve Board


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: coping txt files over a peer to peer.

2004-01-08 Thread david
William Ampeh wrote:
 
 Hello David,
 
 
 How would you convert your code to allow a bidirectional communication
 between the clients and the server without the use of files or named
 pipes?
 
 That is:
 Client send a request to server, waits for server to respond,
 Server processes client's request, and send response back to client,
 Client views server responds and sends another request to server.
 Server responds,

keep the request alive so the client and server can talk to each other:

#!/usr/bin/perl -w
use strict;

use IO::Socket::INET;

#--
#-- server.pl
#--
my $server = IO::Socket::INET-new(Listen= 5,
   LocalAddr = 'localhost',
   LocalPort = 5050,
   Proto = 'tcp') || die $!;

while($| = my $client = $server-accept){

while(sysread($client,$_,1024)){

next unless(/^time: (.+)/i);

my $token = $1;
if($token eq 'whole'){
print $client scalar localtime;
}elsif($token eq 'year'){
print $client (localtime)[5]+1900;
}elsif($token eq 'month'){
print $client sprintf(%02d,(localtime)[4]+1);
}elsif($token eq 'day'){
print $client sprintf(%02d,(localtime)[6]);
}elsif($token eq 'done'){
#--
#-- at this point, the client wants to stop talking
#--
print $client -1;
close($client);
$client = undef;
last;
}else{
print $client ?token: $token;
}
}

close($client) if($client);
}

__END__

#!/usr/bin/perl -w
use strict;

use IO::Socket::INET;

#--
#-- client.pl
#--
my $server = IO::Socket::INET-new(PeerAddr = 'localhost',
   PeerPort = 5050,
   Proto= 'tcp') || die $!;

$| = 1;

while(STDIN){
chomp;
last if(/quit/i);
print $server $_;
if(sysread($server,my $answer,1024)){
if($answer =~ /^\?(.+)/){
print STDERR server response with unknown $1\n;
}elsif($answer =~ /^-1$/){
close($server);
$server = undef;
last;
}else{
print STDERR Server: $answer\n;
}
}
}

close($server) if($server);

__END__

transaction:

[panda]# perl server.pl 
[panda]# perl client.pl
time: whole
Server: Thu Jan  8 14:50:46 2004
time: day
Server: 04
time: year
Server: 2004
time: month
Server: 01
time: what
server response with unknown token: what
quit
[panda]#

the server keeps respond to client request until the client:

1. close the connection on its end
2. send a done command

i notice that you might have sent another reply to one of my previous 
message with some of your code. i haven't get a chance to look at them yet. 
pretty busy nowadays, i will take a look at your code when i have more 
time.

david
-- 
sub'_{print@_ ;* \ = * __ ,\  \}
sub'__{print@_ ;* \ = * ___ ,\  \}
sub'___{print@_ ;* \ = *  ,\  \}
sub'{print@_,\n}{_+Just}(another)-(Perl)-(Hacker)

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




RE: coping txt files over a peer to peer.

2004-01-07 Thread Dan Muey
 Hello folks,

Howdy, 
Funny I was just thinking about Sockets today.
I don't use them nitty gritty like this but I would assume you need to do multiple 
send/receive/accept in a little session via your own prtocol.

Something like:

Client hello
Server howdy
Client NAME fred.txt
Server NAMEIS fred.xt Thanks - SENDFILE
Client FILE 
Server Thanks Writing file...FILEOK
Client bye I am done
Server bye hava good one

I'm interested in seeing how to do this, sorry it probably wasn't much help.


 
 How do I send the file name used by the client so that the 
 server uses the same file name 
 When it is writing it own file. 
 
 
 thank you.
 
 here's the client: 
 
 use IO::Socket;
 
 my $server = IO::Socket::INET-ne­w(
PeerAddr = 'localhost',
PeerPort = 5050,
Proto= 'tcp'
 ) or die Can't create client socket: $!;
 
 
 open FILE, original_file_name;
 while (FILE) {
   print $server $_;
   }
 close FILE;
 
 
 Here's the server:
 
 use IO::Socket;
 
 my $server = IO::Socket::INET-ne­w(
Listen = 5,
LocalAddr = 'localhost',
LocalPort = 5050,
Proto = 'tcp'
 ) or die Can't create server socket: $!;
 
 my $client = $server-accept;
 
 
 open FILE, copy_file_name or die Can't open: $!;
  while ($client) {
print FILE $_;
}
 close FILE;
 
 
 --
 
 
 
 
 
 
 -- 
 __
 Check out the latest SMS services @ http://www.linuxmail.org 
 This allows you to send and receive SMS through your mailbox.
 
 
 Powered by Outblaze
 
 -- 
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED] 
http://learn.perl.org/ http://learn.perl.org/first-response



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




RE: coping txt files over a peer to peer.

2004-01-07 Thread Bakken, Luke
 Howdy, 
 Funny I was just thinking about Sockets today.
 I don't use them nitty gritty like this but I would assume 
 you need to do multiple send/receive/accept in a little 
 session via your own prtocol.
 
 Something like:
 
 Client hello
 Server howdy
 Client NAME fred.txt
 Server NAMEIS fred.xt Thanks - SENDFILE
 Client FILE 
 Server Thanks Writing file...FILEOK
 Client bye I am done
 Server bye hava good one
 
 I'm interested in seeing how to do this, sorry it probably 
 wasn't much help.
 
 
  
  How do I send the file name used by the client so that the 
  server uses the same file name 
  When it is writing it own file. 
  

This is a clever idea - you could also send the file name in a fixed
number of bytes at the beginning of the send, and read() that on the
recieving end first.

Luke

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: coping txt files over a peer to peer.

2004-01-07 Thread david
Tino Arellano wrote:

 Hello folks,
 
 How do I send the file name used by the client so that the server uses the
 same file name When it is writing it own file.

this can't be done without the client and server agree on how to retrive the 
file name. one reasonable approach is let the client send the filename 
first before any newline and then file content, the server will handle the 
task of separating the filename and file content. here is one simple 
implementation:

#!/usr/bin/perl -w
use strict;

use IO::Socket::INET;

#--
#-- server.pl
#--
my $server = IO::Socket::INET-new(Listen= 5,
   LocalAddr = 'localhost',
   LocalPort = 5050,
   Proto = 'tcp') || die $!;

#-- have we receive the filename from the client yet?
my $file = undef;

while(my $client = $server-accept){

#-- 
#-- in your code, you have while($client){...}
#-- which is very dangeous because  will hang if
#-- client and server use a different newline encoding
#-- so please avoid using $client in your networking code
#--
#-- 1024 is also a hack which assume your filename will not
#-- be longer than 1K.
#--
while(sysread($client,$_,1024)){

#--
#-- look for a filename before the first newline
#--
if(/(.+?)\n(.*)/  !$file){

#--
#-- for demo, i will create a filename (sent in by client)
#-- appending with .by_server. this helps you
#-- to run this demo code without worrying overwriting
#-- the original file in the same machine
#--
$file = $1 . '.by_server';
open(FILE,$file) || last;
print FILE $2;
}else{
print FILE if($file);
}
}

close(FILE) if($file);
close($client);
}

__END__

#!/usr/bin/perl -w
use strict;

use IO::Socket::INET;

#--
#-- client.pl
#--
my $server = IO::Socket::INET-new(PeerAddr = 'localhost',
   PeerPort = 5050,
   Proto= 'tcp') || die $!;

my $file = 'tmp.txt';

#--
#-- send the file name to server.pl
#--
print $server $file\n;

#--
#-- and then the content
#--
open(FILE,$file) || die $!;
print $server $_ while(FILE);
close(FILE);

close($server);

__END__

[panda]$ perl server.pl 
[1] 14329
[panda]$ perl client.pl
[panda]$ ls
tmp.txt  tmp.txt.by_server

notice the 'tmp.txt.by_server' file created by the server. other than the 
name, the file is identical to tmp.txt.

david
-- 
sub'_{print@_ ;* \ = * __ ,\  \}
sub'__{print@_ ;* \ = * ___ ,\  \}
sub'___{print@_ ;* \ = *  ,\  \}
sub'{print@_,\n}{_+Just}(another)-(Perl)-(Hacker)

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response