Add the following three lines to your current program, and it will allow it to maintain as many concurrent connections as you have memory to support. That should be at least 100.
If that's too limiting, there are some simple things that can be done to extend that to 1000's. And they say threads are hard :)
Your code is on need of a little cleaning up, like opening the log file outside of the read loop, and closing it once the connection drops, but in essence it should just work.
#!/usr/bin/perl -w use threads; ######################## Added my $socket; my $port = 7777; my $host; use IO::Socket; use Sys::Hostname; $host = hostname(); my $sock = new IO::Socket::INET( LocalHost => $host, LocalPort => $port, Proto => 'tcp', Listen => 5, Reuse => 1, ); if (!$sock){ die "no socket :$!"; } my($new_sock, $c_addr, $buf); #open($log, '>>', 'log.txt') || die "Couldn't open log.txt: $!"; while (($new_sock, $c_addr) = $sock->accept()) { async { ################## Added my ($client_port, $c_ip) =sockaddr_in($c_addr); my $client_ipnum = inet_ntoa($c_ip); my $client_host =gethostbyaddr($c_ip, AF_INET); print "got a connection from: $client_host"," [$client_ipnum] "; while (defined ($buf = <$new_sock>)) { open ($log, '>>','log.txt') || die "Couldn't open log.txt: $!"; print $buf; print $log $buf; close $log; } } ########################## Added }
In reply to Re: multiple connections
by BrowserUk
in thread multiple connections
by xarex
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |