I seek the knowledge of the enlightened monks to help me reach the last leg of a forking server.

I have tried to read documentation available online, in cookbooks but I am not sure if it is the same as what I want.

The aim is to:

    1. Create a telnet server running on port 7070.
    2. Enable 5 clients at MAX to connect to it.
    3. When 1 client is connected and running a command, all commands from other clients should be queued.(There are only 5 possible commands that a client can invoke, 2 of which are "printhelp" and "quit")

I have managed to reach level 1 and partially level 2. Multiple clients are able to connect to the server.

The problems that I face are these

    A) When the first client dies, the server commits suicide. Advise is sought on this.
    B) How can I prevent more than 5 clients from connecting.
    C) How can I enable queuing of commands and ensuring the result of the commands are sent to the right client.
#!/usr/bin/perl -w use IO::Socket; use strict; $SIG{CHLD} = sub{ wait; }; my $inputLine; my $new_sock; my $main_sock; my $pid; # Create Socket on port 7070 $main_sock = new IO::Socket::INET ( LocalHost => 'localhost', LocalPort => '7070', Proto => 'tcp', Listen => 1, Reuse => 1, ); die "Could not create socket: $!\n" unless $main_sock; print "Telnet Server listening on 7070....\n"; # Accept connection and fork child! while( $new_sock = $main_sock->accept() ){ $pid = fork(); unless( defined($pid) ){ die "Cannot fork\n"; } ################## # CHILD PROCESS ################## if($pid == 0) { # Print Welcome message! welcomeClient($new_sock); while( defined( $inputLine=<$new_sock>) ) { $inputLine =~ s/[\r\n]//g; if( $inputLine eq "printhelp" ){ callPrintHelp($new_sock); }elsif( $inputLine eq "quit" ){ callQuit($new_sock); exit(0); }else{ print $new_sock "INFO> Not implemented yet +\n"; } }#while exit(0); } ################## # CHILD PROCESS ################## }# while main_sock close($main_sock); sub welcomeClient { # Show starting point for client and help messages my ($client_sock) = @_; print $client_sock "######################################\n"; print $client_sock " Telnet Interface \n"; print $client_sock "######################################\n\n"; print $client_sock "INFO> Type 'printhelp' for help\n\n"; print $client_sock "READY:\n"; print $client_sock ">"; } sub callPrintHelp{ my ($client_sock) = @_; ...... ..... } sub callQuit{ my ($client_sock) = @_; ...... ..... }

In reply to A suicidal parent OR death of a forking server by MonkeyMonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.