分享

Implementing Multicast Sockets in C#

 googo 2010-07-31
Written by: dimport
Published by: thinkt4nk
Published on: 2003-06-21 07:19:46
Topic: Dot.Net
Search OSI about Dot.Net. More articles by dimport.
 viewed 50935 times  send this article  printer friendly

Digg this!

Ever wanted to broadcast to everyone but still be able to use the net? How about being able to send data only to the nodes that really requested it?Interested? well this is where the next generation of broadcast comes in -> Multicast.

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.

Comments:
Anonymous
2007-02-04 17:16:54
For multicast, also take a look at http://code.google.com/p/emcaster/. It is a free library that supports multiple transports (PGM and UDP) and regular expression based topics.
jjman
2007-03-27 17:02:23
Hi. The link to the source doesn't work.
Any chance it could be updated?
Anonymous
2007-04-10 14:43:28
the code does not work


BeginReceiveFrom throws an exception
Anonymous
2009-03-05 17:40:17
This is extremely unhelpful. Very vague code with errors. Not intuitive, no working example, no explanation for the unapparent logic used.

It's sad to see this as the first result on google.
Anonymous
2009-04-22 09:01:14
Well.. It seems the link to the source doesn't work. Any chance it could be updated? flash games


Anonymous
2009-05-20 16:41:53
very very very very very bad!!!!
Anonymous
2009-05-27 21:05:16
All works apart from the repeat recieve bit!
Anonymous
2009-10-18 18:00:47
bollicks... doesn;t work at all... what;s the point of submitting code if a simple copy paste isn't enough to make it work. if there are any assumptions, please mention them and don't expect the reader to figure them out. they want solutions. that's why they came here in the first place.
Domuk
2009-10-18 20:47:33
No it isn't, I think people typically come here to learn, rather than just copy and paste chunks of code without understanding them...
Anonymous
2009-11-25 19:31:23
It is a dangerous business to copy and paste code without understanding it.
Anonymous
2010-02-27 20:12:42
Hey you whining douche bags. Stop complaining and appreciate the walk through from the author. I haven't run his code directly but I've used all the pieces to create my own little console app to send and receive multicast messages and it took me like 10 mins to do that. If you're expecting to be able to always find pre-chewed and pre-digested solutions on the web that will always work for everyone in every situation, then you're dreaming and you should go back to school and learn more about programming and software engineering.
Anonymous
2010-03-22 14:37:44

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多