LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 03-06-2008, 04:12 AM   #1
Fredde87
Member
 
Registered: Aug 2005
Posts: 158

Rep: Reputation: 30
Perl socket multiple connections


Hi,

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?


Many thanks in advance!
 
Old 03-11-2008, 07:03 AM   #2
Fredde87
Member
 
Registered: Aug 2005
Posts: 158

Original Poster
Rep: Reputation: 30
Ok,

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.



Thanks!

Last edited by Fredde87; 03-11-2008 at 08:33 AM.
 
Old 03-11-2008, 05:10 PM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
why don't you just fork for each new client, much easier
 
Old 03-12-2008, 04:38 AM   #4
Fredde87
Member
 
Registered: Aug 2005
Posts: 158

Original Poster
Rep: Reputation: 30
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)?
 
Old 03-12-2008, 06:02 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 10-12-2008, 11:42 AM   #6
Reion
LQ Newbie
 
Registered: Oct 2008
Posts: 3

Rep: Reputation: 0
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.

Last edited by Reion; 10-12-2008 at 02:11 PM.
 
Old 10-12-2008, 07:15 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
See my comments here: http://www.linuxquestions.org/questi...s-fork-675890/
 
Old 10-18-2010, 08:02 AM   #8
pateet
LQ Newbie
 
Registered: Oct 2010
Posts: 1

Rep: Reputation: 0
Hi there,

just a question on 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?

I want to use the code on a server....
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
rsh connections : rcmd: socket: Permission denied tiuz Linux - Networking 8 08-21-2009 08:21 AM
help with socket connections RedOctober45 Linux - Software 1 10-26-2007 12:03 AM
a question about multiple connections in socket programming moloza Programming 7 05-22-2006 10:31 AM
Multiple connections on one socket? (Java) smoothdogg00 Programming 2 04-09-2006 09:15 PM
Single Socket Vs Multiple Socket Kumar Programming 1 10-05-2005 10:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:35 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration