Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-23 07:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found