Results 1 to 21 of 21

Thread: UDP Broadcasting

Threaded View

  1. #8
    Lively Member
    Join Date
    Apr 2010
    Posts
    121

    Re: UDP Broadcasting

    C# Socket Multicasting

    The Socket class supports IP multicasting by using the SetSocketOption() method of the socket, but in a different way than with broadcasting.
    There are two socket options that can be used for multicasting:
    · Adding the socket to a multicast group
    · Removing the socket from a multicast group

    Both of these options are defined by IP-level Socket option names, AddMembership and DropMembership. The value parameter of the SetSocketOption() method is not a normal data value. Instead, it uses the
    MulticastOption class.

    The MulticastOption class defines the multicast group that the socket will be added to or removed from. These two constructors are used for the MulticastOption class:

    Code:
    MulticastOption(IPAddress)
    MulticastOption(IPAddress,IPAddress)
    The firsts constructor format defines the IP multicast address used in the SetSocketOption. The default behavior of this constructor is to allow all interfaces on the system to be affected by the SetSocketOption() method. If you want to limit the action to an individual network interface on the system, the second constructor allows specification of an
    IPAddress value to represent the interface.

    For example, if you wanted to add a socket to the multicast group 224.100.0.1, you would use the following statement:

    Code:
    sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
    new MulticastOption(IPAddress.Parse("224.100.0.1"));
    This sets the socket to join the multicast group for all interfaces on the system.

    Receiving Multicast Packets

    The MultiRecv sets a socket to receive multicast packets destined for the 224.100.0.1 multicast group.

    CSharp Code:
    1. using System;
    2. using System.Net;
    3. using System.Net.Sockets;
    4. using System.Text;
    5. class MultiRecv
    6. {
    7. public static void Main()
    8. {
    9. Socket sock = new Socket(AddressFamily.InterNetwork,
    10. SocketType.Dgram, ProtocolType.Udp);
    11. Console.WriteLine("Ready to receive ");
    12. IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
    13. EndPoint ep = (EndPoint)iep;
    14. sock.Bind(iep);
    15. sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption IPAddress.Parse("224.100.0.1")));
    16. byte[] data = new byte[1024];
    17. int recv = sock.ReceiveFrom(data, ref ep);
    18. string stringData = Encoding.ASCII.GetString(data, 0, recv);
    19. Console.WriteLine("received: {0} from: {1}", stringData, ep.ToString());
    20. sock.Close();
    21. }
    22. }

    The MultiRecv program creates a UDP socket in the normal way, using the standard Socket class methods. After being added to the multicast group, the socket blocks on a ReceiveFrom() method call, waiting for data to arrive.

    Some Cautions about Multicast Sockets
    There are two very important points to remember about using multicast sockets:

    First, the SetSocketOption() method call must be made after the Bind() method call for the socket. This enables the multicast group to be set for a specific IPEndPoint address on the socket.

    Second, once the socket has been added to a multicast group, the ReceiveFrom() method will accept packets destined for the following:

    · The IPEndPoint address and port specified in the Bind() method
    · The multicast group IP address specified in the MulticastOption constructor and the port specified in the IPEndPoint object
    · Broadcast packets for the specified IPEndPoint port

    Many novice network programmers forget the second item, especially. When a ReceiveFrom() method is performed, you are not guaranteed to receive packets destined just for the multicast group. So be careful of extraneous packets sent from other sources. You will not be able to easily distinguish those packets.

    Sending Multicast Packets

    Sending multicast packets is easy. Nothing special must be done to send the packets to members in the multicast
    group; you just specify the multicast group IP address as the destination address of the packet.

    CSharp Code:
    1. using System;
    2. using System.Net;
    3. using System.Net.Sockets;
    4. using System.Text;
    5. class MultiSend
    6. {
    7. public static void Main()
    8. {
    9. Socket server = new Socket(AddressFamily.InterNetwork,
    10. SocketType.Dgram, ProtocolType.Udp);
    11. IPEndPoint iep = new IPEndPoint(IPAddress.Parse("224.100.0.1"), 9050);
    12. byte[] data = Encoding.ASCII.GetBytes("This is a test message");
    13. server.SendTo(data, iep);
    14. server.Close();
    15. }
    16. }
    Last edited by jduncanator; Jan 27th, 2012 at 06:04 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width