send()发送结构体

              send()默认第二个参数只能发送char*类型数据,怎么发送一个结构体呢?

方法一:

          定义一个结构体,实例化结构体对象。通过memcpy()给结构体变量的成员赋值,这样是直接赋值到对象所在的内存中。

代码如下:

 

struct  SNewStruct
{
	char lenth[10];
	char str[20];

};


//发送数据
	char SendBuf[500];

	memset(SendBuf, 0, sizeof(SendBuf));
	//memset(&newstruct, 0, sizeof(newstruct));

	//newstruct.lenth=sizeof(newstruct.str)-1;
	memcpy(newstruct.lenth, "10", sizeof("10"));
	memcpy(newstruct.str, "send struct test", sizeof("send struct test"));
	int len=sizeof(newstruct);
	send(SockClient, (char *)&newstruct, len, 0);

       接收端代码:

//接收数据
  char RecvBuf[200];
  memset(RecvBuf, 0, sizeof(RecvBuf));//清空接收缓存
  recv(NewSocket, RecvBuf, sizeof(RecvBuf), 0);
  memset(&newstruct, 0, sizeof(newstruct));//清空结构体
  memcpy(&newstruct, RecvBuf, sizeof(newstruct));


  //打印数据
  //printf("%s\n",RecvBuf);
  printf("%s\n",newstruct.lenth);
  printf("%s\n",newstruct.str);

 

方法二:

    定义一个结构体,结构体内部用构造函数对数据成员进行初始化,再实例化结构体变量。将结构体变量地址强制转换为(char *),最后通过send()发送

代码如下:

struct  SNewStruct
{
public :
	int lenth;
	char str[3];

	SNewStruct()
	{
		lenth=10;
		str[0]='z';
		str[1]='b';
		str[2]='\0';
	}

};


 

	//发送数据
	char SendBuf[500];

	int len=sizeof(newstruct);
	printf("%d\n",newstruct.lenth);
	send(SockClient, (char *)&newstruct, len, 0);


接收端代码:

//接收数据
  char RecvBuf[200];
  memset(RecvBuf, 0, sizeof(RecvBuf));//清空接收缓存
  recv(NewSocket, RecvBuf, sizeof(RecvBuf), 0);
  SNewStruct *p=(SNewStruct *)RecvBuf;

 

  //打印数据
  printf("%d\n",p->lenth);
  printf("%s\n",p->str);

 

 


 

要使用 oatpp 库发送结构体,您可以执行以下步骤: 1. 创建一个 oatpp::Object 对象,该对象表示要发送结构体。 2. 使用 oatpp 库中提供的 oatpp::web::client::HttpRequestExecutor 类创建 HTTP 请求。 3. 将 oatpp::Object 对象作为请求正文发送。 以下是一个简单的示例代码,演示如何使用 oatpp 发送结构体: ```c++ #include "oatpp/web/client/HttpRequestExecutor.hpp" #include "oatpp/core/macro/codegen.hpp" #include OATPP_CODEGEN_BEGIN(DTO) class MyDto : public oatpp::DTO { DTO_INIT(MyDto, DTO) DTO_FIELD(Int32, statusCode); DTO_FIELD(String, message); }; #include OATPP_CODEGEN_END(DTO) int main() { // Create an oatpp::Object representing the struct to send auto dto = MyDto::createShared(); dto->statusCode = 200; dto->message = "Hello, World!"; // Create an HTTP request executor auto requestExecutor = oatpp::web::client::HttpRequestExecutor::createShared("http://localhost:8080"); // Send the struct as the request body auto response = requestExecutor->execute(oatpp::web::client::HttpRequest::createShared( "POST", "/api/my-endpoint", dto // Pass the DTO as the request body )); // Handle the response if(response->getStatusCode() == 200) { // Handle success case } else { // Handle error case } return 0; } ``` 该示例创建了一个名为 MyDto 的结构体,该结构体具有两个字段:statusCode 和 message。然后,它创建了一个 oatpp::Object 对象,表示要发送结构体。最后,它使用 oatpp 库中提供的 oatpp::web::client::HttpRequestExecutor 类创建 HTTP 请求,并将 oatpp::Object 对象作为请求正文发送
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值