Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have 2 ethernet adapters/interfaces.

UDP socket is binding to some port say xxx. I want to send same packet to both the ethernet adapters simultaneously.

I have a problem that when I start communication , it sends it to one adapter only by default.
If I disable that adapter then it sends to other one.

Everytime it sends to one of the adapters and not all simulatanoeusly
C++
struct in_addr interface_addr;
	memset( & interface_addr, 0, sizeof(interface_addr) ); 
	interface_addr.S_un.S_addr = htonl(INADDR_ANY); 	 
	SOCKET socket1 = udpSocket->socketDescriptor();
	int r = setsockopt (socket1, IPPROTO_IP, IP_MULTICAST_IF, (const char *) &interface_addr, sizeof(interface_addr));
	DWORD er = GetLastError();


socket is created in the above said way.
Posted
Updated 9-Jan-13 3:08am
v2
Comments
Jochen Arndt 9-Jan-13 9:35am    
You must create sockets for each interface and bind them to the corresponding interface specifying the IP address.

I have not done this so far. Therefore, I can't give you code and posted this as comment rather than a solution.

1 solution

Hello,
1. You need to resolve an IP adresses of each NIC use DNS api for that.
2. After you need to create sockets and bind to each IP address from the list.
3. Once you done - you can broadcast to each socket.
Brief functionality (this from my libraries but I describe somemain methods):
IPAddressList _list = DNS::GetHostAddresses(); // get's the list of IP Addresses
for (int i = 0; i < _list.Length(); i++) // for each address
{
    if (_list[i] == IPAddress::LoopBack()) continue; // if it not 127.0.0.1
    // Create UDP socket
    Socket * pPulseSocket = new Socket(AddressFamily::Inet,SocketType::Dgram,Protocol::Udp);
    BOOL bVal = TRUE;
    setsockopt(*pPulseSocket,SOL_SOCKET,SO_REUSEADDR,(char *)&bVal, sizeof(bVal));
    IPEntry  _entry(_list[i],0);
    // And bind it to that IP
    if (pPulseSocket->Bind(_entry))
    {
        setsockopt(*pPulseSocket,SOL_SOCKET,SO_BROADCAST,(char *)&bVal, sizeof(bVal));
        m_PulseSockets.AddTail(pPulseSocket);
    }
    else
    {
        delete pPulseSocket;
    }
}

Resolve IP Addresses for given host name:
#include <WinDNS.h>
#pragma comment(lib,"Dnsapi.lib")
IPAddressList DNS::GetHostAddresses(const char * sDomain)
{
    IPAddressList _list;
    PDNS_RECORD pDnsRec;
    if (DnsQuery_A(sDomain,DnsType::A,DNSQuery::Standard,NULL,&pDnsRec,NULL) == ERROR_SUCCESS)
    {
        PDNS_RECORD pDnsStart = pDnsRec;
        while (pDnsRec)
        {
            if (pDnsRec->wType == DnsType::A)
            {
                IPAddress _ip(pDnsRec->Data.A.IpAddress,TRUE);
                _list.Add(_ip);
            }
            pDnsRec = pDnsRec->pNext;
        }
        DnsRecordListFree(pDnsStart, DnsFreeRecordList);
    }
    return _list;
}

In code above used HostName not "localhost", HostName you can retrieve also via DNS API.
Other code I think simple to understand.

Regards,
Maxim.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900