Advertisement

03.11.2008 at 06:39AM PDT, ID: 23231624
[x]
Attachment Details

Printing to all clients on a socket from a child process

Asked by Fredde87 in Perl Programming Language

Tags: Perl, Linux

Hi Guys,

I am trying to write a simple socket server which will accept multiple connections. I have this code code below which opens a socket and allows multiple connections. At the moment it only posts back to all the connected clients what someone has sent. But I am going to add switch statements here to perform various tasks depending on what the client sends.

This all works great but the problem I have is creating some sort of a child process running on the side which  is an infinite loop. The infinite loops role is to send out a keep alive signal on the socket to all the clients every X seconds.

How do I do this? I am new to perl but I understand that I someone need to share $read_set to the child process? I tried IPC::Shareable but that failed so I am guessing I need to do something with pipe maybe? Can you please advice how I best accomplish this.


Thanks!Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
#!/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);
                        }
                }
        }
}
 
Loading Advertisement...
 
[+][-]03.11.2008 at 09:37AM PDT, ID: 21097412

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.11.2008 at 09:52AM PDT, ID: 21097578

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.11.2008 at 03:15PM PDT, ID: 21100957

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.12.2008 at 02:42AM PDT, ID: 21104193

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.14.2008 at 11:14AM PDT, ID: 21127937

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.19.2008 at 01:01AM PDT, ID: 21159271

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.26.2008 at 10:11AM PDT, ID: 21213932

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.02.2008 at 01:15AM PDT, ID: 21260949

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.02.2008 at 07:07AM PDT, ID: 21263149

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Perl Programming Language
Tags: Perl, Linux
Sign Up Now!
Solution Provided By: Adam314
Participating Experts: 1
Solution Grade: A
 
 
[+][-]04.14.2008 at 03:25AM PDT, ID: 21348837

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.14.2008 at 07:21AM PDT, ID: 21350229

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.15.2008 at 04:49AM PDT, ID: 21357717

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.15.2008 at 04:51AM PDT, ID: 21357729

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628