Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: socket server threads or fork

by BrowserUk (Patriarch)
on Jun 18, 2013 at 17:53 UTC ( [id://1039615]=note: print w/replies, xml ) Need Help??


in reply to socket server threads or fork

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.

Replies are listed 'Best First'.
Re^2: socket server threads or fork
by lenieto3 (Acolyte) on Jun 18, 2013 at 18:22 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1039615]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-03-29 05:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found