Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Socket programming, problem with fork

Status
Not open for further replies.

thekira

Programmer
Jun 10, 2007
2
PL
Hi. I've been trying to write simple server.


Code:
#!/usr/bin/perl

use strict;
use IO::Socket;
use IO::Select;

my $main = new IO::Socket::INET (LocalHost => 'localhost',
   LocalPort => 6677,
   Listen => 5,
   Proto => 'tcp',
   Reuse => 1
);
 
die unless $main;
 
$SIG{'CHLD'} = sub {
  wait;
};

while ($chld = $main->accept()) {
    my $pid = fork();

    die unless defined $pid;

    if ($pid == 0) {
       my $buf;
        print $chld "Hello world!\n";

        my $select = new IO::Select(\*STDIN, $chld);

        my @ready;

        while (@ready = $select->can_read(180)) {
           foreach (@ready) {
              my ($msg, $str);
 
              sysread($_, $msg, 2048);
 
              unless ($msg) {
                print "Quit\n";
                exit 0;
                # ---> here <---
              }
           }

        }
        exit 0;
     }
}

In this code client connects to server, sends some data and disconnects -> child process does 'exit 0'. However, parent gets SIGCHLD and exit too. My problem is that I want parent to listen at port 6677 forever...

What's wrong with this code?

Thanks in advance
 
Not sure what's going.. here is a piece of code I use that I know works (I wrote it 8+ years ago so no beating me up over it :) )
Code:
[gray]#!/usr/bin/perl[/gray]

[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]IO::Socket[/green][red];[/red]
[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[gray][i]#Set autoflush to true[/i][/gray]
[blue]$|[/blue]=[fuchsia]1[/fuchsia][red];[/red]

[gray][i]#needed to stop zombie procs[/i][/gray]
[blue]$SIG[/blue][red]{[/red][purple]CHLD[/purple][red]}[/red] = [red]'[/red][purple]IGNORE[/purple][red]'[/red][red];[/red]


[gray][i]### Socket[/i][/gray]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$sock[/blue] = new [maroon]IO::Socket::INET[/maroon] [red]([/red][purple]LocalHost[/purple] => [red]"[/red][purple]blah[/purple][red]"[/red],
                               [purple]LocalPort[/purple] => [fuchsia]5000[/fuchsia],
                               [purple]Listen[/purple]    => [fuchsia]5[/fuchsia],
                               [purple]Proto[/purple]     => [red]'[/red][purple]tcp[/purple][red]'[/red],
                               [purple]Reuse[/purple]     => [fuchsia]1[/fuchsia],
                              [red])[/red][red];[/red]
[url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Socket could not be created. Reason: [blue]$![/blue][purple][b]\n[/b][/purple][/purple][red]"[/red] [olive][b]unless[/b][/olive] [red]([/red][blue]$sock[/blue][red])[/red][red];[/red]

[gray][i]#Wait for new connections[/i][/gray]
[olive][b]while[/b][/olive] [red]([/red][black][b]my[/b][/black] [blue]$new_sock[/blue] = [blue]$sock[/blue]->[maroon]accept[/maroon][red]([/red][red])[/red][red])[/red] 
[red]{[/red]
	[blue]$new_sock[/blue]->[maroon]autoflush[/maroon][red]([/red][fuchsia]1[/fuchsia][red])[/red][red];[/red]
	
	[gray][i]#Fork off connection so I can listen for more connections[/i][/gray]
  	[black][b]my[/b][/black] [blue]$pid[/blue] = [url=http://perldoc.perl.org/functions/fork.html][black][b]fork[/b][/black][/url][red]([/red][red])[/red][red];[/red]
	[olive][b]if[/b][/olive] [red]([/red][blue]$pid[/blue] == [fuchsia]0[/fuchsia][red])[/red] 
	[red]{[/red]
		[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url][red]([/red][blue]$sock[/blue][red])[/red][red];[/red]
		[black][b]my[/b][/black] [blue]@sockarray[/blue][red];[/red]	
			
		[gray][i]#Read in buffer[/i][/gray]
   		[olive][b]while[/b][/olive] [red]([/red][black][b]my[/b][/black] [blue]$buf[/blue] = <[blue]$new_sock[/blue]>[red])[/red]
    		[red]{[/red]
			[gray][i]#push info into an array[/i][/gray]
			[url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@sockarray[/blue], [blue]$buf[/blue][red];[/red]

			[gray][i]#if we get bye we're done[/i][/gray]
      			[olive][b]if[/b][/olive] [red]([/red][blue]$buf[/blue] =~ [red]/[/red][purple]bye[/purple][red]/[/red][red])[/red]
      			[red]{[/red]
				[black][b]close[/b][/black][red]([/red][blue]$new_sock[/blue][red])[/red][red];[/red]
      			[red]}[/red]
    		[red]}[/red]

		[gray][i]#Get rid of new lines[/i][/gray]
		[url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url] [blue]@sockarray[/blue][red];[/red]
		
		[url=http://perldoc.perl.org/functions/exit.html][black][b]exit[/b][/black][/url][red];[/red]	
  	[red]}[/red]
	[black][b]close[/b][/black][red]([/red][blue]$new_sock[/blue][red])[/red][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]IO::Socket - Object interface to socket communications[/li]
[/ul]
[/tt]
 
Thanks. I changed 'wait' to 'IGNORE' and it works. I thought I can't just ignore the signal... but it works ;)

Thanks once again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top