Home Forums Support Simple Broadcast and Receive of Raw Bytes using UDP Reply To: Simple Broadcast and Receive of Raw Bytes using UDP

#2678
Anonymous
Inactive

Heya Don,

Many kind thanks for your support. We are glad that you would like to make use of the unmanaged connection support added in version 3.0. The API documentation for these latest version 3.0 features is a little sparse at the moment. The following code example is all you should need to send and receive unmanaged UDP broadcasts:

//Ensure we use the null serializer for unmanaged connections
SendReceiveOptions options = new SendReceiveOptions<NullSerializer>();

//Setup listening for incoming unmanaged UDP broadcasts
UDPConnectionListener listener = new UDPConnectionListener(options, ApplicationLayerProtocolStatus.Disabled, UDPOptions.None);
Connection.StartListening(listener, new IPEndPoint(IPAddress.Any, 10000));

//Add a packet handler for unmanaged connections
NetworkComms.AppendGlobalIncomingUnmanagedPacketHandler((packetHeader, connection, incomingBytes) => {
    Console.WriteLine("Received {0} bytes from {1}", incomingBytes.Length, connection.ConnectionInfo.RemoteEndPoint);
});

//Generate some test data to broadcast
byte[] dataToSend = new byte[] { 1, 2,3, 4 };

//Create an unmanaged packet manually and broadcast the test data
//In future this part of the API could potentially be improved to make it clearer
using(Packet sendPacket = new Packet("Unmanaged", dataToSend, options))
    UDPConnection.SendObject<byte[]>(sendPacket, new IPEndPoint(IPAddress.Broadcast, 10000), options, ApplicationLayerProtocolStatus.Disabled);

Console.WriteLine("Client done!");
Console.ReadKey();

If you have any questions on the above please feel free to post back.
Regards,
Marc