// Generated message map functions
//{{AFX_MSG(CMulticastSocket)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
// Implementation
public:
char m_strBuffer[32000]; // 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
};
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CMulticastSocket, CAsyncSocket)
//{{AFX_MSG_MAP(CMulticastSocket)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CMulticastSocket member functions
BOOL CMulticastSocket::CreateReceivingSocket(LPCTSTR strGroupIP, UINT nGroupPort)
{
/* Create socket for receiving packets from multicast group */
if(!Create(nGroupPort, SOCK_DGRAM, FD_READ))
return FALSE;
BOOL bMultipleApps = TRUE; /* allow reuse of local port if needed */
SetSockOpt(SO_REUSEADDR, (void*)&bMultipleApps, sizeof(BOOL), SOL_SOCKET);
/* Fill m_saHostGroup_in for sending datagrams */
memset(&m_saHostGroup, 0, sizeof(m_saHostGroup));
m_saHostGroup.sin_family = AF_INET;
m_saHostGroup.sin_addr.s_addr = inet_addr(strGroupIP);
m_saHostGroup.sin_port = htons((USHORT)nGroupPort);
/* Join the multicast group */
m_mrMReq.imr_multiaddr.s_addr = inet_addr(strGroupIP); /* group addr */
m_mrMReq.imr_interface.s_addr = htons(INADDR_ANY); /* use default */
if(setsockopt(m_hSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char FAR *)&m_mrMReq, sizeof(m_mrMReq)) < 0)
return FALSE;
BOOL CMulticastSocket::SetTTL(UINT nTTL)
{
/* Set Time to Live to parameter TTL */
if(m_SendSocket.SetSockOpt(IP_MULTICAST_TTL, &nTTL, sizeof(int), IPPROTO_IP) == 0)
return FALSE; /* Error Setting TTL */
else
return TRUE; /* else TTL set successfully */
}
void CMulticastSocket::SetLoopBack(BOOL bLoop)
{
/* Set LOOPBACK option to TRUE OR FALSE according to IsLoop parameter */
int nLoopBack = (int)bLoop;
if(m_SendSocket.SetSockOpt(IP_MULTICAST_LOOP, &nLoopBack, sizeof(int), IPPROTO_IP) == 0)
{
if(!bLoop) /* if required to stop loopback */
{
bForceNoLoopback = TRUE; /* Internally making a note that loopback has to be disabled forcefilly */
// Get IP/Port for send socket in order to disable loopback forcefully */
char localHost[255];
gethostname(localHost, 255);
struct hostent *host = gethostbyname(localHost); /* Get local host IP */
m_strLocalIP = inet_ntoa (*(struct in_addr*)*host->h_addr_list);
CString Dummy; // Dummy string to be passed to the GetSockName function
m_SendSocket.GetSockName(Dummy, m_nLocalPort); /* Get Port Number for Sending Port */
}
}
}
void CMulticastSocket::OnReceive(int nErrorCode)
{
int nError = ReceiveFrom (m_strBuffer, 32000, m_strSendersIP, m_nSendersPort);
if(nError == SOCKET_ERROR)
AfxMessageBox("Error receiving data from the host group");
else
{
if (!bForceNoLoopback || (bForceNoLoopback && !(m_strSendersIP == m_strLocalIP && m_nSendersPort == m_nLocalPort)))
{
// 1. If loopbackback is not to be forced then interface handles the loopback itself
// 2. If you have to loopback and SOCKOPT LOOPBACK fails, no problem, interfaces loopback by default
// 3. If you have to stop loopback and SOCKOPT LOOPBACK fails, ignore messages coming from your own sending socket
// TODO : Add your code for here. The packet received is in m_strBuffer
}
}