Hi, I want to write a socket server in perl. I need that my server keeps listening and every time a client get connected, a new thread or process be started for handling the connection that is: receive a request, execute another perl script, send back the results from this script to the client and close the connection.

I'm n00b and want to start learning perl by doing this project. Until now I have just the beginning of it:

#!/usr/bin/perl use IO::Socket; use strict; # 'Listen' parameter: this is the maximum number of connections that c +an be queued by the socket waiting for you to accept and process them # 'Reuse' option tells the system to allow reuse of the port after th +e program exits. This is to ensure that if our program exits abnormal +ly and does not properly close the socket, running it again will allo +w opening a new socket on the same port. my $sock = new IO::Socket::INET ( LocalHost => '127.0.0.1', LocalPort => '6590', Proto => 'tcp', Listen => 1, Reuse => 1, ); die "Could not create socket: $!\n" unless $sock; while(1){ # To wait for a connection, we use th +e accept() method which will return a new socket through which we can + communicate with the calling program. my $new_sock = $sock->accept(); print $sock->connected(); while(<$new_sock>) { print $_; #close($sock); } }

I think the best option is use fork for starting new process by each connection because what I need is performance and I don't need to share info between the established connections. I'll like your advice about how to start a new thread or process per connection, I've been reading somethings about select or threads and so on but it isn't clear for me.

Thanks monks...


In reply to socket server threads or fork by lenieto3

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.