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."

In reply to Re^3: multiple connections by BrowserUk
in thread multiple connections by xarex

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.