How to write console mode chat client

How to write console mode chat client

am 11.09.2011 09:08:17 von Siegfried Heintze

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". Thanks=0ASiegfried #!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 < 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 @args =3D @_;=0A print('Thread started: ', jo=
in(' ', @args), "\n");=0A my $line;=0A while (<$sock>) { chomp;=0A=
=09print "$_\n"; last 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(){=0A print $sock $_;=0A}=0A$thr->joi=
n();=0Aclose $sock;

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

Re: How to write console mode chat client

am 11.09.2011 09:18:00 von Shlomi Fish

Hi Siegfried,

On Sun, 11 Sep 2011 00:08:17 -0700
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".
>=20

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
>=20
> #!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 < > # 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 @args =3D @_;
> 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 =3D threads->create('start_thread', 'argument');
> while(){
> print $sock $_;

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

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

Regards,

Shlomi Fish


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

To err is human; to apologise â€=94 divine.

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

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