DLLDEMO_API BOOL __stdcall IsFreeGroup(const char *IpAddress,const int Port)
{
if (!AfxSocketInit())
{
AfxMessageBox("SOCKETS INIT FAILED!");
return FALSE;
} #define DATASIZE (2*1024)
#define DATA_MAXSIZE (4*1024)
CAsyncSocket sockInspect; char lpBuf[4+2*sizeof(DWORD)+DATA_MAXSIZE];
if(!sockInspect.Create(Port, SOCK_DGRAM, FD_READ))
return FALSE;
BOOL bMultipleApps = TRUE; // allow reuse of local port if needed
sockInspect.SetSockOpt(SO_REUSEADDR, (void*)&bMultipleApps, sizeof(BOOL), SOL_SOCKET);
CString strAddr;
strAddr.Format(IpAddress);
// Join the multicast group
ip_mreq mrMReq; // Contains IP and interface of the host group
mrMReq.imr_multiaddr.s_addr = inet_addr(strAddr); // group addr
mrMReq.imr_interface.s_addr = htons(INADDR_ANY); // use default
if(setsockopt(sockInspect.m_hSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char FAR *)&mrMReq, sizeof(mrMReq)) < 0)
{
sockInspect.Close();
return FALSE;
}
int m_nBytesOfBlock=DATASIZE;
int nLen = 2*(4+2*sizeof(DWORD)+m_nBytesOfBlock);
sockInspect.SetSockOpt(SO_SNDBUF, &nLen, sizeof(nLen));
sockInspect.SetSockOpt(SO_RCVBUF, &nLen, sizeof(nLen));
int nRet = 10035;
int nTries = 0;
while(nRet == 10035)
{
sockInspect.Receive(lpBuf, nLen, 0);
nRet = GetLastError();
nTries ++;
if(nTries > 10)
{
break;
}
if(nRet == 0)
break;
SleepEx(100, 0);
}
sockInspect.Close();
// AfxSocketTerm();
if(nRet == 10035 )
return TRUE;
else
return FALSE;
{
if (!AfxSocketInit())
{
AfxMessageBox("SOCKETS INIT FAILED!");
return FALSE;
} #define DATASIZE (2*1024)
#define DATA_MAXSIZE (4*1024)
CAsyncSocket sockInspect; char lpBuf[4+2*sizeof(DWORD)+DATA_MAXSIZE];
if(!sockInspect.Create(Port, SOCK_DGRAM, FD_READ))
return FALSE;
BOOL bMultipleApps = TRUE; // allow reuse of local port if needed
sockInspect.SetSockOpt(SO_REUSEADDR, (void*)&bMultipleApps, sizeof(BOOL), SOL_SOCKET);
CString strAddr;
strAddr.Format(IpAddress);
// Join the multicast group
ip_mreq mrMReq; // Contains IP and interface of the host group
mrMReq.imr_multiaddr.s_addr = inet_addr(strAddr); // group addr
mrMReq.imr_interface.s_addr = htons(INADDR_ANY); // use default
if(setsockopt(sockInspect.m_hSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char FAR *)&mrMReq, sizeof(mrMReq)) < 0)
{
sockInspect.Close();
return FALSE;
}
int m_nBytesOfBlock=DATASIZE;
int nLen = 2*(4+2*sizeof(DWORD)+m_nBytesOfBlock);
sockInspect.SetSockOpt(SO_SNDBUF, &nLen, sizeof(nLen));
sockInspect.SetSockOpt(SO_RCVBUF, &nLen, sizeof(nLen));
int nRet = 10035;
int nTries = 0;
while(nRet == 10035)
{
sockInspect.Receive(lpBuf, nLen, 0);
nRet = GetLastError();
nTries ++;
if(nTries > 10)
{
break;
}
if(nRet == 0)
break;
SleepEx(100, 0);
}
sockInspect.Close();
// AfxSocketTerm();
if(nRet == 10035 )
return TRUE;
else
return FALSE;
然后没有任何反应了
无论是TRUE或者FALSE都没有返回
if (IsFreeChannel("224.0.0.3",8000))
AfxMessageBox("OK");
else
AfxMessageBox("Error");
结果什么也没有退出程序再调用就可以.
AfxMessageBox("开始创建Socket!");
if(!sockInspect.Create(Port, SOCK_DGRAM, FD_READ))
{
AfxMessageBox("创建Socket失败!");
return FALSE;
}
AfxMessageBox("创建Socket成功!");
发现第二次到AfxMessageBox("开始创建Socket!");后就没有执行了,为什么?