|
|
|
The Multicast method uses the same idea as broadcast - connectionless messaging, to many nodes - UDP transport of datagrams.The Multicast is a standard communication method and is supported in most network implementations (FDDI, Token Ring, Ethernet, etc.) The Multicast Datagram The only allowed address range for multicast groups is Class D - 224.0.0.0 to 239.255.255.255. the first address is for communicating with routers (224.0.0.1), andall the NICs (Network Interface card) automatically joins this group. The Multicast Workflow One of the main ideas behind multicast is to reduce the load on hosts which are not interested in information, meaning that the packet coming from the network is discarded in a very early begining of the protocol stack, if possible, then on the interface card (NIC):Protocol Stack:UDPIPDevice DriverInterface Cardthis doesn't happen in broadcast, since there filtering occours only at the UDP layer.Another another advantage is the fact that routers relay multicast traffic to other routers so all the hosts in the network which Joined a specific group would recieve this data.This is where IGMP (Internet Group Managment Protocol) comes in:If the multicast is implemented on a single LAN, nothing is done with the IGMP reports, but if there are routers in the network, then they start mapping the nodes which belong togroup and start monitoring groups to make sure, there are still nodes which belong to any group.Every couple of minutes the router sends a query message to each group to see if there are still nodes in it, if there is a response from anyone in the group, then nothing is happened, and the router keeps transmitting multicast packets into the network.else, the router will throw the incoming multicast packets, since no nodes exists in the group to which the packet is directed. The IGMP Message within an IP datagram:|----------------------|--------------||IP Header (20 bytes) | IGMP Message||----------------------|--------------|Format of the fields in IGMP message:|--------------|---------------|------------|---------------------------||4-bit | 4-bit | |||IGMP | IGMP | Unused | 16-bit checksum ||Version (1) | type (1-2) | |||-----------------------------------------------------------------------||32-bit group address (IP address class D) ||-----------------------------------------------------------------------|The IGMP version is 1.The value of type=1 is a query message sent by a multicast routetr.The value of type=2 is a respone sent by a host.The group address is a class D IP address:* In a query the address is set to 0* In the report, the address is set to the multicast group address being reportedOne of the best things about Multicast packets, is the fact the you can control the amount of hops a packet is doing between routers, so that multicast packets only stay in thesame subnet / network, this field is the TTL - Time to live. Implementing The Multicast //Socket creation, regular UDP socket private Socket UDPSocket = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp); private string Target_IP = "224.10.10.10"; private int Target_Port = 31337; //socket initialization public MulticastSocket() { //nothing should go wrong in here try { //recieve data from any source IPEndPoint LocalHostIPEnd = new IPEndPoint(IPAddress.Any,Target_Port); //init Socket properties: UDPSocket.SetSocketOption(SocketOptionLevel.Udp,SocketOptionName.NoDelay,1); //allow for loopback testing UDPSocket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddress,1); //extremly important to bind the Socket before joining multicast groups UDPSocket.Bind(LocalHostIPEnd); //set multicast flags, sending flags - TimeToLive (TTL) // 0 - LAN // 1 - Single Router Hop // 2 - Two Router Hops... UDPSocket.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.MulticastTimeToLive, 0); //join multicast group UDPSocket.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.AddMembership,new MulticastOption(Target_IP)); //get in waiting mode for data - always (this doesn't halt code execution) Recieve(); } catch (Exception e) { Console.WriteLine(ex.Message + " " + ex.StackTrace); } } //client send function public void Send(string sendData) { byte[] bytesToSend = Encoding.ASCII.GetBytes(sendData); //set the target IP IPEndPoint RemoteIPEndPoint = new IPEndPoint(Target_IP,Target_Port); EndPoint RemoteEndPoint = (EndPoint)RemoteIPEndPoint; //do asynchronous send UDPSocket.BeginSendTo(bytesToSend, 0, bytesToSend.Length, SocketFlags.None,RemoteEndPoint, new AsyncCallback(SendCallback), UDPSocket); } //executes the asynchronous send private void SendCallback(IAsyncResult ar) { try { // Retrieve the socket from the state object. Socket client = (Socket) ar.AsyncState; // Complete sending the data to the remote device. int bytesSent = client.EndSendTo(ar); } catch (Exception e) { Console.WriteLine(e.ToString()); } } //initial receive function - called only once private void Recieve() { try { IPEndPoint LocalIPEndPoint = new IPEndPoint(IPAddress.Any ,Target_Port); EndPoint LocalEndPoint = (EndPoint)LocalIPEndPoint; // Create the state object. StateObject state = new StateObject(); state.workSocket = UDPSocket; // Begin receiving the data from the remote device. UDPSocket.BeginReceiveFrom(state.buffer, 0,StateObject.BufferSize, 0,ref LocalEndPoint, new AsyncCallback(ReceiveCallback), state); } catch (Exception e) { Console.WriteLine(e.ToString()); } } //executes the asynchronous receive - executed everytime data is received on the port private void ReceiveCallback( IAsyncResult ar ) { try { IPEndPoint LocalIPEndPoint = new IPEndPoint(IPAddress.Any,Target_Port); EndPoint LocalEndPoint = (EndPoint)LocalIPEndPoint; // Retrieve the state object and the client socket // from the async state object. StateObject state = (StateObject) ar.AsyncState; Socket client = state.workSocket; // Read data from the remote device. int bytesRead = client.EndReceiveFrom(ar,ref LocalEndPoint); //keep listening client.BeginReceiveFrom( state.buffer, 0, StateObject.BufferSize, 0,ref LocalEndPoint, new AsyncCallback(ReceiveCallback), state); } catch (Exception e) { Console.WriteLine(e.ToString()); } } Using the New Socket You can find the test project and all source codes here**********************Interesting Facts:1. A multicast packet is actually a UDP packet2. multicast address range is from 224.0.0.0 to 239.255.255.2553. multicast can be implemented on a simple LAN with no routers, just TCP/IP4. multicast is much better than broadcast5. 224.0.0.1 is a permanent address assisgned by the IANA, which means "all hosts on this subnet"6. 224.0.0.2 is also a permanent address which means "all routers in a this subnet"7. 224.0.1.1 is for NTP - Network Time Protocol8. 224.0.0.4 is used by DVMRP (Distance Vector Multicast Routing Protocol) - used for multicast routing ;)9. Don't use the XP Firewall option protection on the NIC - this won't allow you to get broadcast / multicast packets from the network**********************Sources:TCP/IP Ilustrated, Volume 1 - W. Richard Stevens (2000)MSDN This article was originally written by nashcontrol |
Did you like this article? There are hundreds more.
|
![]() | |||
![]() | |||
![]() | |||
![]() | |||
|
| ||
![]() | |||
|
| ||
![]() | |||
|
| ||
![]() | |||
|
| ||
![]() | |||
|
| ||
![]() | |||
|
| ||
![]() | |||
|
| ||
![]() | |||
|
| ||
![]() | |||
|
| ||
![]() | |||
|
| ||
![]() | |||
|
| ||
![]() | |||
|
| ||
![]() | |||
|
联系客服