C#實現組播源及客戶端

Just4life發表於2014-08-11

組播源:

using System;
using System.Net;
using System.Net.Sockets;

namespace multiCastSend
{
	class send
	{
		send(string mcastGroup, string port, string ttl, string rep) 
		{
			IPAddress ip;
			try 
			{
				Console.WriteLine("MCAST Send on Group: {0} Port: {1} TTL: {2}",mcastGroup,port,ttl);
				ip=IPAddress.Parse(mcastGroup);
				
				Socket s=new Socket(AddressFamily.InterNetwork, 
								SocketType.Dgram, ProtocolType.Udp);
				
				s.SetSocketOption(SocketOptionLevel.IP, 
					SocketOptionName.AddMembership, new MulticastOption(ip));

				s.SetSocketOption(SocketOptionLevel.IP, 
					SocketOptionName.MulticastTimeToLive, int.Parse(ttl));
			
				byte[] b=new byte[10];
				for(int x=0;x<b.Length;x++) b[x]=(byte)(x+65);

				IPEndPoint ipep=new IPEndPoint(IPAddress.Parse(mcastGroup),int.Parse(port));
				
				Console.WriteLine("Connecting...");

				s.Connect(ipep);

                for(int x=0;x<int.Parse(rep);x++) {
					Console.WriteLine("Sending ABCDEFGHIJ...");
					s.Send(b,b.Length,SocketFlags.None);
                }

				Console.WriteLine("Closing Connection...");
				s.Close();
			} 
			catch(System.Exception e) { Console.Error.WriteLine(e.Message); }
		}

		static void Main(string[] args)
		{

            new send("224.5.6.7", "5000", "1", "2");
		}
	}
}

客戶端:

using System.Net.Sockets;
using System.Net;
using System;


namespace multiCastRecv 
{

	class recv 
	{

		recv(string mcastGroup, string port) 
		{
			Socket s=new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
			
			IPEndPoint ipep=new IPEndPoint(IPAddress.Any,int.Parse(port));
                        s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
			s.Bind(ipep);
			
			IPAddress ip=IPAddress.Parse(mcastGroup);

			s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip,IPAddress.Any));

			while(true) 
			{
				byte[] b=new byte[10];
                <span style="white-space:pre">		</span>Console.WriteLine("Waiting for data..");
				s.Receive(b);
				string str = System.Text.Encoding.ASCII.GetString(b,0,b.Length);
				Console.WriteLine("RX: " + str.Trim());
			}
			
<span style="white-space:pre">			</span>s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, new MulticastOption(ip,IPAddress.Any));
<span style="white-space:pre">			</span>s.Close();
		}


		public static void Main(string[] args) 
		{
			new recv("224.5.6.7","5000");
		}
	}
}


相關文章