title: connect to hs100 with c++ sockets
body: <p>I'm new to the c++ world. But I want to make a script that turns on / off my tp-link switch. I have found a <a href="https://github.com/ggeorgovassilis/linuxscripts/blob/master/tp-link-hs100-smartplug/hs100.sh" rel="nofollow noreferrer">bash</a>, <a href="https://github.com/plasticrake/hs100-api" rel="nofollow noreferrer">javascript</a> and <a href="https://github.com/natefox/tplink-hs100/blob/master/turn_on.py" rel="nofollow noreferrer">python</a> script to do this but I want to make it in c++. </p>
  
  <p>After a few days trying, reading webpages and coping code I have this that sends a request but there is something wrong with it. But I can't figure out what the problem is. </p>
  
  <p>I can see in the Wireshark overview that the requested are send to the device. </p>
  
  <h2>update 22-4-2017:</h2>
  
  <p>I have updated the c++ with a hex decoder from the internet. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;string&gt;
  using namespace std;
  
  string decode(string hex);
  int tcp_send();
  
  int main(int argc, char *argv[]) {
      tcp_send();
  }
  
  string decode(string hex) {
      unsigned long len = hex.length();
      string newString;
      for (int i = 0; i &lt; len; i += 2) {
          string byte = hex.substr(i, 2);
          char chr = (char) (int) strtol(byte.c_str(), nullptr, 16);
          newString.push_back(chr);
      }
      return newString;
  }
  int tcp_send() {
      int sd, rc;
      struct sockaddr_in servAddr;
      string message;
  
      servAddr.sin_family = AF_INET;
      servAddr.sin_port = htons(9999);
      inet_pton(AF_INET, "192.168.2.4", &amp;(servAddr.sin_addr));
  
      /* create socket */
      sd = socket(AF_INET, SOCK_STREAM, 0);
      rc = connect(sd, (struct sockaddr *) &amp;servAddr, sizeof(servAddr));
      if (rc &lt; 0) {
          puts("error by send");
      } else {
          puts("connected");
      }
  
      message = decode("0000002ad0f281f88bff9af7d5ef94b6c5a0d48bf99cf091e8b7c4b0d1a5c0e2d8a381f286e793f6d4eedfa2dfa2");
  
      rc = send(sd, (void *)(&amp;message), sizeof(message), 0);
      if (rc &lt; 0) {
          puts("error bij send 1");
          close(sd);
          exit(1);
      } else puts("did send it");
  
      sleep(1);
  
      message = decode("AAAAKtDygfiL/5r31e+UtsWg1Iv5nPCR6LfEsNGlwOLYo4HyhueT9tTu36Lfog==");
  
      rc = send(sd, (void *)(&amp;message), sizeof(message), 0);
  
      if (rc &lt; 0) {
          puts("error bij send 2");
          close(sd);
          exit(1);
      } else puts("dit send it the second time");
      return 0;
  }
  </code></pre>
  
  <h2>update 21-4-2017:</h2>
  
  <p>These are the commands that I can send (hex encoded) </p>
  
  <pre><code>commands = {'info'     : '{"system":{"get_sysinfo":{}}}',
              'on'       : '{"system":{"set_relay_state":{"state":1}}}',
              'off'      : '{"system":{"set_relay_state":{"state":0}}}',
              'cloudinfo': '{"cnCloud":{"get_info":{}}}',
              'wlanscan' : '{"netif":{"get_scaninfo":{"refresh":0}}}',
              'time'     : '{"time":{"get_time":{}}}',
              'schedule' : '{"schedule":{"get_rules":{}}}',
              'countdown': '{"count_down":{"get_rules":{}}}',
              'antitheft': '{"anti_theft":{"get_rules":{}}}',
              'reboot'   : '{"system":{"reboot":{"delay":1}}}',
              'reset'    : '{"system":{"reset":{"delay":1}}}'
  }
  </code></pre>
  
  <h2>c++ code</h2>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  
  int tcp_send();
  
  int main(int argc, char *argv[]) {
      tcp_send();
  }
  
  int tcp_send() {
      int sd, rc;
      struct sockaddr_in servAddr;
  
      servAddr.sin_family = AF_INET;
      servAddr.sin_port = htons(9999);
      inet_pton(AF_INET, "192.168.2.4", &amp;(servAddr.sin_addr));
  
      /* create socket */
      sd = socket(AF_INET, SOCK_STREAM, 0);
      rc = connect(sd, (struct sockaddr *) &amp;servAddr, sizeof(servAddr));
      if (rc &lt; 0) {
          puts("error bij send");
      } else {
          puts("connected");
      }
  
      rc = send(sd, "0000002ad0f281f88bff9af7d5ef94b6c5a0d48bf99cf091e8b7c4b0d1a5c0e2d8a381f286e793f6d4eedfa2dfa2", strlen("0000002ad0f281f88bff9af7d5ef94b6c5a0d48bf99cf091e8b7c4b0d1a5c0e2d8a381f286e793f6d4eedfa2dfa2"), 0);
      if (rc &lt; 0) {
          puts("error bij send 1");
          close(sd);
          exit(1);
      } else puts("did send it");
  
      sleep(1);
  
      rc = send(sd, "AAAAKtDygfiL/5r31e+UtsWg1Iv5nPCR6LfEsNGlwOLYo4HyhueT9tTu36Lfog==", strlen("AAAAKtDygfiL/5r31e+UtsWg1Iv5nPCR6LfEsNGlwOLYo4HyhueT9tTu36Lfog=="), 0);
  
      if (rc &lt; 0) {
          puts("error bij send 2");
          close(sd);
          exit(1);
      } else puts("dit send it the second time");
      return 0;
  }
  </code></pre>
  
  <h2>wireshark calls:</h2>
  
  <pre><code>No.     Time           Source                Destination           Protocol Length Info
      214 8.443523       192.168.2.9           192.168.2.4           TCP      78     56667 → 9999 [SYN] Seq=0 Win=65535 Len=0 MSS=1460 WS=32 TSval=973574968 TSecr=0 SACK_PERM=1
  
  Frame 214: 78 bytes on wire (624 bits), 78 bytes captured (624 bits) on interface 0
  Ethernet II, Src: Apple_55:12:a7 (78:4f:43:55:12:a7), Dst: Tp-LinkT_01:4a:a9 (50:c7:bf:01:4a:a9)
  Internet Protocol Version 4, Src: 192.168.2.9, Dst: 192.168.2.4
  Transmission Control Protocol, Src Port: 56667, Dst Port: 9999, Seq: 0, Len: 0
  
  No.     Time           Source                Destination           Protocol Length Info
      217 8.446444       192.168.2.9           192.168.2.4           TCP      66     56667 → 9999 [ACK] Seq=1 Ack=1 Win=131744 Len=0 TSval=973574971 TSecr=18699694
  
  Frame 217: 66 bytes on wire (528 bits), 66 bytes captured (528 bits) on interface 0
  Ethernet II, Src: Apple_55:12:a7 (78:4f:43:55:12:a7), Dst: Tp-LinkT_01:4a:a9 (50:c7:bf:01:4a:a9)
  Internet Protocol Version 4, Src: 192.168.2.9, Dst: 192.168.2.4
  Transmission Control Protocol, Src Port: 56667, Dst Port: 9999, Seq: 1, Ack: 1, Len: 0
  
  No.     Time           Source                Destination           Protocol Length Info
      218 8.455485       192.168.2.9           192.168.2.4           TCP      112    56667 → 9999 [PSH, ACK] Seq=1 Ack=1 Win=131744 Len=46 TSval=973574980 TSecr=18699694
  
  Frame 218: 112 bytes on wire (896 bits), 112 bytes captured (896 bits) on interface 0
  Ethernet II, Src: Apple_55:12:a7 (78:4f:43:55:12:a7), Dst: Tp-LinkT_01:4a:a9 (50:c7:bf:01:4a:a9)
  Internet Protocol Version 4, Src: 192.168.2.9, Dst: 192.168.2.4
  Transmission Control Protocol, Src Port: 56667, Dst Port: 9999, Seq: 1, Ack: 1, Len: 46
  Data (46 bytes)
  
  0000  00 00 00 2a d0 f2 81 f8 8b ff 9a f7 d5 ef 94 b6   ...*............
  0010  c5 a0 d4 8b f9 9c f0 91 e8 b7 c4 b0 d1 a5 c0 e2   ................
  0020  d8 a3 81 f2 86 e7 93 f6 d4 ee df a2 df a2         ..............
  
  No.     Time           Source                Destination           Protocol Length Info
      219 8.455538       192.168.2.9           192.168.2.4           TCP      66     56667 → 9999 [FIN, ACK] Seq=47 Ack=1 Win=131744 Len=0 TSval=973574980 TSecr=18699694
  
  Frame 219: 66 bytes on wire (528 bits), 66 bytes captured (528 bits) on interface 0
  Ethernet II, Src: Apple_55:12:a7 (78:4f:43:55:12:a7), Dst: Tp-LinkT_01:4a:a9 (50:c7:bf:01:4a:a9)
  Internet Protocol Version 4, Src: 192.168.2.9, Dst: 192.168.2.4
  Transmission Control Protocol, Src Port: 56667, Dst Port: 9999, Seq: 47, Ack: 1, Len: 0
  
  No.     Time           Source                Destination           Protocol Length Info
      222 8.459966       192.168.2.9           192.168.2.4           TCP      66     56667 → 9999 [ACK] Seq=48 Ack=50 Win=131712 Len=0 TSval=973574984 TSecr=18699696
  
  Frame 222: 66 bytes on wire (528 bits), 66 bytes captured (528 bits) on interface 0
  Ethernet II, Src: Apple_55:12:a7 (78:4f:43:55:12:a7), Dst: Tp-LinkT_01:4a:a9 (50:c7:bf:01:4a:a9)
  Internet Protocol Version 4, Src: 192.168.2.9, Dst: 192.168.2.4
  Transmission Control Protocol, Src Port: 56667, Dst Port: 9999, Seq: 48, Ack: 50, Len: 0
  
  No.     Time           Source                Destination           Protocol Length Info
      224 8.460343       192.168.2.9           192.168.2.4           TCP      66     56667 → 9999 [ACK] Seq=48 Ack=51 Win=131712 Len=0 TSval=973574984 TSecr=18699696
  
  Frame 224: 66 bytes on wire (528 bits), 66 bytes captured (528 bits) on interface 0
  Ethernet II, Src: Apple_55:12:a7 (78:4f:43:55:12:a7), Dst: Tp-LinkT_01:4a:a9 (50:c7:bf:01:4a:a9)
  Internet Protocol Version 4, Src: 192.168.2.9, Dst: 192.168.2.4
  Transmission Control Protocol, Src Port: 56667, Dst Port: 9999, Seq: 48, Ack: 51, Len: 0
  
  No.     Time           Source                Destination           Protocol Length Info
     1397 68.198574      192.168.2.9           192.168.2.4           TCP      78     56704 → 9999 [SYN] Seq=0 Win=65535 Len=0 MSS=1460 WS=32 TSval=973634481 TSecr=0 SACK_PERM=1
  
  Frame 1397: 78 bytes on wire (624 bits), 78 bytes captured (624 bits) on interface 0
  Ethernet II, Src: Apple_55:12:a7 (78:4f:43:55:12:a7), Dst: Tp-LinkT_01:4a:a9 (50:c7:bf:01:4a:a9)
  Internet Protocol Version 4, Src: 192.168.2.9, Dst: 192.168.2.4
  Transmission Control Protocol, Src Port: 56704, Dst Port: 9999, Seq: 0, Len: 0
  
  No.     Time           Source                Destination           Protocol Length Info
     1399 68.202027      192.168.2.9           192.168.2.4           TCP      66     56704 → 9999 [ACK] Seq=1 Ack=1 Win=131744 Len=0 TSval=973634484 TSecr=18712532
  
  Frame 1399: 66 bytes on wire (528 bits), 66 bytes captured (528 bits) on interface 0
  Ethernet II, Src: Apple_55:12:a7 (78:4f:43:55:12:a7), Dst: Tp-LinkT_01:4a:a9 (50:c7:bf:01:4a:a9)
  Internet Protocol Version 4, Src: 192.168.2.9, Dst: 192.168.2.4
  Transmission Control Protocol, Src Port: 56704, Dst Port: 9999, Seq: 1, Ack: 1, Len: 0
  
  No.     Time           Source                Destination           Protocol Length Info
     1400 68.202117      192.168.2.9           192.168.2.4           TCP      158    56704 → 9999 [PSH, ACK] Seq=1 Ack=1 Win=131744 Len=92 TSval=973634484 TSecr=18712532
  
  Frame 1400: 158 bytes on wire (1264 bits), 158 bytes captured (1264 bits) on interface 0
  Ethernet II, Src: Apple_55:12:a7 (78:4f:43:55:12:a7), Dst: Tp-LinkT_01:4a:a9 (50:c7:bf:01:4a:a9)
  Internet Protocol Version 4, Src: 192.168.2.9, Dst: 192.168.2.4
  Transmission Control Protocol, Src Port: 56704, Dst Port: 9999, Seq: 1, Ack: 1, Len: 92
  Data (92 bytes)
  
  0000  30 30 30 30 30 30 32 61 64 30 66 32 38 31 66 38   0000002ad0f281f8
  0010  38 62 66 66 39 61 66 37 64 35 65 66 39 34 62 36   8bff9af7d5ef94b6
  0020  63 35 61 30 64 34 38 62 66 39 39 63 66 30 39 31   c5a0d48bf99cf091
  0030  65 38 62 37 63 34 62 30 64 31 61 35 63 30 65 32   e8b7c4b0d1a5c0e2
  0040  64 38 61 33 38 31 66 32 38 36 65 37 39 33 66 36   d8a381f286e793f6
  0050  64 34 65 65 64 66 61 32 64 66 61 32               d4eedfa2dfa2
  
  No.     Time           Source                Destination           Protocol Length Info
     1403 69.205674      192.168.2.9           192.168.2.4           TCP      130    56704 → 9999 [PSH, ACK] Seq=93 Ack=1 Win=131744 Len=64 TSval=973635485 TSecr=18712532
  
  Frame 1403: 130 bytes on wire (1040 bits), 130 bytes captured (1040 bits) on interface 0
  Ethernet II, Src: Apple_55:12:a7 (78:4f:43:55:12:a7), Dst: Tp-LinkT_01:4a:a9 (50:c7:bf:01:4a:a9)
  Internet Protocol Version 4, Src: 192.168.2.9, Dst: 192.168.2.4
  Transmission Control Protocol, Src Port: 56704, Dst Port: 9999, Seq: 93, Ack: 1, Len: 64
  Data (64 bytes)
  
  0000  41 41 41 41 4b 74 44 79 67 66 69 4c 2f 35 72 33   AAAAKtDygfiL/5r3
  0010  31 65 2b 55 74 73 57 67 31 49 76 35 6e 50 43 52   1e+UtsWg1Iv5nPCR
  0020  36 4c 66 45 73 4e 47 6c 77 4f 4c 59 6f 34 48 79   6LfEsNGlwOLYo4Hy
  0030  68 75 65 54 39 74 54 75 33 36 4c 66 6f 67 3d 3d   hueT9tTu36Lfog==
  
  No.     Time           Source                Destination           Protocol Length Info
     1404 69.206651      192.168.2.9           192.168.2.4           TCP      66     56704 → 9999 [FIN, ACK] Seq=157 Ack=1 Win=131744 Len=0 TSval=973635485 TSecr=18712532
  
  Frame 1404: 66 bytes on wire (528 bits), 66 bytes captured (528 bits) on interface 0
  Ethernet II, Src: Apple_55:12:a7 (78:4f:43:55:12:a7), Dst: Tp-LinkT_01:4a:a9 (50:c7:bf:01:4a:a9)
  Internet Protocol Version 4, Src: 192.168.2.9, Dst: 192.168.2.4
  Transmission Control Protocol, Src Port: 56704, Dst Port: 9999, Seq: 157, Ack: 1, Len: 0
  
  No.     Time           Source                Destination           Protocol Length Info
     1407 69.209081      192.168.2.9           192.168.2.4           TCP      66     56704 → 9999 [ACK] Seq=158 Ack=2 Win=131744 Len=0 TSval=973635487 TSecr=18712748
  
  Frame 1407: 66 bytes on wire (528 bits), 66 bytes captured (528 bits) on interface 0
  Ethernet II, Src: Apple_55:12:a7 (78:4f:43:55:12:a7), Dst: Tp-LinkT_01:4a:a9 (50:c7:bf:01:4a:a9)
  Internet Protocol Version 4, Src: 192.168.2.9, Dst: 192.168.2.4
  Transmission Control Protocol, Src Port: 56704, Dst Port: 9999, Seq: 158, Ack: 2, Len: 0
  </code></pre>