Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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:
Here is now the Client from the same site, that shoul receive the bars and print them out for now.
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:
This server sends every 10 sec one line which is printed as well.$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
Here is now the Client from the same site, that shoul receive the bars and print them out for now.
I thank all who take time to read it at least for their effort, Carluse 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; } } }
Back to
Seekers of Perl Wisdom