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

时间:2018-07-24 16:08:00

标签: c++ json node.js tcp

我试图通过JSON使用JSON从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函数中的数据缓冲区有关? 任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:2)

您在send/recv上犯了一些常见错误:

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