Forking Server

Discussion in 'Perl Misc' started by SRam, Sep 15, 2003.

  1. SRam

    SRam Guest

    I a writing Forking Server for a Small application..

    This is code for multiplexing

    my $listen = IO::Socket::INET->new(Proto => 'tcp',LocalPort => 2323,
    Listen => 1, Reuse => 1)
    or die $!;

    $listen->autoflush(1);

    my $readable_handles = new IO::Select();
    $readable_handles->add($listen);
    while (1)
    {
    $listen->autoflush(1);
    ($new_readable) = IO::Select->select($readable_handles, undef,
    undef, 0);

    foreach $sock (@$new_readable)
    {

    if ($sock == $listen)
    {
    my $new_sock = $sock->accept();
    $readable_handles->add($new_sock);
    $count++;
    $flag = 0;
    print $new_sock->fileno . ": connected\n";
    }
    else
    {
    $sock->recv($line,300);
    print "Received Message\n";
    print "Message = $line\n";
    ......operations....
    }
    }
    }

    I also got code for Forking Server from advanced perl programming

    # Forking server
    use IO::Socket;
    $SIG{CHLD} = sub {wait ()};
    $main_sock = new IO::Socket::INET (LocalHost => 'goldengate',
    LocalPort => 1200,
    Listen => 5,
    Proto => 'tcp',
    Reuse => 1,
    );
    die "Socket could not be created. Reason: $!\n" unless ($sock);
    while ($new_sock = $main_sock->accept()) {
    $pid = fork();
    die "Cannot fork: $!" unless defined($pid);
    if ($pid == 0) {
    # Child process
    while (defined ($buf = <$new_sock>)) {
    # do something with $buf ....
    print $new_sock "You said: $buf\n";
    }
    exit(0); # Child process exits when it is done.
    } # else 'tis the parent process, which goes back to accept()
    }
    close ($main_sock);

    I need to convert the multipexing one of first to forking..I don't
    know exactly where to modify...Can Anyone help me get me the exact
    code...
     
    SRam, Sep 15, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SRam

    Forking server

    SRam, Aug 21, 2003, in forum: Perl
    Replies:
    1
    Views:
    3,027
  2. SRam

    Forking Server

    SRam, Sep 15, 2003, in forum: Perl
    Replies:
    0
    Views:
    563
  3. Ed
    Replies:
    0
    Views:
    710
  4. Replies:
    3
    Views:
    625
    Thomas Guettler
    Jun 12, 2006
  5. Scottman

    Daemon Server, Forking, Defunct Processes

    Scottman, Sep 11, 2008, in forum: C Programming
    Replies:
    2
    Views:
    991
    CBFalconer
    Sep 11, 2008
Loading...

Share This Page