Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default Forking Server

    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 Guest

  2. Similar Questions and Discussions

    1. Net::Telnet and forking multiple processes
      Under normal conditions the entire program works, however when trying to fork multiple processes, the script cannot open multiple telnet sessions...
    2. Forking for performance?
      Ok, first off I should preface this by saying I've never forked before. Guess you could call me a virgin :) Anyways, I'm used to writing scripts...
    3. forking and ending a CGI process
      Hi; I spent about 4 hours searching through newsgroups, faqs, and O'reilly books with various odd-smelling desert animals on the cover. I found...
    4. Pre-forking SOAP servers
      Hi all, Rob Riepel, in http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=mvyitg0vhui.fsf%40Zathras.Stanford.EDU&rnum=2 indicates...
    5. Perl Forking Server Problems
      littleretsam@yahoo.com (Jeff Chinnock) wrote in message news:<49fcf7a7.0306251728.1a1a4160@posting.google.com>... This looks like an infinite...
  3. #2

    Default Re: Forking Server

    This looks allot like a homework assignment.


    "SRam" <aksivaram2k2@yahoo.com> wrote in message
    news:20ffa179.0309142351.1dee07c8@posting.google.c om...
    > 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...

    Robert Fonda Guest

  4. #3

    Default Re: Forking Server



    SRam wrote:
    > I a writing Forking Server for a Small application..
    Nobody else has replied constructively, so I will attempt to. I have not
    used the IO::Socket module, but have done some server programming in C
    and am familiar with some of the issues.
    >
    > This is code for multiplexing
    >
    > my $listen = IO::Socket::INET->new(Proto => 'tcp',LocalPort => 2323,
    > Listen => 1, Reuse => 1)
    > or die $!;
    You probably want to increase Listen to more than 1. The default is
    usually 5. If you leave it at only 1, you are liable to miss a client
    when two or more try to connect at the same time. This number is the
    number of waiting clients that will be queued up when trying to connect
    to your socket.
    >
    > $listen->autoflush(1);
    >
    > my $readable_handles = new IO::Select();
    > $readable_handles->add($listen);
    > while (1)
    > {
    > $listen->autoflush(1);
    This is probably redundant to the autoflush call above. Once set, this
    switch shouldn't change.
    > ($new_readable) = IO::Select->select($readable_handles, undef,
    > undef, 0);
    >
    > foreach $sock (@$new_readable)
    > {
    >
    > if ($sock == $listen)
    > {
    > my $new_sock = $sock->accept();
    You now have a handle to a socket object for a newly-connected client.
    If you want to fork off a process to serve this client, this is the
    place to do it. In that case, you probably don't want to add this client
    to the set of sockets being waited on by the select statement, and your
    select statement will never be looking at more than one socket: the
    original server socket.
    > $readable_handles->add($new_sock);
    > $count++;
    > $flag = 0;
    > print $new_sock->fileno . ": connected\n";
    > }
    > else
    > {
    You now have a handle to an IO::Socket::INET object that has data ready
    for reading. If you want to fork off a process to process the data, this
    is the place to do it.
    > $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...
    You haven't explained why you want to do this. In general, a server
    would be implemented either by multiplexing or multiprocessing
    (forking). Multiplexing using select lets you get data from a client
    without blocking because you are told when the data is ready. The
    advantage is simplicity (once you figure out how the select statement
    works), efficiency (because you only have one process), and
    responsiveness (because you don't have to wait for a process to fork).
    The disadvantage is unresponsiveness if it takes a long time to service
    a client, and other clients are waiting. Advanced Perl Programming, by
    Srinivasan, discusses both of these approaches.

    So why do you want to do both multiplexing and multiprocessing?

    Jim Gibson Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139