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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-22-2012, 01:21 PM
|
#1
|
|
Member
Registered: Aug 2009
Location: Iran-Sari
Distribution: CentOS,Fedora,Slitaz
Posts: 130
Rep:
|
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.
|
|
|
|
11-23-2012, 05:17 AM
|
#2
|
|
Member
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,OpenBSD
Posts: 500
Rep: 
|
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.
|
11-23-2012, 06:01 AM
|
#3
|
|
Member
Registered: Aug 2009
Location: Iran-Sari
Distribution: CentOS,Fedora,Slitaz
Posts: 130
Original Poster
Rep:
|
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
|
|
|
|
11-23-2012, 06:19 AM
|
#4
|
|
Senior Member
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 2,806
|
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.
|
11-23-2012, 06:30 AM
|
#5
|
|
Member
Registered: Aug 2009
Location: Iran-Sari
Distribution: CentOS,Fedora,Slitaz
Posts: 130
Original Poster
Rep:
|
thank you
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:02 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|