in reply to multiple connections

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 }

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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: multiple connections
by xarex (Initiate) on May 21, 2007 at 06:21 UTC
    Hi BrowserUK, Could you give me some tips on how to clean up the code, If i could extend it to 1000's of connections that would be fantastic, Also would it somehow be possible to create different logs based on the ip? K-
      Could you give me some tips on how to clean up the code, If i could extend it to 1000's of connections that would be fantastic, Also would it somehow be possible to create different logs based on the ip?

      Hmm. Sure, but there are some worrying signs in your questions.

      Cleaning up the piece of code that you posted and making it write a separate log file for each in-bound ip is ... well, frankly easy. That is, it is a pretty simple task and anyone with a small amount of programming experience would be able to see how to do this themselves.

      If you cannot see how to do that, it makes me nervous about trying to help you further as it tends to indicate that you are not looking for someone to help you, but rather looking for some to do this for you. This place is a self-help community, not a 'bespoke software for free' shop.

      Here's the thing. The greatest tip for writing good multitasking code is

      1. Make your code do everything you need it to do for one connection first.
      2. Then make it do the right thing for two serial connections.
      3. Then, when you are happy with that, make it do it for two concurrent connections.
      4. Once you have that working, extending it to do it for more, up to the limitations of your hardware is usually relatively simple.

      So, if you really just want help, not someone to write this for you, I suggest that you try and extend your original code to handle step 2 above.

      You say "that works 100% basically i have a open port and it listens for connections then once a connection is established it saves all the data to a log file, ...".

      As it is, each time you have something to write to a log file, you are opening that log file using the following line:

      open ($log, '>>','log.txt') || die "Couldn't open log.txt: $!";

      Now, that is obviously going to put all the data into a single file.

      You also have a variable, my $client_ipnum = inet_ntoa($c_ip); which from the name seems to indicate that it is the ip address of the connecting client.

      How do you think that you could modify your original program to write the input from different ips to different log files?


      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.
        thanks, i get it now, :) i could simply add
        open ($log, '>>','log.$client_ipnum.txt')
        or something in those lines? Kenny