如果将两个端点连接到两个端口上的100个多播组,那么现在如果我将数据发送到一个组地址和端口comb(g1,p1),它是否也不会被其他套接字接收,因为它们也是群组。
如果没有,为什么不呢?如果是,那么下面的代码有什么问题?
#include "stdafx.h"
#include <stdio.h>
#include <winsock2.h>
void socketCreation(void)
{
unsigned int i=0;
u_int yes=1;
for(i=0;i<gConfiguration.numSockets;i++) //200
{
if ((gConfiguration.sockArray[i] = WSASocket(AF_INET,SOCK_DGRAM,IPPROTO_UDP,NULL,0,0))== INVALID_SOCKET)
{
printf("\n\t Socket Creation failed with error ::%d::", WSAGetLastError());
WSACleanup();
exit(1);
}
// Allow Multiple Sockets to use the same PORT number */
if (setsockopt(gConfiguration.sockArray[i],SOL_SOCKET,SO_REUSEADDR,(char*)&yes,sizeof(yes)) < 0)
{
printf("\n\t SetSockOpt for SO_REUSEADDR failed with error ::%d::", WSAGetLastError());
WSACleanup();
exit(1);
}
}
}
void socketBinding(void)
{
struct sockaddr_in addr;
unsigned int i=0;
unsigned int j=0;
unsigned int tempPort =0;
long localaddr = inet_addr("127.0.0.1");
int k = 0;
for(i = 0;i<gConfiguration.numPorts;i++)
{
addr.sin_port=htons(gConfiguration.ports[i]);
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=htonl(INADDR_ANY);
// bind to receive address
if (bind(gConfiguration.sockArray[i],(struct sockaddr *) &addr,sizeof(addr)) == SOCKET_ERROR)
{
printf("\n\t Bind failed with error ::%d::", WSAGetLastError());
exit(1);
}
printf("\n socket %d bind to port %d\t\n", gConfiguration.sockArray[i], gConfiguration.ports[i]);
k++;
}
}
void joinMCastGroup(void)
{
struct ip_mreq mreq = {0};
unsigned int i=0;
unsigned int j=0;
mreq.imr_interface.s_addr=htonl(INADDR_ANY);
for(i=0;i<gConfiguration.numSockets;i++)
{
//In order to be a member of multiple MGroups run a loop
for(j=0;j<gConfiguration.numAddress;j++)
{
//Select MulticastGroup
mreq.imr_multiaddr.s_addr=gConfiguration.address[j];
if (setsockopt(gConfiguration.sockArray[i],IPPROTO_IP,IP_ADD_MEMBERSHIP,(char*)&mreq,sizeof(mreq)) < 0)
{
printf("\n\t Setsocketopt IP_ADD_MEMBERSHIP failed with::%d:: on ::%d:: socket", WSAGetLastError(),j);
exit(1);
}
//printf("\n %d socket is a member of the group add = %d,\n", i, gConfiguration.address[j]);
}
}
}
void dataRecv(void)
{
int retVal=-1;
int nBytes=-1;
int addrLen=0;
char buffer[MSG_BUF_SIZE];
struct sockaddr_in addr;
fd_set readFD;
unsigned int i=0;
unsigned int tickCount =0;
gLastTickCount=GetTickCount();
long lTotalPkt = 0;
timeval timeout;
timeout.tv_sec = 10;
timeout.tv_usec = 100000;
DWORD dwWaitResult = 0;
// system("cls");
printf("\n *********************************************** ");
printf("\n Waiting for packets to come...");
printf("\n ***********************************************\n ");
addrLen=sizeof(addr);
printf("\n Total Seq no to recv = %d\n", gTotalPkt);
gSeqNo2 = 0;
dwWaitResult = WaitForSingleObject(mutex, INFINITE);
if(dwWaitResult == WAIT_OBJECT_0)
{
while(1)
{
FD_ZERO(&readFD);
for(i=0;i<gConfiguration.numSockets;i++)
FD_SET(gConfiguration.sockArray[i],&readFD);
retVal=select(FD_SETSIZE,&readFD,NULL,NULL,&timeout);
if(0<retVal)
{
for(i=0;i<gConfiguration.numSockets;i++)
{
if(FD_ISSET(gConfiguration.sockArray[i],&readFD))
{
addrLen=sizeof(addr);
if ((nBytes=recvfrom(gConfiguration.sockArray[i],buffer,sizeof(buffer),0,(struct sockaddr *) &addr,&addrLen)) < 0)
{
printf("\n\t RecvFrom Error with::%d::\n", WSAGetLastError());
exit(1);
}
//printf("\n recvd on socket no = %d, data=%s, address = %d\n", i, buffer, gConfiguration.address[i]);
gSeqNo2++;
gBytesCount++;//= nBytes;
gTotalByteReceived += gBytesCount;
}
}
//tickCount = GetTickCount();
//if((tickCount - gLastTickCount) > 1000) //Checking if it's more than 2 seconds or not
//{
}
else
{
printf("timeout\n");
}
if(gTotalPkt == gSeqNo2)
{
printf("\n %d packets recvd.\n", gSeqNo2);
ReleaseMutex(mutex);
break;
}
}
}
ReleaseMutex(mutex);
printf("\n Total packet Received= %d\n", gSeqNo2);
}
void RecvUsingThread()
{
int i = 0;
gtCount = 2;
gTotalPkt = gConfiguration.numPacket * gConfiguration.numAddress * gConfiguration.numPorts;//atol(buffer);
mutex = CreateMutex(NULL, FALSE, NULL);
if(NULL == mutex)
{
printf("\nunable to create mutex.\n.");
}
for(i = 0;i<gtCount;i++)
{
threadHandle[i] = CreateThread(NULL, 0, ( LPTHREAD_START_ROUTINE )dataRecv, NULL, 0, &threadId[i]);
if (threadHandle[i] == NULL)
{
printf("Unable to create Thread\n");
}
}
WaitForMultipleObjects(gtCount, threadHandle, TRUE, INFINITE);
for(i = 0;i<gtCount;i++)
{
CloseHandle(threadHandle[i]);
}
printf("\nTotal packet recvd = %d\n", gSeqNo2);
}
void socketOperations(void)
{
int retVal=-1;
WSADATA WsaData;
// Initialize Winsock version 2.2
if ((retVal = WSAStartup(MAKEWORD(2,2), &WsaData)) != 0)
{
// NOTE: Since Winsock failed to load we cannot use WSAGetLastError
// to determine the error code as is normally done when a Winsock
// API fails. We have to report the return status of the function.
printf("\n\t WSAStartup failed with error ::%d::", retVal);
exit(1);
}
//Creating Sockets and setting the SO_REUSEADDR option
socketCreation();
//Binding Sockets
socketBinding();
//Joining with the Multicast Group.
joinMCastGroup();
//Receiving the data on a Socket.
RecvUsingThread();
}
//100AddrOn2Ports
最佳答案
端口是最终目标,而不是多播组。
因此,除非平台是Windows,否则发送到端口100的数据包A将通过它连接到的任何组到达任何已加入的应用程序,因为WinSock提供的过滤比Unix主机还要多。
您必须使用唯一的一对多播组和UDP端口,这不足以将其视为唯一的元组。
使用226.0.0.1:500、226.0.0.2:502等,请勿使用226.0.0.1:500、226.0.0.2:500等。