LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-22-2012, 01:21 PM   #1
z99
Member
 
Registered: Aug 2009
Location: Iran-Sari
Distribution: CentOS,Fedora,Slitaz
Posts: 131

Rep: Reputation: 20
very simple perl chat server


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";} 
}

Last edited by z99; 11-22-2012 at 01:35 PM.
 
Old 11-23-2012, 05:17 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,OpenBSD
Posts: 666
Blog Entries: 2

Rep: Reputation: 169Reputation: 169
Code:
while(1) { 
next unless $clnt=$svr->accept;
Each time round this loop you're getting a new connection to a new client in a new process. This is ok for providing independent service to your clients but bad for putting them in communication with each other. In your case you probably want to keep a single process and use select() on multiple sockets.
 
1 members found this post helpful.
Old 11-23-2012, 06:01 AM   #3
z99
Member
 
Registered: Aug 2009
Location: Iran-Sari
Distribution: CentOS,Fedora,Slitaz
Posts: 131

Original Poster
Rep: Reputation: 20
thank you,
for instance,if 5 connection accept then we'll have 5 $clnt ?
how can i have a single process without using select() just with fork().
can we have all the client(s) in an array? like!
Quote:
while(1) {
next unless $clnt=$svr->accept;
$c++;
push(@arr,$clnt); <=======
and then pass them to other subs.
is that even right?
thanks in advance
 
Old 11-23-2012, 06:19 AM   #4
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,001

Rep: Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003
you can keep your clients in an array, that would be ok. But I think you will need to use select otherwise it will be really difficult to implement (or probably impossible)
 
1 members found this post helpful.
Old 11-23-2012, 06:30 AM   #5
z99
Member
 
Registered: Aug 2009
Location: Iran-Sari
Distribution: CentOS,Fedora,Slitaz
Posts: 131

Original Poster
Rep: Reputation: 20
thank you
 
Old 11-23-2012, 06:38 AM   #6
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,001

Rep: Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003
this is a sample for you: http://poe.perl.org/?POE_Cookbook/Chat_Server
select is here hidden inside the POE package. You can find several tutorials about it: http://www.nntp.perl.org/group/perl....msg113832.html or ...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows chat client communicates with Linux server chat application friend511 Linux - Newbie 1 05-16-2012 06:56 AM
Configure LVS for chat server(tomcat) and file server reetika786 Linux - Newbie 0 03-14-2012 05:02 AM
Any chat server recommendation for Linux server? yaelyael Linux - Software 8 08-23-2010 09:32 AM
Java Sockets: Help with a simple chat client/server Mega Man X Programming 6 07-15-2009 05:16 PM
Simple distro for a server (a simple guy) dwmartini Linux - Distributions 2 02-03-2006 11:08 AM


All times are GMT -5. The time now is 04:19 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration