title: C++ Structure declaration and usage to hold IP + Connections
body: <p>Good day to all, i am having a massive confusion declaring and using my structure to hold <strong>[IP] - [Connections]</strong> record.
  I am trying to insert the IP address that is connecting into the structure, and his connection number <strong>#</strong>, for example if <strong>IP 123.123.12</strong> connects 2 (two) times, then update the <strong>[Connections]</strong> number in the struct for that <strong>IP (123.123.12)</strong>.</p>
  
  <p>I have the following code, wich should work:</p>
  
  <pre><code>// the struct
  typedef struct {
      int id; // is this usefull anyway ?
      char *ip;
      int connNumbers;
  }test_sock;
  
  // init struct
  test_sock holder[5000];
  int len = 0;
  
  // the function
  void AddtoStruct(char *ip)
  {
  
      if (len == 0) //if empty, insert.
      {
          len++;
          holder-&gt;id = len;
          holder-&gt;ip = ip;
          holder-&gt;connNumbers = 1; //1 conexiune
  
          return;
      }
  
      for (int i = 0; i&lt;len; i++)
      {
  
  
  
          if (test_sock-&gt;id != 0) //check if its the same id !?
          {
                  //Exista deja in structura , doar increase connNumbers;
                  if (strcmp(ip, holder-&gt;ip) == 0)
                  {
                      holder++;
                      holder-&gt;connNumbers++;
                      holder-&gt;id = antiddos_len;
                      holder-&gt;ip = ip;
  
  
  
                      return; // should return or not ?!
                  }
                  else{ //new IP, insert into struct.
                      len++; // COUNT AGAIN ?
                      holder-&gt;id = len;
                      holder-&gt;ip = ip;
                      holder-&gt;connNumbers = 1; // 1 connection
  
  
                      return; // should return or not ?!
                  }
          }
      }
  }
  </code></pre>
  
  <p>Ok so what it should is:
  Check the new incoming <strong>IP</strong> if it`s <strong>ALLREADY</strong> in the structure, then increase the number of connections for that ip
  If the new incoming <strong>IP</strong> is <strong>NOT</strong> inside the structure, insert it, and if he connects again, of course increase the number of connections.</p>
  
  <p>I compiled a minimal example below, wich you can run without problems on a Windows machine, using Visual Studio (i used 2013).</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;iostream&gt;
  #include &lt;Windows.h&gt;
  using namespace std;
  
  // the struct
  typedef struct {
      int id; // is this usefull anyway ?
      char *ip;
      int connNumbers;
  }test_sock;
  
  // init struct
  test_sock holder[5000];
  int len = 0;
  
  // the function
  void AddtoStruct(char *ip)
  {
  
      if (len == 0) //if the struct is empty, insert.
      {
          len++;
          holder-&gt;id = len;
          holder-&gt;ip = ip;
          holder-&gt;connNumbers = 1; //1 conexiune
          cout &lt;&lt; "ADDED NEW IP: " &lt;&lt; holder-&gt;ip &lt;&lt; " Connections: " &lt;&lt; holder-&gt;connNumbers &lt;&lt; endl;
          cout &lt;&lt; "-----------------------------------------------------------" &lt;&lt; endl;
          return;
      }
  
      for (int i = 0; i &lt;= len; i++)
      {
  
          if (holder-&gt;id != 0) //verificam ca sa nu fie ACELASI ID
          {
              //Exista deja in structura , doar increase connNumbers;
              if (strcmp(ip, holder-&gt;ip) == 0)
              {
                  len++;
                  holder-&gt;connNumbers++;
                  holder-&gt;id = len;
                  holder-&gt;ip = ip;
                  cout &lt;&lt; "NEW CONNECTION FROM IP: " &lt;&lt; holder-&gt;ip &lt;&lt; " Connections: " &lt;&lt; holder-&gt;connNumbers &lt;&lt; endl;
                  cout &lt;&lt; "-----------------------------------------------------------" &lt;&lt; endl;
              }
              else{ //new IP, insert into struct.
                  len++; // COUNT AGAIN ?
                  holder-&gt;id = len;
                  holder-&gt;ip = ip;
                  holder-&gt;connNumbers = 1; // 1 connection
                  cout &lt;&lt; "CONNECTION FROM NEW IP: " &lt;&lt; holder-&gt;ip &lt;&lt; " Connections: " &lt;&lt; holder-&gt;connNumbers &lt;&lt; endl;
                  cout &lt;&lt; "-----------------------------------------------------------" &lt;&lt; endl;
                  return; // should return or not ?!
              }
          }
      }
  }
  
  // use the function
  
  int main() {
  
      char *ip = "127.0.0.1";
  
      char *ip2 = "127.0.0.3";
  
      AddtoStruct(ip);
      Sleep(5); // wait for new IP
      AddtoStruct(ip2);
      Sleep(5); // wait for SAME IP
      AddtoStruct(ip);
  
      system("pause");
  }
  </code></pre>
  
  <p>As you can see, if you run the code, it does not work as it should, it count the same number of connections for every new IP ...
  So please give me advice, or a fix, or anything, because i really need this, and it`s been 3 days of testing, without any progress.
  Many thanks!</p>