组播源码测试结论:
无论是组播还是广播,只能在同一个网段内进行,或者说需要路由器的支持,目前国内多数路由器都不支持,因此,测试结论就是,只要经过路由器才能相互访问的两台主机,基本上就不要使用这种传输技术了。
下面是C#测试源码,在局域网内通过,可以传输数据,单机上更是没有问题。但是,经过路由器以后就不一定了。
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
// This is the listener example that shows how to use the MulticastOption class.
// In particular, it shows how to use the MulticastOption(IPAddress, IPAddress)
// constructor, which you need to use if you have a host with more than one
// network card.
// The first parameter specifies the multicast group address, and the second
// specifies the local address of the network card you want to use for the data
// exchange.
// You must run this program in conjunction with the sender program as
// follows:
// Open a console window and run the listener from the command line.
// In another console window run the sender. In both cases you must specify
// the local IPAddress to use. To obtain this address run the ipconfig command
// from the command line.
//
namespace Mssc.TransportProtocols.Utilities
{
public class TestMulticastOption
{
private static IPAddress mcastAddress;
private static int mcastPort;
private static Socket mcastSocket;
private static MulticastOption mcastOption;
private static void MulticastOptionProperties()
{
Console.WriteLine("Current multicast group is: " + mcastOption.Group);
Console.WriteLine("Current multicast local address is: " + mcastOption.LocalAddress);
}
private static void StartMulticast()
{
try
{
mcastSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp);
Console.Write("Enter the local IP address: ");
IPAddress localIPAddr = IPAddress.Parse(Console.ReadLine());
//IPAddress localIP = IPAddress.Any;
EndPoint localEP = (EndPoint)new IPEndPoint(localIPAddr, mcastPort);
mcastSocket.Bind(localEP);
// Define a MulticastOption object specifying the multicast group
// address and the local IPAddress.
// The multicast group address is the same as the address used by the server.
mcastOption = new MulticastOption(mcastAddress, localIPAddr);
mcastSocket.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.AddMembership,
mcastOption);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void ReceiveBroadcastMessages()
{
bool done = false;
byte[] bytes = new Byte[100];
IPEndPoint groupEP = new IPEndPoint(mcastAddress, mcastPort);
EndPoint remoteEP = (EndPoint)new IPEndPoint(IPAddress.Any, 0);
try
{
while (!done)
{
Console.WriteLine("Waiting for multicast packets.......");
Console.WriteLine("Enter ^C to terminate.");
mcastSocket.ReceiveFrom(bytes, ref remoteEP);
Console.WriteLine("Received broadcast from {0} :\n {1}\n",
groupEP.ToString(),
Encoding.ASCII.GetString(bytes, 0, bytes.Length));
}
mcastSocket.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static void Main(String[] args)
{
// Initialize the multicast address group and multicast port.
// Both address and port are selected from the allowed sets as
// defined in the related RFC documents. These are the same
// as the values used by the sender.
mcastAddress = IPAddress.Parse("224.168.100.2");
mcastPort = 11000;
// Start a multicast group.
StartMulticast();
// Display MulticastOption properties.
MulticastOptionProperties();
// Receive broadcast messages.
ReceiveBroadcastMessages();
}
}
}
using System;
using System.Net.Sockets;
using System.Net;
using System.Text;
// This sender example must be used in conjunction with the listener program.
// You must run this program as follows:
// Open a console window and run the listener from the command line.
// In another console window run the sender. In both cases you must specify
// the local IPAddress to use. To obtain this address, run the ipconfig command
// from the command line.
//
namespace Mssc.TransportProtocols.Utilities
{
class TestMulticastOption
{
static IPAddress mcastAddress;
static int mcastPort;
static Socket mcastSocket;
static string str="";
static void JoinMulticastGroup()
{
try
{
// Create a multicast socket.
mcastSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp);
// Get the local IP address used by the listener and the sender to
// exchange multicast messages.
Console.Write("\nEnter local IPAddress for sending multicast packets: ");
//IPAddress localIPAddr = IPAddress.Parse("192.168.1.200");
str = Console.ReadLine();
IPAddress localIPAddr = IPAddress.Parse(str);
// Create an IPEndPoint object.
IPEndPoint IPlocal = new IPEndPoint(localIPAddr, 0);
// Bind this endpoint to the multicast socket.
mcastSocket.Bind(IPlocal);
// Define a MulticastOption object specifying the multicast group
// address and the local IP address.
// The multicast group address is the same as the address used by the listener.
MulticastOption mcastOption;
mcastOption = new MulticastOption(mcastAddress, localIPAddr);
mcastSocket.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.AddMembership,
mcastOption);
}
catch (Exception e)
{
Console.WriteLine("\n" + e.ToString());
}
}
static void BroadcastMessage(string message)
{
IPEndPoint endPoint;
try
{
//Send multicast packets to the listener.
endPoint = new IPEndPoint(mcastAddress, mcastPort);
mcastSocket.SendTo(ASCIIEncoding.ASCII.GetBytes(message), endPoint);
Console.WriteLine("Multicast data sent.....");
}
catch (Exception e)
{
Console.WriteLine("\n" + e.ToString());
}
mcastSocket.Close();
}
static void Main(string[] args)
{
// Initialize the multicast address group and multicast port.
// Both address and port are selected from the allowed sets as
// defined in the related RFC documents. These are the same
// as the values used by the sender.
mcastAddress = IPAddress.Parse("224.168.100.2");
mcastPort = 11000;
// Join the listener multicast group.
JoinMulticastGroup();
// Broadcast the message to the listener.
BroadcastMessage(str+":Hello multicast listener.");
}
}
}