Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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
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 9-Jan-13 2:46am
Edited 9-Jan-13 3:08am
v2
Comments
Jochen Arndt at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 210
1 Richard MacCutchan 100
2 kbrandwijk 100
3 ProgramFOX 80
4 Mukesh Bhagat 65
0 Sergey Alexandrovich Kryukov 9,050
1 OriginalGriff 8,151
2 CPallini 2,613
3 Richard MacCutchan 2,221
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100