PowerTCP Sockets for .NET

from $349.00
Available Platforms
<

Windows Forms UDP Multicast Code Example

See all PowerTCP Sockets for .NET Samples and Code Examples

 

The following C# example demonstrates a UDP session in a Windows Forms environment. The app joins a multicast group, sends a message, receives a response and leaves. The session is executed asynchronously, without blocking the UI.

 

private void buttonHello_Click(object sender, EventArgs e)
{
    //This application has a User-Interface, which we do not want to block.
    //Start launches the specified method on a worker thread.
    udp1.Start(sendHello, new Dart.Sockets.IPEndPoint("234.5.6.7", 59877));
}

private void sendHello(object groupEP)
{
    //Join a multicast group, send a message, receive a response, and leave.
    //The group endpoint is passed into the function.
    Dart.Sockets.IPEndPoint ep = (Dart.Sockets.IPEndPoint)groupEP;

    //Join Multicast group.
    udp1.Open(ep.Port);
    udp1.JoinMulticastGroup(ep.Address);

    //Send message.
    udp1.Send("hello world", ep);

    //Receive response.
    byte[] buffer = new byte[1024];
    udp1.Receive(buffer);

    //Leave group.
    udp1.LeaveMulticastGroup(ep.Address);
}

 

To download a trial please visit the PowerTCP Sockets for .NET product page.