You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
3.3 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*---------------------------------------------------------------------------
概述UDP组播读写操作类用于收发数据。
使用环境windows操作系统
VS2008 VS2010 VS2013
应用示例:
1. 定义对象
CMulticastSocket m_multiSocket;
2. 初始化
根据需要执行函数2 或 3 或4
3. 根据需要设置接收数据回调函数
m_multiSocket.SetCallBack();
4. 通过组播发送数据
m_Serial.SendTo(buffer,10); // 举例
5. 离开组播,释放资源
m_Serial.LeaveGroup();
其它接口函数根据实际需求进行调用。
问题及解决情况记录:
问题1
解决情况:
问题2
解决情况:
......
修改记录:
版本 日期 作者 备注
V1.0 2016/12/28 王家星 在原有代码整理与简化,添加头文件注释
----------------------------------------------------------------------------*/
#if !defined(AFX_MULTICASTSOCKET_H__269E2C7F_2037_11D3_8EF3_0000C0FD25F8__INCLUDED_)
#define AFX_MULTICASTSOCKET_H__269E2C7F_2037_11D3_8EF3_0000C0FD25F8__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
#include <afxsock.h>
// 定义数据接收回调函数格式
typedef void ( CALLBACK *RevNetDataCALLBACK )(const void* buffer, int length);
class CMulticastSocket : public CAsyncSocket
{
// Operations
public:
CMulticastSocket();
virtual ~CMulticastSocket();
public:
/* 【1】
功能: 设置对网口UDP组播读入数据进行处理的回调函数
输入:
DataCallBack串口回调函数地址
输出:
返回值:无
*/
void SetCallBack( RevNetDataCALLBACK DataCallBack)
{
m_RevData = DataCallBack;
}
/* 【2】
功能: 创建接收数据Socket
输入:
1. strGroupIP组播IP
2. nGroupPort组播端口号
输出:
返回值:创建成功返回True, 否则返回False
*/
BOOL CreateReceivingSocket(LPCTSTR strGroupIP, UINT nGroupPort);
/* 【3】
功能: 创建发送数据Socket
输入:
1. nTTLTime to Live即数据最多可经过几个网关
2. bLoopBack是否接收自己发送的数据
输出:
返回值:创建成功返回True, 否则返回False
*/
BOOL CreateSendingSocket(UINT nTTL = 5, BOOL bLoopBack = FALSE);
/* 【4】
功能: 发送数据
输入:
1. strMessage指针
2. nSize长度
输出:
返回值:发送成功返回True, 否则返回False
*/
BOOL SendTo(const void* strMessage, int nSize);
/* 【5】
功能: 功能2 + 功能3
*/
BOOL JoinGroup(CString GroupIP, UINT nGroupPort, UINT nTTL = 5, BOOL bLoopback = FALSE);
/* 【6】
功能: 退出组播
输入:
输出:
返回值:退出成功返回True, 否则返回False
*/
BOOL LeaveGroup();
private:
RevNetDataCALLBACK m_RevData;
char m_strBuffer[10240]; // Receiving buffer for the packet that has arrived
SOCKADDR_IN m_saHostGroup; // SOCKADDR structure to hold IP/Port of the Host group to send data to it
ip_mreq m_mrMReq; // Contains IP and interface of the host group
UINT m_nSendersPort; // Holds Port No. of the socket from which last packet was received
CString m_strSendersIP; // Hold IP of the socket from which the last packet was received
UINT m_nLocalPort; // Ephemeral port number of the sending port
CString m_strLocalIP; // IP Address of the local host or your machine
BOOL bForceNoLoopback; // If interface does not support lopback and the service is required, the bool is set to true
CAsyncSocket m_SendSocket; // Socket for sending data to the host group
private:
void SetLoopBack(BOOL);
BOOL SetTTL(UINT nTTL);
protected:
virtual void OnReceive(int nErrorCode);
};
#endif // !defined(AFX_MULTICASTSOCKET_H__269E2C7F_2037_11D3_8EF3_0000C0FD25F8__INCLUDED_)