LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-24-2007, 05:24 AM   #1
son_t
Member
 
Registered: Sep 2006
Posts: 49

Rep: Reputation: 15
Unhappy Stop perl socket server broken pipe or exiting


Hello

I want to run a simple socket server written in perl, as such:

Code:
#!/usr/bin/perl
use POSIX;
use IO::Socket;

$PORT = 6001;
$server = IO::Socket::INET->new( Proto     => 'tcp',
                                 LocalPort => $PORT,
                                 Listen    => SOMAXCONN,
                                 Reuse     => 1);
die "$0: can't setup server" unless $server;

while ($client = $server->accept())
{
   while($client)
   {
        print $client `date`;
        sleep 30;
   }
}
This continously loops and gives me the date when I telnet to he port 6001. The problem is, if the client disconnects it causes a broken pipe and the program exits. If I have:

Code:
   while(1)
   {
        print $client `date`;
        sleep 30;
   }
When the client dies, the program just exits. I need this socket server to remain alive for subsequent or more client connections. Could anyone suggest a small modification to this program to achieve this?

Any hints would be appreciated.
TIA
 
Old 10-25-2007, 03:52 AM   #2
son_t
Member
 
Registered: Sep 2006
Posts: 49

Original Poster
Rep: Reputation: 15
Thanks for looking... I solved it with a fork!

Code:
#!/usr/bin/perl
use POSIX;
use IO::Socket;

$PORT = 6001;
$server = IO::Socket::INET->new( Proto     => 'tcp',
                                 LocalPort => $PORT,
                                 Listen    => SOMAXCONN,
                                 Reuse     => 1);
die "$0: can't setup server" unless $server;

while ($client = $server->accept())
{
    unless ($pid = fork)
    {
         status($client);
         exit;
    }
}

sub status
{
   while(1)
   {
        print $client `date`;
        sleep 30;
   }
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
broken pipe pansarevaibhav Linux - Newbie 1 09-12-2006 04:11 AM
Sox not exiting when used in a pipe rignes Slackware 3 08-06-2006 06:43 PM
Broken pipe henkelarsson12 *BSD 1 04-08-2006 11:12 AM
Broken pipe? Nubian Linux - Newbie 2 08-16-2005 08:56 AM
Broken Pipe Cynthia Blue Linux - Newbie 0 12-30-2002 02:31 PM


All times are GMT -5. The time now is 02:34 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration