gSOAP错误的响应数据

时间:2017-05-18 09:56:26

标签: c linux soap embedded gsoap

我正在尝试在Linux上运行的嵌入式系统上设置SOAP服务器,以便向其发送命令并获得响应。我选择使用gSOAP,因为我在C编码。我写了一个简单的gsoap服务器,在我的嵌入式系统和客户端应用程序上运行,在我的linux PC上运行。现在我只是实现了服务器,因为它将请求中的命令数据复制到响应并发送。 但我在PC上收到的数据与我发送的数据不同。请帮忙。 我已附上我的服务器和客户端代码

标题

struct data_packet
{
   uint8_t *data;
   int8_t data_length;
};

int ns__send_command(struct data_packet cmd_packet, struct data_packet *resp_packet);

服务器

#include "soapH.h" // include the generated declarations 
#include "ns.nsmap" // include the XML namespace mappings 
int main() 
{
  struct soap soap;
  int m, s, i; // master and slave sockets with loop variable
  soap_init(&soap);
  m = soap_bind(&soap, "10.0.0.10", 2012, 100);
  if (m < 0)
     soap_print_fault(&soap, stderr);
  else
  {
     fprintf(stderr, "Socket connection successful: master socket = %d\n", m);
     for (i = 1; ; i++)
     {
        s = soap_accept(&soap);
        if (s < 0)
        {
           soap_print_fault(&soap, stderr);
           break;
        }
     fprintf(stderr, "%d: accepted connection from IP=%d.%d.%d.%d socket=%d", i,
        (int)(soap.ip >> 24)&0xFF, (int)(soap.ip >> 16)&0xFF, (int)(soap.ip >> 8)&0xFF, (int)soap.ip&0xFF, s);
        if (soap_serve(&soap) != SOAP_OK) // process RPC request
           soap_print_fault(&soap, stderr); // print error
        fprintf(stderr, "request served\n");
        soap_destroy(&soap); // clean up class instances
        soap_end(&soap); // clean up everything and close socket
     }
  }
  soap_done(&soap); // close master socket and detach context
}

int ns__send_command(struct soap *soap, struct data_packet cmd_packet, struct data_packet *resp_packet)
{ 
   resp_packet->data_length = cmd_packet.data_length;
   resp_packet->data = (uint8_t*)soap_malloc(soap, sizeof(uint8_t*) * resp_packet->data_length);
   memcpy(&resp_packet->data[0], &cmd_packet.data[0], resp_packet->data_length);
   return SOAP_OK; 
}

客户端

#include "soapH.h"
#include "ns.nsmap"

int main()
{
    struct soap soap;
    uint8_t cmd_ping[16] = {0x56, 0x69, 0x56, 0x4F, 0x70, 0x61, 0x79, 0x56, 0x33, 0x00, 0x18, 0x01, 0x00, 0x00, 0x04, 0x7E};

    struct data_packet cmd_packet;
    struct data_packet resp_packet;

    cmd_packet.data_length = 16;
    cmd_packet.data = (uint8_t*)soap_malloc(&soap, sizeof(uint8_t*) * cmd_packet.data_length);
    memcpy(&cmd_packet.data[0], &cmd_ping[0], cmd_packet.data_length);

    soap_init1(&soap, SOAP_XML_INDENT);

    soap_call_ns__send_command(&soap, "http://10.0.0.10:2012", "", cmd_packet, &resp_packet);
    if (soap.error)
    {   
        soap_print_fault(&soap, stderr);
        exit(1);
    }
    else
    {
        int j;
        printf("Command Data, length = %d\n", cmd_packet.data_length);
        for(j = 0; j < cmd_packet.data_length; j++)
            printf("%02X ", cmd_packet.data[j]);
        printf("\n");
        printf("Response Data, length = %d\n", resp_packet.data_length);
        for(j = 0; j < resp_packet.data_length; j++)
            printf("%02X ", resp_packet.data[j]);
        printf("\n");
    }

    soap_destroy(&soap);
    soap_end(&soap);
    soap_done(&soap);
    return 0;       
}

客户端输出

Command Data, length = 16
56 69 56 4F 70 61 79 56 33 00 18 01 00 00 04 7E 
Response Data, length = 16
56 00 DE C0 F4 C8 45 09 04 00 00 00 F9 06 02 00 

1 个答案:

答案 0 :(得分:1)

在soap服务器中使用soap_memcpy而不是memcpy解决了问题