当前位置:我的异常网» 网络通信 » UDP组播,完成端口,双网卡收不到数据?帮帮忙
UDP组播,完成端口,双网卡收不到数据?帮帮忙
www.myexceptions.net 网友分享于:2013-04-13 浏览:848次
UDP组播,完成端口,双网卡收不到数据?各位大哥,帮帮忙?
各位大哥:
小弟我写了一个网络通信程序,通过完成端口在指定的网卡上接收网络上传输过来的语音数据,现在情况是:
1:各主机均配置了两张网卡,其中一张网卡由该程序使用,另一张网卡其他程序在用;
2:程序在大部分客户端上运行都正常,唯独在其中一个客户端上GetQueuedCompletionStatus该函数不返回,若该客户端把另一张网卡的网线拔掉,或者禁用掉该网卡,启动软件工作正常。在工作过程中插上另一张网卡的网线也没有问题。但是如果在程序启动前两张网卡都在工作,则GetQueuedCompletionStatus不返回。
主要代码如下:
调用Create函数的代码:
m_Receive.Create(uLocalPort,lpDstStr,0, lpBindAddr)
uLocalPort:组播端口
lpDstStr:组播地址
lpBindAddr:本机上要绑定的网卡地址
bool CUdpSocket::Create(USHORT uLocalPort,LPCTSTR lpDstAddr,USHORT uDstPort,LPCTSTR lpBindAddr)
{
if ( htons(uLocalPort) == m_stLclAddr.sin_port
&& m_stDstAddr.sin_addr.s_addr == inet_addr(lpDstAddr)
&& m_stDstAddr.sin_port == htons(uDstPort))
{
return true ;
}
/
m_stDstAddr.sin_family = AF_INET;
m_stDstAddr.sin_addr.s_addr = inet_addr(lpDstAddr);
m_stDstAddr.sin_port = htons(uDstPort);
//广播
if(strstr(lpDstAddr,"255"))
{
BOOL bEnable = TRUE ;
int nRes = setsockopt(m_socket,SOL_SOCKET,SO_BROADCAST,(const char*)&bEnable,sizeof(bEnable));
}
//
else
{
// note the 2 says how many concurrent cpu bound threads to allow thru
// this should be tunable based on the requests. CPU bound requests will
// really really honor this.
//
JoinMultiCastGroup(lpDstAddr,uDstPort);
}
/
/
//
return CreateCompleteIO();
}
bool CUdpSocket::JoinMultiCastGroup(LPCTSTR lpDstAddr, USHORT uDstPort)
{
m_stDstAddr.sin_family = AF_INET;
m_stDstAddr.sin_addr.s_addr = inet_addr(lpDstAddr);
m_stDstAddr.sin_port = htons(uDstPort);
struct ip_mreq stMreq;
stMreq.imr_multiaddr.s_addr = inet_addr(lpDstAddr);
stMreq.imr_interface.s_addr = INADDR_ANY;
int nRet = setsockopt(m_socket,
IPPROTO_IP,
IP_ADD_MEMBERSHIP,
(char *)&stMreq,
sizeof(stMreq));
if (nRet == SOCKET_ERROR) {
printf (
"setsockopt() IP_ADD_MEMBERSHIP address %s failed, Err: %d\n",
lpDstAddr, WSAGetLastError());
return false ;
}
/
return true ;
}
//
bool CUdpSocket::CreateCompleteIO()
{
m_hCompletionPort = CreateIoCompletionPort (INVALID_HANDLE_VALUE,NULL,0,1);
if (!m_hCompletionPort)
{
printf ("m_hCompletionPort Create Failed\n");
return false;
}
//Associate this socket to this I/O completion port
文章评论