CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: simple chat server: Edit Log



z99
New User

Nov 21, 2012, 3:07 AM


Views: 755
simple chat server

hey everyone,my first post here.
i'm new to Perl , i am trying to write a very simple chat server,which will take each client's message and send it back to all other clients,but i'm unable to send the message to other clients,anyway this is my code.any help would be appreciated


Code
use strict; 
use IO::Socket;
use POSIX 'WNOHANG';
my $c=0;
my @arr;
$SIG{CHLD}=\&h;
sub h {
while(waitpid(-1,WNOHANG)>0) {}
};
$SIG{'INT'}=sub
{
print "server terminated\n";
exit 0;
};

my $svr=IO::Socket::INET->new(LocalPort=>'12345',Listen=>20,Proto=>'tcp',Reuse=>1) or die 'Cant create Socket';
my $clnt;
while(1) {
next unless $clnt=$svr->accept;
$c++;
#push(@arr,$clnt);
my $child=fork();
my $ip=$clnt->peerhost();
my $port=$clnt->peerport();
print "Connection Aceepted $ip:$port\n";
if($child==0) {
$svr->close;
wc($clnt);
exit 0;
print "$ip:$port Left \n";
}
$clnt->close;
}

sub wc {
my $clnt=shift;
my $msg="$c users(s) online\n";
print $clnt $msg;
rcv($clnt);
}

sub rcv {
my $ip=$clnt->peerhost();
my $port=$clnt->peerport();
my $c=shift;
while(my $msgin=<$c>) {
print "$ip:$port => $msgin \n";}
}



(This post was edited by z99 on Nov 21, 2012, 3:08 AM)


Edit Log:
Post edited by z99 (New User) on Nov 21, 2012, 3:08 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives