How to write console mode chat client
This program works, except it does not echo its input from the socket=0Aimm=
ediately. Can someone modify the client for me so it will display=0Awhat it=
reads from the socket immediately? Presently, I only get to see=0Awhat it =
reads from the socket if it sees "exit".=0A=0AThanks=0ASiegfried=0A=0A#!c:/=
perl/bin/perl=0A#!/usr/bin/perl=0A#!/usr/local/bin/perl5.8.9 =0A#=0A# $Log$=
=0A#=0A# Begin commands to execute this file using Perl with bash=0A# ./cli=
ent.pl <<EOF=0A# hello=0A# there=0A# EOF=0A# echo "all done"=0A# End comman=
ds to execute this file using Perl with bash=0A#=0Ause strict;=0Ause warnin=
gs;=0Ause POSIX;=0Ause IO::Socket;=0Ause threads ('yield',=0A'stack_size' =
=3D> 64*4096,=0A'exit' =3D> 'threads_only',=0A'stringify');=0Amy $port =3D =
8795;=0Amy $rhost =3D 'localhost';=0Amy $sock =3D new IO::Socket::INET(Peer=
Addr =3D> $rhost, PeerPort =3D> $port,=0AProto =3D> 'tcp', Timeout =3D> 5);=
=0Asub start_thread {=0A my [at] args =3D [at] _;=0A print('Thread started: ', jo=
in(' ', [at] args), "\n");=0A my $line;=0A while (<$sock>) {=0A=09chomp;=0A=
=09print "$_\n";=0A=09last if /exit/;=0A }=0A}=0Aprint "Hello ".(strftime =
"%a %b %d %H:%M:%S %Y\n", localtime);=0Amy $thr =3D threads->create('start_=
thread', 'argument');=0Awhile(<STDIN>){=0A print $sock $_;=0A}=0A$thr->joi=
n();=0Aclose $sock;=0A=0A=0A=0A=0A=0A
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: How to write console mode chat client
Hi Siegfried,
On Sun, 11 Sep 2011 00:08:17 -0700
<siegfried [at] 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' =3D> 64*4096,
> 'exit' =3D> 'threads_only',
> 'stringify');
For threads in Perl see:
http://www.perlmonks.org/?node_id=3D288022
Generally, they are not recommended.
> my $port =3D 8795;
> my $rhost =3D 'localhost';
> my $sock =3D new IO::Socket::INET(PeerAddr =3D> $rhost, PeerPort =3D> $po=
rt,
> Proto =3D> 'tcp', Timeout =3D> 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 [at] args =3D [at] _;
> print('Thread started: ', join(' ', [at] 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 =3D threads->create('start_thread', 'argument');
> while(<STDIN>){
> print $sock $_;
That should preferably be =C2=ABprint {$sock} $_;=C2=BB, otherwise it is ea=
sily confused
with =C2=ABprint $sock, $_;=C2=BB
> }
> $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 =E2=80=94 divine.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/