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 am writing a program in perl (sorry new to perl) using a socket to listen for commands etc. The perl script just acts as a interface which calls my appropriate shell scripts. I use one of this standard of the internet examples which looks similuar to the one below,
Code:
my $sock = new IO::Socket::INET (
LocalHost => '192.168.0.1',
LocalPort => '8888',
Proto => 'tcp',
Listen => 1,
Reuse => 1,
);
die "Could not create socket: $!\n" unless $sock;
my $new_sock = $sock->accept();
while(<$new_sock>) {
$m=$_;
$m =~ s/\n|\r//g;
last if ($m eq ".PART.");
print "$m";
print $new_sock "Hello! You said $m;
}
close($new_sock);
I need to be able to accept multiple connections to the same socket. I want to have up to 10 users connect and they should all receive the same data being sent to them. Basically my program wont care who is connected, it is going to broadcast the same data to everyone on a regular interval. How can I easily accomplish this?
I have practically dumped my current script. I have now one which allows multiple connections which looks like this.
Code:
#!/usr/bin/perl -w
#
# Socket for communication
# Libraries
use IO::Socket;
use IO::Select;
use IO::Handle;
my @sockets;
my $s = new IO::Socket::INET (
LocalHost => '192.168.0.1',
LocalPort => '1234',
Proto => 'tcp',
Listen => 16,
Reuse => 1,
);
die "Could not create socket: $!\n" unless $s;
$read_set = new IO::Select(); # create handle set for reading
$read_set->add($s); # add the main socket to the set
while (1) {
my ($rh_set) = IO::Select->select($read_set, undef, undef, 0);
foreach $rh (@$rh_set) {
if ($rh == $s) {
# Client has connected
$ns = $rh->accept();
$read_set->add($ns);
} else {
$buf = <$rh>;
if($buf) {
# Client has sent something
$buf =~ s/\n|\r//g;
my @sockets = $read_set->can_write();
foreach my $sck(@sockets){print $sck "Hi everybody, I just received the following for one of you: $buf\r\n";}
}
else {
# Client has disconnected
$read_set->remove($rh);
close($rh);
}
}
}
}
But I also used to have a child process which sent a keep alive every second but this does not work anymore now because my sockets are stored in a handle. Am I correct in thinking that I need to use a pipe in order to print to all the connected clients? I am unable to get it to work though, could someone advice me of how I do this?
To summerize I guess I want the code above but with a child process which prints to all the connected clients based on a timer.
Sorry I am new to perl, how do I fork each new client? If I fork them, wont I have the same problem I got now that I cant write to them all at once repeatedly (every 5 seconds or so)?
Every time your (main) server prog receives a client cxn, it forks a copy of itself to deal with that client.
Since this is a copy of the orig server, it'll have all your code needed to send a msg whenever.
I'm new to the whole perl thing, but I've been looking around for ways to do what you said and have tried many ways to get the whole "fork a new child for every connection" working but can't seem to, is there any chance you could give me a run down and some code on how to do it, ive got the whole socket bit working just not this.
Thanks
Sorry for posting in this thread, just i had been looking for a while and stumbled across this.
If you fork a new child for each tcp connection - and execute a custom function like (client_process()) for each client - should this function be a thread or does the fork already handle the function as a thread because it is part of a unique fork?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.