in reply to Re^2: multiple connections
in thread multiple connections

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

Replies are listed 'Best First'.
Re^4: multiple connections
by xarex (Initiate) on May 21, 2007 at 09:09 UTC
    thanks, i get it now, :) i could simply add
    open ($log, '>>','log.$client_ipnum.txt')
    or something in those lines? Kenny

      The markup for code here is <code> ... </code> not [script] ... [/script].

      could simply add open ($log, '>>','log.$client_ipnum.txt') or something in those lines?

      Yes. Kinda :). The value of the variable $client_ipnum with not be substituted (interpolated) into a string that uses single quote (') delimiters. So, the way you have it now, the file opened would still have the same name every time: log.$client_ipnum.txt.

      To get the effect you want, you will need to use double quotes for the string:

      open ($log, '>>', "log.$client_ipnum.txt" )

      That will make your filenames something like:

      log.62.123.1.100.txt log.168.0.0.123.txt

      I point this out to make sure that is what you intended?


      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.
        BrowserUK,

        I am busy rading in o'rileys PHP Wrox , about the different types of servers and have a quick question...

        Which is better ( Less of a resources hog)

        a) Forking Server
        b) Polling Server
        c) Threaded Server

        Forgive all the stupid questions, but i am actually a php programmer, and simply need to get the data to a log file so i can process it using PHP

        K-