http://www.perlmonks.org?node_id=1039609

lenieto3 has asked for the wisdom of the Perl Monks concerning the following question:

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...