BOOST UDP 网络通信(2) --如何传输结构体
2017-07-16 12:19
351 查看
①、Struct.h -- 存放传输数据的结构体
//由于不同操作系统之间的字节对齐方式不同,故网络传输数据时,要设置字节对齐方式
//设置变量以n字节对齐方式,指定结构体、联合以及类成员
#pragma pack(push) //保持对齐状态
#pragma pack(1) //设定为1字节对齐
#ifndef _MESSAGE_HEADER
#define _MESSAGE_HEADER
typedef struct _MESSAGE_HEADER
{
unsigned short int uhMessageCode; //消息类型
unsigned short int uhMessageLength; //消息长度
unsigned long int ulTimeTag; //时间
unsigned short int uhSerialNumber; //消息序号;
unsigned char ucSpare[22] //空余位
}SOCKET_MESSAGE_HEADER;
#endif
namespace DateStruct
{
struct DS_STATE
{
char cID[32]; //ID
char cState; //状态
}
}
#pragma pack(pop) //恢复对齐状态
②、使用socket发送数据
{
//消息头;
SOCKET_MESSAGE_HEADER header;
memset( &header, 0, sizeof(header) );
header.uhMessageCode = 2;
//消息体;
DateStruct::DS_STATE sendMsg;
memset( &sendMsg, 0, sizeof(DateStruct::DS_STATE) );
std::string tempID = "LHC010";
memcpy( sendMsg.cID, tempID, sizeof(std::string) );
char tempState("1");
sendMsg.cState = tempState;
//消息体长度;
int bodyLength = sizeof( DateStruct::DS_STATE );
//创建并初始化buffer用于存储 "消息头+消息体";
char * buff = new char[sizeof(header) + bodyLength];
memset( buff, 0, sizeof(header)+ bodyLegth );
//将消息头拷贝到buffer中;
memcpy( buff, &header, sizeof(header) );
//将消息体拷贝到buffer中;
memcpy( buff+sizeof(header), &sendMsg, sizeof(sendMsg) );
//将char*类型转换成char类型,因为boost-udp发送数据时,接收char类型,而非char*类型;
char sendBuff[2000] = {0};
int bLen = sizeof(SOCKET_MESSAGE_HEADER) + sizeof(DateStruct::DS_STATE);
memcpy( sendBuff, buff, bLen );
//使用socket发送数据;
boost::shared_ptr<boost::asio::ip::udp::socket> _ScoketPtr;
boost::asio::ip::udp::endpoint _SendEndPoint;
_SocketPtr.reset( new boost::asio::ip::udp::socket(_IOS) );
_SocketPtr->send_to( boost::asio::buffer(sendBuff), _SendEndPoint, 0 );
}③ 接收数据-创建UDP
void ServerProcRecv()
{
size_t len;
boost::system::error_code ec;
//对方(即 发送数据端) 的端口与IP
boost::asio::ip::udp::endpoint revEndpoint;
while(true)
{
char tempBuffer[2000] = {0};
len = _SocketPtr->receive_from( boost::asio::buffer(tempBuffer), revEndpoint, 0, ec );
//发送端IP与端口号;
str::string revIP = revEndpoint.address().to_string();
unsigned short revPort = revEndpoint.port();
if( !ec )
{
if( len > 0 )
{
SOCKET_MESSAGE_HEADER mesHeader;
memcpy( &mesHeader, tempBuffer, sizeof(SOCKET_MESSAGE_HEADER) );
switch( mesHeader.uhMessageCode )
{
case 2:
{
DateStruct::DS_STATE msg;
memcpy( &msg, tempBuffer+sizeof(SOCKET_MESSAGE_HEADER), sizeof(DateStruct::DS_STATE) );
//得到发送过来的消息体msg;
//--TODO:
//--TODO:
break;
}
}
}
}
}
}
//由于不同操作系统之间的字节对齐方式不同,故网络传输数据时,要设置字节对齐方式
//设置变量以n字节对齐方式,指定结构体、联合以及类成员
#pragma pack(push) //保持对齐状态
#pragma pack(1) //设定为1字节对齐
#ifndef _MESSAGE_HEADER
#define _MESSAGE_HEADER
typedef struct _MESSAGE_HEADER
{
unsigned short int uhMessageCode; //消息类型
unsigned short int uhMessageLength; //消息长度
unsigned long int ulTimeTag; //时间
unsigned short int uhSerialNumber; //消息序号;
unsigned char ucSpare[22] //空余位
}SOCKET_MESSAGE_HEADER;
#endif
namespace DateStruct
{
struct DS_STATE
{
char cID[32]; //ID
char cState; //状态
}
}
#pragma pack(pop) //恢复对齐状态
②、使用socket发送数据
{
//消息头;
SOCKET_MESSAGE_HEADER header;
memset( &header, 0, sizeof(header) );
header.uhMessageCode = 2;
//消息体;
DateStruct::DS_STATE sendMsg;
memset( &sendMsg, 0, sizeof(DateStruct::DS_STATE) );
std::string tempID = "LHC010";
memcpy( sendMsg.cID, tempID, sizeof(std::string) );
char tempState("1");
sendMsg.cState = tempState;
//消息体长度;
int bodyLength = sizeof( DateStruct::DS_STATE );
//创建并初始化buffer用于存储 "消息头+消息体";
char * buff = new char[sizeof(header) + bodyLength];
memset( buff, 0, sizeof(header)+ bodyLegth );
//将消息头拷贝到buffer中;
memcpy( buff, &header, sizeof(header) );
//将消息体拷贝到buffer中;
memcpy( buff+sizeof(header), &sendMsg, sizeof(sendMsg) );
//将char*类型转换成char类型,因为boost-udp发送数据时,接收char类型,而非char*类型;
char sendBuff[2000] = {0};
int bLen = sizeof(SOCKET_MESSAGE_HEADER) + sizeof(DateStruct::DS_STATE);
memcpy( sendBuff, buff, bLen );
//使用socket发送数据;
boost::shared_ptr<boost::asio::ip::udp::socket> _ScoketPtr;
boost::asio::ip::udp::endpoint _SendEndPoint;
_SocketPtr.reset( new boost::asio::ip::udp::socket(_IOS) );
_SocketPtr->send_to( boost::asio::buffer(sendBuff), _SendEndPoint, 0 );
}③ 接收数据-创建UDP
void CreatUDP() { _SocketPtr.reset( new boost::asio::ip::udp::socket(_IOS) ); try { boost::asio::ip::udp::endpoint endpoint(boost::asio::ip::udp::v4(), _SendPort); _SocketPtr->open( endpoint.protocol() ); _SocketPtr->bind( endpoint ); _Thread.rest( new boost::thread(&ServerProcRecv, this) ); } catch( boost::system::system_error e ) { const char * errMsg = e.what(); } }④socket循环接收数据
void ServerProcRecv()
{
size_t len;
boost::system::error_code ec;
//对方(即 发送数据端) 的端口与IP
boost::asio::ip::udp::endpoint revEndpoint;
while(true)
{
char tempBuffer[2000] = {0};
len = _SocketPtr->receive_from( boost::asio::buffer(tempBuffer), revEndpoint, 0, ec );
//发送端IP与端口号;
str::string revIP = revEndpoint.address().to_string();
unsigned short revPort = revEndpoint.port();
if( !ec )
{
if( len > 0 )
{
SOCKET_MESSAGE_HEADER mesHeader;
memcpy( &mesHeader, tempBuffer, sizeof(SOCKET_MESSAGE_HEADER) );
switch( mesHeader.uhMessageCode )
{
case 2:
{
DateStruct::DS_STATE msg;
memcpy( &msg, tempBuffer+sizeof(SOCKET_MESSAGE_HEADER), sizeof(DateStruct::DS_STATE) );
//得到发送过来的消息体msg;
//--TODO:
//--TODO:
break;
}
}
}
}
}
}
相关文章推荐
- 网络编程的概述2 网络通信中的三要素3 InetAddress的概述和测试,IP地址:InetAddress 传输协议(网络协议)TCP 可靠协议,需要连接,效率会稍低 没有大小的限制UDP 不:
- 无线网络中传输层通信协议分析与比较(TCP与UDP)
- BOOST UDP 网络通信(1)
- Java基础—网络编程【OSI/RM TCP/IP】【网络通信三要素】【UDP传输 & TCP传输】【DNS域名解析】
- 网络编程【OSI/RM TCP/IP】【网络通信三要素】【UDP传输 & TCP传输】【DNS域名解析】
- NS3仿真wifi网络环境 - NS3中无线节点的通信传输范围如何设置?
- Java基础—网络编程【OSI/RM TCP/IP】【网络通信三要素】【UDP传输 & TCP传输】【DNS域名解析】
- 网络编程、三要素、Socket通信、UDP传输、TCP协议、服务端(二十五)
- Java使用udp传输方式进行网络通信
- Java基础—网络编程【OSI/RM TCP/IP】【网络通信三要素】【UDP传输 & TCP传输】【DNS域名解析】
- boost::asio::ip::tcp实现网络通信的小例子
- java网络编程--udp传输示例
- Java TCP/UDP网络通信编程
- java 网络编程 基于TCP ,UDP的网络传输
- Java学习之路0803(URL、基于UDP的数据包网络通信)
- socket网络通信(udp)
- 网络通信之简易聊天室(UDP实现)
- linux网络通信(TCP和UDP)
- Unity——通过byte字节流进行网络传输,如何将一个类转换为一个byte字节流,再从byte字节流转换为一个类
- socket 网络编程快速入门(二)教你编写基于UDP/TCP的服务端多线程通信