关于组播端口的问题

lwx_work 2008-11-04 10:03:25
用udp方式向一组播地址的组播端口发送数据,先有另一主机已加入该组播域,接收该数据时候用的端口要和组播端口一样吗,应该没道理吧,不过我试的时候不一样真的是没接收不到数据,小弟第一次接触这方面,请给位达人不吝赐教!
...全文
1475 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
码农7号- 2009-09-15
  • 打赏
  • 举报
回复
的确如此!
收听端口和组播的端口要一致!否则recvfrom会僵死直到天长地久!
Wenxy1 2008-11-24
  • 打赏
  • 举报
回复
多播,参考一下我的blog:
http://blog.csdn.net/wenxy1/archive/2007/03/22/1537267.aspx
tingtj 2008-11-24
  • 打赏
  • 举报
回复
关键就:
ip_mreq m_mrMReq; // Contains IP and interface of the host group

SOCKADDR_IN m_saHostGroup; // SOCKADDR structure to hold IP/Port of the Host group to send data to it


唉,结吧

www.wantsoft.com 隐形者源码交流
lwx_work 2008-11-24
  • 打赏
  • 举报
回复
难道就要这样结贴了~
lwx_work 2008-11-24
  • 打赏
  • 举报
回复
难道就要这样结贴了~
wsjdouble 2008-11-05
  • 打赏
  • 举报
回复
我这儿有些代码可以共享讨论一下
#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
// MulticastSocket.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CMulticastSocket command target

class CMulticastSocket : public CAsyncSocket
{
// Attributes
public:

// Operations
public:
CMulticastSocket();
virtual ~CMulticastSocket();

// Overrides
public:
BOOL JoinGroup(CString, UINT, UINT, BOOL);
BOOL LeaveGroup();
BOOL SendTo(const void*, int);
void SetLoopBack(BOOL);
BOOL SetTTL(UINT nTTL);
BOOL CreateSendingSocket(UINT, BOOL);
BOOL CreateReceivingSocket(LPCTSTR, UINT);
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMulticastSocket)
public:
virtual void OnReceive(int nErrorCode);
//}}AFX_VIRTUAL

// 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
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MULTICASTSOCKET_H__269E2C7F_2037_11D3_8EF3_0000C0FD25F8__INCLUDED_)
// MulticastSocket.cpp : implementation file
//

#include "stdafx.h"
#include "MulticastSocket.h"

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

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

CMulticastSocket::CMulticastSocket()
{
bForceNoLoopback = FALSE;
}

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

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

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 */
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
}
}

CAsyncSocket::OnReceive(nErrorCode);
}

BOOL CMulticastSocket::LeaveGroup()
{
if(setsockopt (m_hSocket, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char FAR *)&m_mrMReq, sizeof(m_mrMReq)) < 0)
return FALSE;

m_SendSocket.Close(); // Close sending socket
Close(); // Close receving socket
return TRUE;
}

BOOL CMulticastSocket::SendTo(const void* strMessage, int nSize)
{
if(m_SendSocket.SendTo(strMessage, nSize, (SOCKADDR*)&m_saHostGroup, sizeof(SOCKADDR), 0) == SOCKET_ERROR)
return FALSE;
else
return TRUE;
}

BOOL CMulticastSocket::JoinGroup(CString GroupIP, UINT nGroupPort, UINT nTTL, BOOL bLoopback)
{
if(!CreateReceivingSocket(GroupIP, nGroupPort)) /* Create Socket for receiving and join the host group */
return FALSE;
if(!CreateSendingSocket(nTTL, bLoopback)) /* Create Socket for sending */
return FALSE;

return TRUE;
}

18,363

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧