Posts Tagged ‘ UDP Adapter ’

Raw UDP Adapter – Listener

My previous blog I showed how easy it is to create a UDP broadcast adapter.  The problem with using the soap.udp adapter is that the message is wrapped in a soap envelope.  I tried various way in vain to get around the soap envelope so that I can broadcast or receive messages without the extra fat.  Although for most part it soap envelope would not matter, but just for kicks, I wanted to receive data without soap, I decided to write an adapter.

I followed this tutorial (echo adapter) and made the following changes to include socket connections.  On initialization, I create and bind the socket to the ip and port

listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
localEndPoint = new IPEndPoint(IPAddress.Parse(RemoteAddress), RemotePort);
listener.Bind(localEndPoint);
endpoint = (EndPoint) localEndPoint;

On background worker, I open the connection and start listening.

if (IsConnected)
{
   ReceiveStarted = true;
   while (!StopReciving)
   {
      byte[] data = new byte[1024];
      int receive = listener.ReceiveFrom(data, ref endpoint);
      var receivedData = Encoding.ASCII.GetString(data, 0, receive);
      lock (inboundQueueSynchronizationLock)
      {
          Message requestMessage = Message.CreateMessage(MessageVersion.Default, "UDP/Data",
                                                                       receivedData);
          InboundQueue.Enqueue(requestMessage);
       }
    }
}

And basically thats all. Download the full code here.  The zip file includes the code and projects for setup and custom action.

Advertisements

UDP Broadcast using BizTalk Server 2010

Recently we had talked about developing an adapter to broadcast data over UDP.  The idea was to develop such an adapter that can be used in private equity firms to receive price data over multicast.

Yesterday I had a chance to try out Dot Net 4.5 (currently in beta) which natively supports UDPBindings.  I never realized broadcasting data over UDP would be such a simple task that I could implemented a send port in a matter of minutes with 0 coding. Well 0 coding to set the port up but may be a few lines of code for the UDP receiver.

Setting up a UDP Send port

In your BizTalk application, create a send port of WCF-Custom Type.  In the General tab, set the End Point Address URI to be

soap.udp://<multicast_address>:<port>

Image

On the Binding Tab, Select the Binding Type to be “udpBinding”.  Leave the defaults as is

Image

Set the other required files and maps on your port and that’s it.  You can now broadcast messages over UDP.

Client Code

Here is the client code that I used to test my UDP Transmission.

            const int GROUP_PORT = 15000;
            //const string IP_ADDRESS = "192.168.56.1";
            const string IP_ADDRESS = "192.168.0.110";
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse(IP_ADDRESS), GROUP_PORT);
            listener.Bind(localEndPoint);
            EndPoint ep = (EndPoint)localEndPoint;
            Console.WriteLine("Ready to receive…");
            byte[] data = new byte[1024];
            int recv = listener.ReceiveFrom(data, ref ep);
            string stringData = Encoding.ASCII.GetString(data, 0, recv);
            Console.WriteLine("received: {0} from: {1}", stringData, ep.ToString());
            Console.ReadLine();
            listener.Close();

Its a pure socket based connection and works like a charm.

In my BizTalk application, my UDP Broadcast port was subscribing to a file receive port.  For both ports the pipeline was simple pass through.  I dropped in a simple xml file with data:

<?xml version="1.0" encoding="utf-8"?>
<data>WORKING PARTY</data>

And this is what I received on my client console application:

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing"><s:Header><a:MessageID>urn:uuid:9552802b-0168-431f-b694-eff5fed86a47</a:MessageID><a:To s:mustUnderstand="1">soap.udp://192.168.0.110:15000/</a:To></s:Header><s:Body><data>WORKING PARTY</data></s:Body></s:Envelope>