Hi guys,
I'm using the code from
http://www.perldoc.com/perl5.6.1/lib/Sys/Syslog.html
to write a Socket-Server-Client connection. The Server sends his lines with $EOL = "\015\012", but the client just does not print anything until the server is killed?
This is my code
The SERVER:
$WaitSec = 10; # to test it's set to 10 sec
# later it will be set to 60
# to wait 1 min to send next 1 min bar.
@a = @ARGV;
$f = (@a) ? $_[0] : "SP1Min.txt";
open (MF, "< $f") or die "can't open $f: $!";
$MIN = <MF>; #read 1. line
chomp($MIN);
$SEP = ''; # to set Global Seperator
&startServer();
##### subs #########
sub logmsg {
print "$0 $$: > @_ < at ", scalar localtime, "\n" if $v;
}
sub startServer {
#use strict;
BEGIN { $ENV{PATH} = '/usr/ucb:/bin' }
use Socket;
use Carp;
my $EOL = "\015\012";
my $port = shift || 2345;
my $proto = getprotobyname('tcp');
($port) = $port =~ /^(\d+)$/ or die "invalid port";
socket(Server, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR,
pack("l", 1)) || die "setsockopt: $!";
bind(Server, sockaddr_in($port,
INADDR_ANY)) || die "bind: $!";
listen(Server,SOMAXCONN) || die "listen: $!";
logmsg "server started on port $port";
my $paddr;
$SIG{CHLD} = \&REAPER;
for ( ; $paddr = accept(Client,Server); close Client) {
my($port,$iaddr) = sockaddr_in($paddr);
my $name = gethostbyaddr($iaddr,AF_INET);
logmsg "connection from $name [",
inet_ntoa($iaddr), "] at port:$port, proto:$proto, iAdr:$iaddr";
print Client "Hello", $name, $EOL; # connected to s.o.
#now send every $WaitSec one line of the file.
print Client $MIN, $EOL;
$t = time;
while ( defined($MIN = <MF>)) {
chomp($MIN);
sleep ((time + $WaitSec) - $t);
$t=time;
print Client $MIN, $EOL;
logmsg($MIN);
}
print Client "End of Session", $EOL;
logmsg("File Ende -- Session-Ende, Good Night");
close(MF);
exit;
} #end for
} #end sub
This server sends every 10 sec one line which is printed as well.
Here is now the Client from the same site, that shoul receive the bars and print them out for now.
use strict;
use Socket;
my $v = 1;
my ($remote,$port, $iaddr, $paddr, $proto, $line);
$remote = shift || 'localhost';
$port = shift || 2345; # random port
if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') }
die "No port" unless $port;
$iaddr = inet_aton($remote) || die "no host: $remote";
$paddr = sockaddr_in($port, $iaddr);
$proto = getprotobyname('tcp');
socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
connect(SOCK, $paddr) || die "connect: $!";
logmsg("3Remote:$remote, Port:$port, Proto:$proto");
# This message is print, so I do get here!
# At the same tiome the server starts sending bars
while (1) {
$line = <SOCK>;
print "\nGot1: ", $line;
# until the server is killed nothing appears.
# Then it prints all the time in a new line: Got1: NIX ..
if ( !defined($line) ) {
#if ( !defined($line = <SOCK>) ) {
print "NIX, ";
sleep 1;
next;
} else {
if ($line !~ /End\sof\sSession/) {
print "\nGot2: ", $line;
sleep 5;
next;
} else {
close (SOCK) || die "close: $!";
exit;
}
}
}
I thank all who take time to read it at least for their effort,
Carl
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.