多网卡的广播

1)多网卡广播时,每个网卡都绑定一次,广播一次,才能保证网络里都能收到该广播消息

m_Socket = socket( AF_INET, SOCK_DGRAM, 0 );
 
     // set a socket option
     BOOL  bBroadcast = TRUE;
     nResult = setsockopt(    m_Socket, 
                             SOL_SOCKET,
                             SO_BROADCAST,
                             ( char *)&bBroadcast,
                             sizeof BOOL  )
                         );
m_saUdpCli.sin_family        = AF_INET;
     m_saUdpCli.sin_addr.s_addr    = inet_addr(m_szLocalHostIP); //这个IP就是我选择要发送广播包的网卡的IP
     m_saUdpCli.sin_port            = htons( UDP_UDAP_PORT );
nResult = bind( m_Socket, (SOCKADDR *)&m_saUdpCli,  sizeof (SOCKADDR_IN) );


2)判断ping一个ip

 

你可能感兴趣的:(多网卡的广播)