Hi Siegfried,

On Sun, 11 Sep 2011 00:08:17 -0700
<siegfr...@heintze.com> wrote:

> This program works, except it does not echo its input from the socket
> immediately. Can someone modify the client for me so it will display
> what it reads from the socket immediately? Presently, I only get to see
> what it reads from the socket if it sees "exit".
> 

My guess is that you need to add:

        STDOUT->autoflush(1);

To the code close to the beginning. See:

http://perl.plover.com/FAQs/Buffering.html

A few comments on your code below.

> Thanks
> Siegfried
> 
> #!c:/perl/bin/perl
> #!/usr/bin/perl
> #!/usr/local/bin/perl5.8.9
> #

Only the first sha-bang line has any effect, so there's no need to specify
three.

> # $Log$
> #
> # Begin commands to execute this file using Perl with bash
> # ./client.pl <<EOF
> # hello
> # there
> # EOF
> # echo "all done"
> # End commands to execute this file using Perl with bash
> #
> use strict;
> use warnings;

It's good that you're using strict and warnings.

> use POSIX;
> use IO::Socket;
> use threads ('yield',
> 'stack_size' => 64*4096,
> 'exit' => 'threads_only',
> 'stringify');

For threads in Perl see:

http://www.perlmonks.org/?node_id=288022

Generally, they are not recommended.

> my $port = 8795;
> my $rhost = 'localhost';
> my $sock = new IO::Socket::INET(PeerAddr => $rhost, PeerPort => $port,
> Proto => 'tcp', Timeout => 5);

You shouldn't use indirect-object notation:

http://www.modernperlbooks.com/mt/2009/08/the-problems-with-indirect-object-notation.html

> sub start_thread {
>   my @args = @_;
>   print('Thread started: ', join(' ', @args), "\n");
>   my $line;
>   while (<$sock>) {

I'm not sure if the global $sock is accessible to the thread. iThreads are
weird.

>       chomp;
>       print "$_\n";
>       last if /exit/;
>   }
> }
> print "Hello ".(strftime "%a %b %d %H:%M:%S %Y\n", localtime);
> my $thr = threads->create('start_thread', 'argument');
> while(<STDIN>){
>   print $sock $_;

That should preferably be «print {$sock} $_;», otherwise it is easily confused
with «print $sock, $_;»

> }
> $thr->join();
> close $sock;
> 
> 
> 
> 
> 
> 
> 

Regards,

        Shlomi Fish


-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Optimising Code for Speed - http://shlom.in/optimise

To err is human; to apologise — divine.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to