Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

socket server threads or fork

by lenieto3 (Acolyte)
on Jun 18, 2013 at 17:29 UTC ( #1039609=perlquestion: print w/ replies, xml ) Need Help??
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...

Comment on socket server threads or fork
Download Code
Re: socket server threads or fork
by BrowserUk (Pope) on Jun 18, 2013 at 17:40 UTC
    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.

    If you are on *nix, use fork; if you are on Windows, use threads. If you need to run anywhere perl does, use the latter.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: socket server threads or fork
by BrowserUk (Pope) on Jun 18, 2013 at 17:53 UTC

    To get you started.

    Ostensibly, you only need to add 4 lines to your existing code (here cleaned up a little) to make it a threaded socket server:

    #!/usr/bin/perl use IO::Socket; use threads; ## 1 use strict; my $sock = new IO::Socket::INET ( LocalHost => '127.0.0.1', LocalPort => '6590', Proto => 'tcp', Listen => 1, Reuse => 1, ) or die "Could not create socket: $!\n"; while(1){ my $new_sock = $sock->accept(); print $sock->connected(); async { ## 2 while(<$new_sock>) { print $_; } close $new_sock; }->detach; ## 3 sleep 0; ## 4 }

    Note: That is untested, and not a perfectly working socket server; but pretty much everything that needs adding; also needs adding to your original code.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thanks for the lines, but I'm on linux, so I'll use fork. Could you please help me with the fork?
        Could you please help me with the fork?

        Sorry, no. I've no expertise with fork, nor do I run an OS that provides it natively.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: socket server threads or fork
by sundialsvc4 (Monsignor) on Jun 18, 2013 at 17:58 UTC

    Also, have a look in the CPAN library, and do some serious soul-searching here.   There are many workload-server systems (POE is only one).   You don’t have to start from scratch here.

    A principle that you will see in your investigations is that most-often there is not either a thread or a process “per connection,” because that does not scale well.   Instead, when a socket connection is accepted, the request (and socket handle) are handed to a pool of service processes/threads, which remove the request from a queue and process it.   These threads survive to process many requests during their lifetime, and there’s never more than a specified number of them, nor less than another specified number, no matter how many requests are in-process or waiting to be processed at a time.   To prevent flooding, the systems won’t allow the total request-count to grow too large.

    “Do not do a thing already done.™”

      Code?

      (A complete socket server using POE? Something that adds two numbers together? Anything?)


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1039609]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2014-08-22 11:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (156 votes), past polls