stalling server client program

D

deadpickle

This program is designed to transfer a file to a client. When i go to
run the program It sends the file once then sits there. The file that
is sent is not closed and there fore conatins no data. I'm not sure
what the problem is.

client
=================================
for (;;) {
use strict;
use IO::Socket;
$|=1;
my $host = shift || '192.168.1.100';
my $port = shift || '7890';
my $sock = new IO::Socket::INET(
PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp');
$sock or die "no socket :$!";
#--------------/ Socket opened \----------------

my $filename = 'L:\School\Meteorology\UAV\uavposition.txt'; # the file
to be sent by the server side
my $fileout = 'C:\uavposition.txt'; # the file in which we should place
received datas
unless (open (FH_RECEPTION, ">", $fileout)){
die "Unable to create $fileout $!";
}

print $sock "FILE|$filename\n";
my @elem=();
my $all_received;
while (my $line=<$sock>){
chomp($line);
#
# Remove the next line in production
#
print "RECEIVED>$line\n";
@elem = split /\|/, $line;
if ($line=~/^FIL\|/){
$elem[1]='' unless (defined $elem[1]);
print FH_RECEPTION $elem[1]."\n";
$all_received=0;
}
elsif ($line=~/^EOT\|/){
print "Normal End of Transmission received\n";
$all_received=1;
last;
}
elsif ($line=~/^NACK\|/){
print "ERROR: $elem[1]\n";
last;
}
else {
print "Do not understand|$line\n";
last;
}
}
close($sock);
close(FH_RECEPTION);
if ((defined $all_received) and ($all_received==1)){
print "Everything has been received in $fileout\n";
}

elsif (defined $all_received) {
print "some part of the file has been received in $fileout\n";
}
sleep (5);
}
exit;

Server
===============================
use strict;
use IO::Socket;
my $sock = new IO::Socket::INET(
LocalPort => '7890', #port to bind socket to
Proto => 'tcp',
Listen => SOMAXCONN,
Reuse => 1);
$sock or die "no socket :$!";
my($new_sock, $c_addr, $buf);
while (($new_sock, $c_addr) = $sock->accept()) {
my ($client_port, $c_ip) =
sockaddr_in($c_addr);
my $client_ipnum = inet_ntoa($c_ip);
my $client_host =
gethostbyaddr($c_ip, AF_INET);
print "got a connection from: $client_host",
" [$client_ipnum]\n";
while (defined ($buf = <$new_sock>)) {
print $buf;
}
}
 
D

DJ Stunks

deadpickle said:
This program is designed to transfer a file to a client. When i go to
run the program It sends the file once then sits there. The file that
is sent is not closed and there fore conatins no data. I'm not sure
what the problem is.
<snip>

There are dozens of modules out there to accomplish what you're trying
to do - there's no need to get your hands dirty and introduce errors
like the one you are struggling with.

How about an IO::All solution?

client:

#!/usr/bin/perl

# client

use strict;
use warnings;

use IO::All;

my $ip = 'localhost';
my $port = 12345;
my $file = shift;

die "Supply file to be transmitted on command line\n"
if not defined $file;

my $socket = io( "$ip:$port" );
$socket->print($_) for io->file( $file )->getlines;

__END__

server:

#!/usr/bin/perl

# server

use strict;
use warnings;

use IO::All;

my $port = 12345;

my $listener = io( ":$port" )->accept;
print while $_ = $listener->getline;

__END__

HTH,
-jp
 
D

deadpickle

DJ said:
There are dozens of modules out there to accomplish what you're trying
to do - there's no need to get your hands dirty and introduce errors
like the one you are struggling with.

How about an IO::All solution?

client:

#!/usr/bin/perl

# client

use strict;
use warnings;

use IO::All;

my $ip = 'localhost';
my $port = 12345;
my $file = shift;

die "Supply file to be transmitted on command line\n"
if not defined $file;

my $socket = io( "$ip:$port" );
$socket->print($_) for io->file( $file )->getlines;

__END__

server:

#!/usr/bin/perl

# server

use strict;
use warnings;

use IO::All;

my $port = 12345;

my $listener = io( ":$port" )->accept;
print while $_ = $listener->getline;

__END__

HTH,
-jp

Your right this looks hugly simplified but, when I run a syntax check I
get the error "Can't locate IO/All.pm in @INC (@INC contains:
C:/Perl/site/lib C:/Perl/lib .) at serverAll.pl line 4." not sure what
that means, any idea? also will this be able to transfer the file over
and over every 5 seconds?
 
D

DJ Stunks

deadpickle said:
Your right this looks hugly simplified but, when I run a syntax check I
get the error "Can't locate IO/All.pm in @INC (@INC contains:
C:/Perl/site/lib C:/Perl/lib .) at serverAll.pl line 4." not sure what
that means, any idea?

it means you need to install the IO::All module before you can use it.


refer to perldoc perlmodinstall

from cmd prompt type ppm3
you will probably have to add the trouchelle.com repository
(from ppm prompt: rep add trouchelle.com http://trouchelle.com/ppm/
)
from ppm prompt: install IO::All

I'll leave you to it.
also will this be able to transfer the file over
and over every 5 seconds?

you can do anything your little heart desires. This is Perl.

-jp
 
D

deadpickle

DJ said:
it means you need to install the IO::All module before you can use it.


refer to perldoc perlmodinstall

from cmd prompt type ppm3
you will probably have to add the trouchelle.com repository
(from ppm prompt: rep add trouchelle.com http://trouchelle.com/ppm/
)
from ppm prompt: install IO::All

I'll leave you to it.


you can do anything your little heart desires. This is Perl.

-jp

When I try to install the modules I recieve the error
"ERROR: Don't know how to unpack
http://trouchelle.com/ppm/MSWin32-x86-multi-thread-5.8/IO-All-0.36.zip"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top