Simple messaging program.

--0016364eec06efd5cd049e8ea937
Content-Type: text/plain; charset=ISO-8859-1

Hello!

I'm trying to learn about network programming and sockets in perl. I'm
fairly new to the language, I just read Randal's book.

What I'm trying to do is a simple program that reads every line of <STDIN>
from multiple clients and prints them on real time to <STDOUT> on a server.

After a lot of googling, struggling with threads and fork, I ended up on the
perl-doc page for IO::Select, which has a sample code that does almost
exactly what I want. I modified it a bit and I have the following on the
server side:

$lsn = new IO::Socket::INET(Listen => 1, LocalPort => 9000);
$sel = new IO::Select ( $lsn );

while ( [at] ready = $sel->can_read) {
foreach $fh ( [at] ready) {
if ($fh == $lsn) {
$new = $lsn->accept;
$sel->add($new);
} else {
while (<$fh>) {
print;
}
$sel->remove($fh);
$fh->close;
}
}
}

This works perfectly for the first client, all the lines are received as
soon as you type them. But for a second client the lines are only displayed
when the first client quits. It probably has to do with it getting stuck on
that while <$fh> loop, but I can't see how to fix it. Seems like a very
simple problem but I can't get myself around it. How to I make two or more
clients have their lines printed simultaneously? Can someone point me in the
right direction?

Much appreciated,
~ Daniel.

--0016364eec06efd5cd049e8ea937--
Daniel Calvo [ Mi, 16 März 2011 01:31 ] [ ID #2056648 ]

Re: Simple messaging program.

ä=BA=8E 2011-3-16 8:31, Daniel Calvo =E5=86=99=E9=81=93:
> while (<$fh>) {
> print;
> }


For the first look, you shouldn't be using <> for receiving data from
the socket. For more details, please loot at this article and its comment=
s:

http://www.perlfect.com/articles/select.shtml

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Terry [ Mi, 16 März 2011 02:29 ] [ ID #2056649 ]

Re: Simple messaging program.

>>>>> "t" =3D=3D terry <terry5 [at] arcor.de> writes:

t> =98=B0 2011-3-16 8:31, Daniel Calvo =8E=CA=93=B9:
>> while (<$fh>) {
>> print;
>> }


t> For the first look, you shouldn't be using <> for receiving data from
t> the socket. For more details, please loot at this article and its
t> comments:

t> http://www.perlfect.com/articles/select.shtml

that barely expands on the code the OP posted. and one tiny comment on
dealing with unbuffered reads. and that comment's code is missing all
the logic to deal with socket closing detection and more.

the best source for this is the book network programming in perl. it is
slightly old but still valuable in its coverage of buffering issues.

another solution for the OP, have your clients close their sockets or
use the shutdown call to close the write portion. if you are just
slurping in all of stdin, then the client needs to do no more than write
all of its data to the socket and exit or do one of the previous things
first.

also perldoc perlipc is a useful tutorial.

uri

--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com =
--
----- Perl Code Review , Architecture, Development, Training, Support ----=
--
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com -------=
--

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Mi, 16 März 2011 02:38 ] [ ID #2056650 ]

Simple messaging program.

--0016367d67a400ef28049e90f560
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Hello gentlemen. Thanks for the replies.

Terry, before mailing the list I already had stumbled upon that page. I
thought it was less clearer than the perl-doc page, I couldn't grasp it at
first like with perl-doc.

Uri, thanks a lot for the pointers. Yes, what my clients are doing is
dumping all of stdin to a given socket. My problem was handling multiple
clients on the server side. I will check that book though, seems promissing=
..
And perlipc too. Thanks again.

It seems I had this problem due to my poor understanding of when to use
threads. I managed to put together the following code, which I don't know i=
f
its any good, but its working.

use IO::Select;
use IO::Socket;
use threads;


$lsn =3D new IO::Socket::INET(Listen =3D> 1, LocalPort =3D> 9000);
$sel =3D new IO::Select ( $lsn );

while ( [at] ready =3D $sel->can_read) {
foreach $fh ( [at] ready) {
if ($fh =3D=3D $lsn) {
$new =3D $lsn->accept;
$sel->add($new);
} else {
my $t =3D threads -> new (\&subprint, $fh);
push ( [at] threads, $t);
}
}
}

foreach ( [at] threads){
$_->join;
}

sub subprint {
my $fhh =3D shift;
while (<$fhh>){
print;
}
$sel->remove($fhh);
$fhh->close;

}




2011/3/15 Uri Guttman <uri [at] stemsystems.com>

> >>>>> "t" =3D=3D terry <terry5 [at] arcor.de> writes:
>
> t> ä=BA=8E 2011-3-16 8:31, Daniel Calvo =E5=86=99=E9=81=93:
> >> while (<$fh>) {
> >> print;
> >> }
>
>
> t> For the first look, you shouldn't be using <> for receiving data from
> t> the socket. For more details, please loot at this article and its
> t> comments:
>
> t> http://www.perlfect.com/articles/select.shtml
>
> that barely expands on the code the OP posted. and one tiny comment on
> dealing with unbuffered reads. and that comment's code is missing all
> the logic to deal with socket closing detection and more.
>
> the best source for this is the book network programming in perl. it is
> slightly old but still valuable in its coverage of buffering issues.
>
> another solution for the OP, have your clients close their sockets or
> use the shutdown call to close the write portion. if you are just
> slurping in all of stdin, then the client needs to do no more than write
> all of its data to the socket and exit or do one of the previous things
> first.
>
> also perldoc perlipc is a useful tutorial.
>
> uri
>
> --
> Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.co=
m--
> ----- Perl Code Review , Architecture, Development, Training, Support
> ------
> --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com------=
---
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
> For additional commands, e-mail: beginners-help [at] perl.org
> http://learn.perl.org/
>
>
>

--0016367d67a400ef28049e90f560--
Daniel Calvo [ Mi, 16 März 2011 04:15 ] [ ID #2056651 ]
Perl » gmane.comp.lang.perl.beginners » Simple messaging program.

Vorheriges Thema: how to handle "up movement key" in perl
Nächstes Thema: Detecting if input is provided