Home » Source Code » Network debugging assistant » MulticastSocket.cpp

MulticastSocket.cpp ( File view )

  • By nwpurain 2014-10-21
  • View(s):717
  • Download(s):20
  • Point(s): 3
			// MulticastSocket.cpp : implementation file
//

#include "stdafx.h"
#include "Multicast.h"
#include "MulticastSocket.h"
#include "NetAssist.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMulticastSocket

CMulticastSocket::CMulticastSocket()
{

	bForceNoLoopback = FALSE;
	bDataReceived = FALSE;		/* Variable defined for this project. Not necessarily part of CMulticastSocket */
	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);

}


void CMulticastSocket::SetCallBack(void* pClass)
{

	bForceNoLoopback = FALSE;
	bDataReceived = FALSE;		/* Variable defined for this project. Not necessarily part of CMulticastSocket */
	ptrClass=pClass;

}


CMulticastSocket::~CMulticastSocket()
{


}


// 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)
{

	if (!Socket(SOCK_DGRAM, 0))
	{

		int nResult = GetLastError();
		Close();
		WSASetLastError(nResult);
		return FALSE;
	
}

	BOOL bMultipleApps = TRUE;		/* allow reuse of local port if needed */
	int nOptionLength;
	if(!SetSockOpt(SO_REUSEADDR, (void*)&bMultipleApps, sizeof(BOOL))) 
		ShowErrorMessage();
//	if(!GetSockOpt(SO_REUSEADDR, (void FAR*)&bMultipleApps, (int FAR*)&nOptionLength)) ShowErrorMessage();
//	下面这行在Release版会出错,且没有任何提示信息,不知道什么原因。
//	if(!getsockopt(m_hSocket, SOL_SOCKET, SO_REUSEADDR, (char FAR*)&bMultipleApps, (int FAR*)&nOptionLength)) ShowErrorMessage();
	if(AsyncSelect(FD_READ)) ShowErrorMessage();
	/* 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 (!Bind(nGroupPort,NULL))
	{

		Close();
		ShowErrorMessage();
		return FALSE;
	
}
	int a = setsockopt(m_hSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char FAR *)&m_mrMReq, sizeof(m_mrMReq));
	if(a < 0)
		return FALSE;
	int   sock_reuse   =   1;
	if(setsockopt(m_hSocket, SOL_SOCKET, SO_REUSEADDR, (char   *)&sock_reuse,   sizeof(sock_reuse)) < 0)
		return FALSE;
	return TRUE;

}

BOOL CMulticastSocket::CreateSendingSocket(UINT nTTL, BOOL bLoopBack)
{

	if(!m_SendSocket.Create(0, SOCK_DGRAM, 0))		// Create an unconnected UDP socket
		return FALSE;

	if(!SetTTL(nTTL))								// Set Time to Live as specified by user
		AfxMessageBox("Warning! Error Setting TTL");

	SetLoopBack(bLoopBack);							// Enable/Disable Loopback

	unsigned   long   addr   =   inet_addr(m_strLocalIP);   

	if(setsockopt(m_SendSocket, IPPROTO_IP, IP_MULTICAST_IF, (char *)&addr, sizeof(addr)))
	{

		ShowErrorMessage();
	
}

	return TRUE;

}

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 */
			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 th
...
...
(Please download the complete source code to view)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 3

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
AcceptSocket.cpp899.00 B14-04-12|15:31
AcceptSocket.h1.24 kB15-06-14|22:31
Broadcast.cpp5.17 kB16-06-14|22:24
Broadcast.h1.45 kB16-06-14|22:24
ConnectSocket.cpp871.00 B14-04-12|15:26
ConnectSocket.h1.31 kB15-06-14|22:31
AcceptSocket.obj5.96 kB16-06-14|21:40
Broadcast.obj23.60 kB16-06-14|22:24
ConnectSocket.obj6.00 kB16-06-14|21:40
ListenSocket.obj7.41 kB16-06-14|21:40
Multicast.obj35.04 kB16-06-14|21:40
MulticastSocket.obj20.98 kB16-06-14|21:40
NetAssist.exe2.11 MB16-06-14|23:06
NetAssist.ilk2.48 MB16-06-14|23:06
NetAssist.obj12.44 kB16-06-14|22:29
NetAssist.pch5.27 MB16-06-14|21:40
NetAssist.pdb4.05 MB16-06-14|23:06
NetAssist.res10.88 kB16-06-14|22:29
NetAssistDlg.obj30.00 kB16-06-14|22:29
StdAfx.obj103.86 kB16-06-14|21:40
TCP.obj25.63 kB16-06-14|22:57
Unicast.obj22.99 kB16-06-14|23:06
vc60.idb233.00 kB15-10-14|23:04
vc60.pdb364.00 kB16-06-14|23:06
复件0.00 B17%|16-06-14
ListenSocket.cpp1.13 kB14-04-12|15:31
ListenSocket.h1.27 kB15-06-14|22:31
Multicast.cpp7.64 kB16-06-14|00:07
Multicast.h1.96 kB16-06-14|00:09
MulticastSocket.cpp6.81 kB16-06-14|00:00
MulticastSocket.h2.33 kB10-06-14|19:54
NetAssist.aps40.26 kB16-06-14|22:29
NetAssist.clw4.55 kB15-10-14|23:19
NetAssist.cpp2.15 kB15-06-14|22:26
NetAssist.dsp4.91 kB16-06-14|21:12
NetAssist.dsw541.00 B15-06-14|22:26
NetAssist.h1.33 kB15-06-14|22:26
NetAssist.ncb113.00 kB16-10-14|00:11
NetAssist.opt58.50 kB16-10-14|00:11
NetAssist.plg869.00 B16-06-14|23:06
NetAssist.rc10.41 kB16-06-14|22:29
NetAssistDlg.cpp5.98 kB16-06-14|22:29
NetAssistDlg.h1.63 kB16-06-14|22:29
ReadMe.txt3.55 kB15-06-14|22:26
NetAssist.ico1.05 kB15-06-14|22:26
NetAssist.rc2401.00 B15-06-14|22:26
resource.h3.09 kB16-06-14|22:29
StdAfx.cpp211.00 B15-06-14|22:26
StdAfx.h1.08 kB15-06-14|22:26
TCP.cpp5.92 kB16-06-14|21:09
TCP.h1.54 kB15-06-14|23:32
Unicast.cpp5.00 kB16-06-14|23:06
Unicast.h1.49 kB16-06-14|21:01
Debug0.00 B16-06-14|23:06
res0.00 B15-06-14|22:26
NetAssist06160.00 B16-10-14|00:11
...
Sponsored links

MulticastSocket.cpp (3.87 MB)

Need 3 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D