C#Udp組播


using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Net;  
using System.Net.Sockets;  
using System.Threading;  
  
namespace Test  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            UdpClient client = new UdpClient(5566);  
            client.JoinMulticastGroup(IPAddress.Parse("234.5.6.7"));  
            IPEndPoint multicast = new IPEndPoint(IPAddress.Parse("234.5.6.7"), 7788);  
            byte[] buf = Encoding.Default.GetBytes("Hello from multicast");  
            Thread t = new Thread(new ThreadStart(RecvThread));  
            t.IsBackground = true;  
            t.Start();  
            while (true)  
            {  
                client.Send(buf, buf.Length, multicast);  
                Thread.Sleep(1000);  
            }  
        }  
  
        static void RecvThread()  
        {  
            UdpClient client = new UdpClient(7788);  
            client.JoinMulticastGroup(IPAddress.Parse("234.5.6.7"));  
            IPEndPoint multicast = new IPEndPoint(IPAddress.Parse("234.5.6.7"), 5566);  
            while (true)  
            {  
                byte[] buf = client.Receive(ref multicast);  
                string msg = Encoding.Default.GetString(buf);  
                Console.WriteLine(msg);  
            }  
        }  
    }  
}  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM