c++ - 从 C++ 客户端通过 TCP 将数据发送到 JSON 服务器

标签 c++ json node.js tcp

我正在尝试通过 TCP 从 C++ 客户端使用 JSON 将数据发送到本地服务器。这是我的 JSON 服务器

comm.js

'use strict';

const net = require('net');


var client = new net.Socket();
var _SIGNATURE = 206;

client.connect(5000, '192.168.1.107', function () {
  let info = {
    "alo":"alo"
  }

  let message = JSON.stringify(info);


    var messageLength = Buffer.byteLength(message, 'utf8');
    var buffer = Buffer.alloc(messageLength + 6);
  console.log(buffer);

    buffer.writeUInt16LE(_SIGNATURE, 0);
  console.log(buffer);

    buffer.writeUInt32LE(messageLength, 2);
  console.log(buffer);

    buffer.write(message, 6);
  console.log(buffer);
  console.log(`${buffer} -> ${messageLength}` );

  client.write(buffer);

})

还有我的 C++ 客户端

/**
    C++ client
*/
#include<iostream>    //cout
#include <sstream>
#include<stdio.h> //printf
#include<string.h>    //strlen
#include<string>  //string
#include<sys/socket.h>    //socket
#include<arpa/inet.h> //inet_addr
#include<netdb.h> //hostent

using namespace std;

/**
    TCP Client class
*/
class tcp_client  {

private:

    int sock;
    std::string address;
    int port;
    struct sockaddr_in server;

public:

    tcp_client();
    bool conn(string, int);
    bool send_data(string data);
    string receive(int);
};

tcp_client::tcp_client()  {
    sock = -1;
    port = 0;
    address = "";
}

/**
    Connect to a host on a certain port number
*/
bool tcp_client::conn(string address , int port)  {
    //create socket if it is not already created
    if(sock == -1)
    {
        //Create socket
        sock = socket(AF_INET , SOCK_STREAM , 0);
        if (sock == -1)
        {
            perror("Could not create socket");
        }

        cout<<"Socket created\n";
    }
    else    {   /* OK , nothing */  }

    //setup address structure
    if(inet_addr(address.c_str()) == -1)  {

        struct hostent *he;
        struct in_addr **addr_list;

        //resolve the hostname, its not an ip address
        if ( (he = gethostbyname( address.c_str() ) ) == NULL)  {

            //gethostbyname failed
            herror("gethostbyname");
            cout<<"Failed to resolve hostname\n";

            return false;
        }

        //Cast the h_addr_list to in_addr , since h_addr_list also has the ip address in long format only
        addr_list = (struct in_addr **) he->h_addr_list;

        for(int i = 0; addr_list[i] != NULL; i++) {
            //strcpy(ip , inet_ntoa(*addr_list[i]) );
            server.sin_addr = *addr_list[i];

            cout<<address<<" resolved to "<<inet_ntoa(*addr_list[i])<<endl;

            break;
        }
    }

    //plain ip address
    else  {
        server.sin_addr.s_addr = inet_addr( address.c_str() );
    }

    server.sin_family = AF_INET;
    server.sin_port = htons( port );

    //Connect to remote server
    if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)  {

        perror("connect failed. Error");
        return 1;
    }

    cout<<"Connected\n";
    return true;
}

/**
    Send data to the connected host
*/
bool tcp_client::send_data(string data) {

    //Send some data
    if( send(sock , data.c_str() , strlen( data.c_str() ) , 0) < 0) {

        perror("Send failed : ");
        return false;
    }
    cout<<"Data send\n";

    return true;
}

/**
    Receive data from the connected host
*/
string tcp_client::receive(int size=512)  {

    char buffer[size];
    string reply;

    //Receive a reply from the server
    if( recv(sock , buffer , sizeof(buffer) , 0) < 0) {

        puts("recv failed");
    }

    reply = buffer;
    return reply;
}

int main(int argc , char *argv[]) {

    tcp_client c;
    string host, test;

    host = "192.168.1.107";

    cout<<"Hostname : 192.168.1.107:5000";

    //connect to host
    c.conn(host , 5000);

    //send some data
    std::cout << "Enter string to send:" << '\n';
    cin >> test;


    c.send_data(test);

    //receive and echo reply
    cout<<"----------------------------\n\n";
    cout<<c.receive(1024);
    cout<<"\n\n----------------------------\n\n";

    //done
    return 0;
}

问题是 JSON 服务器需要首先以字节形式接收 _SIGNATURE 206 的值才能识别数据,否则它会崩溃。我已经尝试过移位,但没有用,我还尝试了 memset() 来自客户端的数据字符串,但也失败了。 我正在研究以找到解决方案,但希望这里有人知道答案,也许与发送函数中的数据缓冲区有关? 任何帮助表示赞赏!

最佳答案

你在 send/recv 中犯了一些常见错误:

  • send 不一定发送整个缓冲区。您需要继续调用 send 直到所有字节都已发送。
  • recv 接收的数据可能少于预期,并且接收到的数据不是零终止的,因此您不能对其执行 strlen 或将其传递给 std::字符串。而是 reply.append(buffer, received_bytes);,其中 received_bytesrecv 的返回值。

关于c++ - 从 C++ 客户端通过 TCP 将数据发送到 JSON 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51503190/

相关文章:

javascript - 如何在 Angular JS 中读取 JSON 对象

node.js - 使用 Couchnode 模块 v.1.2.4 从 Node.js 连接到 CouchBase 时出错

node.js - 如何在无服务器 Node js中使用i18next?

c++ - 交错 std::map 插入和迭代

json - 所有对象的单个自定义反序列化器作为它们的 ID 或在 POST/PUT 期间嵌入整个对象

c++ - 为什么我的有限状态机需要这么长时间才能执行?

javascript - 如何在 Javascript React Native 中获取对象的子值总和?

node.js - 如何在处理请求时获取node.js中的端口号?

android - 我可以在 iOS 中使用 C++ 模板吗?

c++ - 无需汇编的 C/C++ 函数定义