Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: socket server threads or fork

by BrowserUk (Pope)
on Jun 18, 2013 at 17:53 UTC ( #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.


Comment on Re: socket server threads or fork
Download Code
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
Node Status?
node history
Node Type: note [id://1039615]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2014-07-22 21:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (128 votes), past polls