C# UDP

//简单例子
http://blog.sina.com.cn/s/blog_66dc2c130100kcnc.html
//组播 - 单播
https://blog.csdn.net/kenjianqi1647/article/details/85679938
//详细例子
https://www.codeproject.com/Articles/1705/IP-Multicasting-in-C

udp
224.0.0.1 子网上的所有系统
224.0.0.2 子网上的所有路由器
224.0.0.12 dhcp服务器
224.0.1.1 ntp
224.0.1.24 wins服务器

//多网卡时 切换网卡发送广播

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace udpTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //网卡1
            IPEndPoint ipe= new IPEndPoint(IPAddress.Parse("192.168.1.65"),5212);
            //网卡2
            IPEndPoint ipe1 = new IPEndPoint(IPAddress.Parse("192.168.1.67"), 5213);

            //目标服务器
              IPEndPoint ipe2= new IPEndPoint(IPAddress.Parse("192.168.1.17"),5000);

            //用网卡1发
                UdpClient udp = new UdpClient(ipe);
                byte[] by = { 0x01, 0x02 };
                udp.Send(by, by.Length,ipe2);

                //用网卡2发

                UdpClient udp1 = new UdpClient(ipe1);
                udp1.Send(by, by.Length, ipe2);    
      
           
        }
    }
}

//获取网卡IPV4

            //获取说有网卡信息
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in nics)
            {
                //判断是否为以太网卡
                //Wireless80211         无线网卡    Ppp     宽带连接
                //Ethernet              以太网卡   
                //这里篇幅有限贴几个常用的,其他的返回值大家就自己百度吧!
                if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                {
                    //获取以太网卡网络接口信息
                    IPInterfaceProperties ip = adapter.GetIPProperties();
                    //获取单播地址集
                    UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses;
                    foreach (UnicastIPAddressInformation ipadd in ipCollection)
                    {
                        //InterNetwork    IPV4地址      InterNetworkV6        IPV6地址
                        //Max            MAX 位址
                        if (ipadd.Address.AddressFamily == AddressFamily.InterNetwork) {
                            //判断是否为ipv4
                            string ipv4 = ipadd.Address.ToString();//获取ip
                            Console.WriteLine(ipv4);
                        }
                    }
                }
            }

//获取所有网卡 IPV4地址 [推荐]


        public static string[] GetNetWorkInfo()
        {
            List netWorkList = new List();
            NetworkInterface[] NetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();  //获取所有的网络接口
            foreach (NetworkInterface NetworkIntf in NetworkInterfaces)                         //针对每张网卡
            {
                IPInterfaceProperties IPInterfaceProperties = NetworkIntf.GetIPProperties();    //获取描述此网络接口的配置的对象
                UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = IPInterfaceProperties.UnicastAddresses;//获取分配给此接口的单播地址
                foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection) //针对每个IP
                {
                    if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)//IPv4
                    {
                        string IP = UnicastIPAddressInformation.Address.ToString();
                        if (IP != "127.0.0.1")//不是本地IP
                        {
                            if (NetworkIntf.OperationalStatus == OperationalStatus.Up)//网卡已连接
                            {
                                //cNetWorkInfo info = new cNetWorkInfo();
                                //info.mIp = IP;
                                //info.mDiscription = NetworkIntf.Description;
                                //netWorkList.Add(info);
                                Console.WriteLine("mIp={0} mDiscription={1} ", IP, NetworkIntf.Description);
                            }
                        }
                    }
                }
            }
            return netWorkList.ToArray();
        }

你可能感兴趣的:(C# UDP)