LinuxQuestions.org
Review your favorite Linux distribution.
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-05-2008, 05:56 AM   #1
Fredde87
Member
 
Registered: Aug 2005
Posts: 158

Rep: Reputation: 30
Perl socket stopping


Hi Guys,

Was wondering if you can help me. I am relative new to perl scriptting, I have the following script (simplified a bit as it is work related).

Code:
#!/usr/bin/perl -w
#

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;

# Open socket
start:
$alive=1;
my $new_sock = $sock->accept();
print $new_sock "WELCOME 100";

# Command listner
while(<$new_sock>) {
        $m=$_;
        $m =~ s/\n|\r//g;
        if ($m eq ".PART.") {
                print $new_sock "CLOSE 101 Disconnecting. Good bye!";
                $alive=0;
        }
       last if ($m eq ".PART.");
       # Call shell script with appropriate action
       system "/bin/bash -x /root/scripts/functions $m >&1";
}
close($new_sock);
print "Closed socket and will restart it now";
goto start
I use goto start at the end to reopen the socket and listen for a new connmection after a user disconnects.

Code:
# Keep alive thread
my $thrKL = threads->create(\&keepalive);
$thrKL->detach();
sub keepalive{
        while ($alive == 1) {
                print $new_sock "Keep alive!\n";
                sleep(1);
        }
}
However if I add my keepalive thread which looks like the above code, then the script will exit after a .PART. is received and it will not even print the "Closed socket and will restart it now" line. What am I doing wrong?


Thanks!
 
Old 03-05-2008, 07:56 PM   #2
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
How do the 2 things relate?
You've got a standalone prog and a standalone thr sub, but there's no connection between the 2.
Obviously the thr cannot run alone, it's not a complete prog.
Need more info.
 
Old 03-06-2008, 03:14 AM   #3
Fredde87
Member
 
Registered: Aug 2005
Posts: 158

Original Poster
Rep: Reputation: 30
Well I put the thread just above "# Command listner" and it is called and runs as it should. I might have completely misunderstood how threads work but what I am trying to acomplish is to run a function and not wait for it to finish. I want to run a inifinite loop in the background which prints a keep alive string to my open socket.

Is there another approach I should be takeing?

So just to clarify the two above codes are in the same script, the keepalive thread is started after the socket has been created but before my while loop which monitors incoming data.
 
Old 03-06-2008, 04:05 AM   #4
Fredde87
Member
 
Registered: Aug 2005
Posts: 158

Original Poster
Rep: Reputation: 30
I got it working! I ended up using Fork rather then thread to make it run. Thanks!
 
  


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
Perl socket daemon twantrd Programming 10 01-10-2008 04:04 AM
Raw socket with perl Whiteghost Linux - General 1 10-14-2005 10:29 AM
perl - IO::Socket::INET - for (;;) statement?? AM1SHFURN1TURE Programming 3 03-29-2005 10:55 PM
use IO::Socket help (perl) vexer Programming 0 03-06-2003 08:47 PM
stopping a perl script safra Linux - Newbie 5 07-07-2002 12:45 PM

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

All times are GMT -5. The time now is 07:29 PM.

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