chat server example

------_=_NextPart_001_01CB3928.5FE75584
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

Hi All,



In this commonly used example I don't get something...



Why does it always echo the local ip address, even when people connected
from other systems are talking?



So if ip [1.2.3.4] talks it still echo's [5.6.7.8] instaid of [1.2.3.4]
I don't get that?

In this example you can never see who's talking cause it always uses the
IP of the system that you are on...





#!/usr/bin/perl



use IO::Socket;

use IO::Select;



$max_clients =3D 10;

$port =3D 56789;



$new_client =3D IO::Socket::INET->new(Proto=3D>"tcp", =
LocalPort=3D>$port,
Listen=3D>$max_clients, Reuse=3D>1);



# create a new selection and add our basic socket for incoming
connections

$sel =3D IO::Select->new($new_client);



while ( [at] ready =3D $sel->can_read) {

# for every readable socket

foreach $client ( [at] ready) {

# check if it is the basic socket

if ($client =3D=3D $new_client) {

# if it is establish new connection

$add =3D $client->accept;

# print status to new user

$count =3D $sel->count;

print $add "--- $count users(s) online ---\n";

# add new socket to the selection

$sel->add($add);

# if it is a already established connection

} else {

# get the message

$message =3D <$client>;

# remove last newline characters

chop($message);

chop($message);

if ($message =3D~ /^\./) {

if ($message eq ".quit") {

# remove him from selection

$sel->remove($client);

# close his socket

close $client;

} elsif ($message eq ".version") {

print $client "*** version 1.0 Beta ***\n";

} else {

print $client "*** commands are: .version .quit ***)\n";

}

} else {

# send the message to all users

foreach ($sel->can_write) {

print $_ "[".$_->peerhost."] ".$message."\n"

}

}

}

}

}



-

Marco van Kammen
Springer Science+Business Media
System Manager & Postmaster

-

van Godewijckstraat 30 | 3311 GX
Office Number: 05E21
Dordrecht | The Netherlands

-

tel

+31(78)6576446

fax

+31(78)6576302

-

www.springeronline.com <http://www.springeronline.com>
www.springer.com <http://www.springer.com/>

-






------_=_NextPart_001_01CB3928.5FE75584--
Marco.vanKammen [ Mi, 11 August 2010 09:39 ] [ ID #2045887 ]
Perl » gmane.comp.lang.perl.beginners » chat server example

Vorheriges Thema: perl DBI problem while installing bugzilla
Nächstes Thema: how to find path up to installled modules @ sun solaris box