SOCKET发送结构体时,必须使用连续的内存。也就是结构内(嵌套)不能包含指针。
发送时将结构体转为字节流
1. 定长结构体
2. 变长结构体
//参考
http://blog.csdn.net/zhaobangyu/article/details/8904005
阅读(1686) | 评论(0) | 转发(0) |
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
发送时将结构体转为字节流
1. 定长结构体
- struct Sendbuf
- {
- char SendDate[1024]; //定长的数据
- };
-
- const char *msg = "Think You!!!"; //这里是要发送的内容
-
- int SendDateklength = sizeof(Sendbuf) + strlen(msg)+1; //计算要发送的总的长度 (结构体长度+发送内容的长度)
- Sendbuf SendDate;
-
- SendDate.length = strlen(msg);
- strcpy_s(SendDate.SendDate,strlen(msg) + 1,msg);
//将结构体首地址转为(const char*),发送
- send(socket,(const char *)&SendDate,SendDateklength,0)
- struct Sendbuf
- {
- char sendDataWarning[0]//VS2010: warning C4200: 使用了非标准扩展 : 结构/联合中的零大小数组.
- // 当 UDT 包含大小为零的数组时,无法生成复制构造函数或副本赋值运算符
- char SendDate[1]; //未知长度的数据
- int length; //发送数据的长度
- };
- const char *msg = "Think You!!!"; //这里是要发送的内容(msg是未知大小的内容)
- int SendDateklength = sizeof(Sendbuf) + strlen(msg) +1; //计算要发送的总的长度 (结构体长度+发送内容的长度)
-
- Sendbuf *SendDate = (Sendbuf*)new char[SendDateklength ]; //申请待发送的数据的实际长度
- SendDate->length = strlen(msg);
- strcpy_s(SendDate->SendDate,strlen(msg) + 1,msg); //这里使用strcpy_s赋值
-
- send(socket,(const char *)SendDate,SendDateklength,0); //Send
- delete []SendDate; //发送完之后记得释放内存
http://blog.csdn.net/zhaobangyu/article/details/8904005
相关热门文章
给主人留下些什么吧!~~
评论热议