ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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";}
}
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.
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
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)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.