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