用c+连接到hs100+;插座 我是C++世界的新手。但是我想制作一个脚本来打开/关闭我的tp-link开关。我已经找到了一个脚本,但是我想用C++来实现。p>

用c+连接到hs100+;插座 我是C++世界的新手。但是我想制作一个脚本来打开/关闭我的tp-link开关。我已经找到了一个脚本,但是我想用C++来实现。p>,c++,sockets,C++,Sockets,经过几天的尝试,阅读网页和应对代码,我有这个发送请求,但有一些问题。但我不知道问题出在哪里 我可以在Wireshark概述中看到,请求的内容已发送到设备 更新日期:2017年4月22日: 我已经用互联网上的十六进制解码器更新了C++。p> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.

经过几天的尝试,阅读网页和应对代码,我有这个发送请求,但有一些问题。但我不知道问题出在哪里

我可以在Wireshark概述中看到,请求的内容已发送到设备

更新日期:2017年4月22日: 我已经用互联网上的十六进制解码器更新了C++。p>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string>
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 < 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", &(servAddr.sin_addr));

    /* create socket */
    sd = socket(AF_INET, SOCK_STREAM, 0);
    rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));
    if (rc < 0) {
        puts("error by send");
    } else {
        puts("connected");
    }

    message = decode("0000002ad0f281f88bff9af7d5ef94b6c5a0d48bf99cf091e8b7c4b0d1a5c0e2d8a381f286e793f6d4eedfa2dfa2");

    rc = send(sd, (void *)(&message), sizeof(message), 0);
    if (rc < 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 *)(&message), sizeof(message), 0);

    if (rc < 0) {
        puts("error bij send 2");
        close(sd);
        exit(1);
    } else puts("dit send it the second time");
    return 0;
}
C++代码
你检查过你发现的脚本是否有效吗?如果是这样,那么比较脚本发送的数据包和C++程序发送的数据包。看看区别是什么。你发送了错误的数据。我从Python和Bash脚本中看到了十六进制编码的数据和base64编码的数据(请注意,这些脚本不仅仅发送字符串)。您需要发送以字符串而不是字符串编码的原始二进制数据itself@AndreyTurkin谢谢你的回复!我已经在主题中添加了api的命令。所以我必须用base64或hex将这些对象转换为原始二进制数据。我添加了一个解码器,这是我从het互联网上找到的。但是,当我使用Wireshark@andreyturkin对消息进行调查时,消息是否仍然不同?这不是发送
std::string
中包含的数据的方式。如果你能解决这个问题,它可能会起作用。然而,这整个处理字符串和解码器的过程并不是必需的,因为在C/C++中,您可以使用一个填充了二进制数据的字节数组。
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}}}'
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

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", &(servAddr.sin_addr));

    /* create socket */
    sd = socket(AF_INET, SOCK_STREAM, 0);
    rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));
    if (rc < 0) {
        puts("error bij send");
    } else {
        puts("connected");
    }

    rc = send(sd, "0000002ad0f281f88bff9af7d5ef94b6c5a0d48bf99cf091e8b7c4b0d1a5c0e2d8a381f286e793f6d4eedfa2dfa2", strlen("0000002ad0f281f88bff9af7d5ef94b6c5a0d48bf99cf091e8b7c4b0d1a5c0e2d8a381f286e793f6d4eedfa2dfa2"), 0);
    if (rc < 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 < 0) {
        puts("error bij send 2");
        close(sd);
        exit(1);
    } else puts("dit send it the second time");
    return 0;
}
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