gpt4 book ai didi

c - gSOAP 不正确的响应数据

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:39:53 26 4
gpt4 key购买 nike

我正在尝试在 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

最佳答案

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

关于c - gSOAP 不正确的响应数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44044517/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com