Click here to Skip to main content
15,888,461 members
Articles / Programming Languages / C#

Silverlight: How to Send Message to Desktop Application

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
1 Nov 2010CPOL1 min read 26.1K   582   17   1
Very simple example showing how to send a message from Silverlight application to standalone desktop application

Summary

This is a very simple example showing how to send a message from Silverlight application to standalone desktop application.

Introduction

The example below shows how to use Eneter Messaging Framework to send text messages from Silverlight application to standalone desktop application using TCP connection.

(The framework is free and can be downloaded from http://www.eneter.net/. The online help for developers can be found here.)

When we want to use the TCP connection in Silverlight, we must be aware of the following specifics:

  • The Silverlight framework requires the policy server.
  • The Silverlight framework allows only ports of range 4502 - 4532.

Policy Server

The Policy Server is a special service listening on the port 943. The service receives '<policy-file-request/>' and responses the policy file that says who is allowed to communicate.

Silverlight automatically uses this service when it creates the TCP connection. It sends the request on the port 943 and expects the policy file. If the policy server is not there or the content of the policy file does not allow the communication, the TCP connection is not created.

Image 1

Eneter Messaging Framework wraps the low-level socket communication and provides convenient API for the TCP communication. It also provides the Policy Server for you. Therefore, the implementation is very simple. See the code below.

Desktop Application

The desktop application is responsible for starting the policy server and receiving messages. The whole implementation is here:

C#
using System;
using Eneter.Messaging.EndPoints.StringMessages;
using Eneter.Messaging.MessagingSystems.MessagingSystemBase;
using Eneter.Messaging.MessagingSystems.TcpMessagingSystem;

namespace Receiver
{
    class Program
    {
        static void Main(string[] args)
        {
            // Start policy server enabling Silverlight security to communicate via TCP.
            TcpPolicyServer aPolicyServer = new TcpPolicyServer();
            aPolicyServer.StartPolicyServer();

            // Create receiver of string messages.
            IStringMessagesFactory aStringMessageReceiverFactory = 
                    new StringMessagesFactory();
            IStringMessageReceiver aStringMessageReceiver = 
        aStringMessageReceiverFactory.CreateStringMessageReceiver();
            aStringMessageReceiver.MessageReceived += StringMessageReceived;

            // Create TCP listening channel.
            // Note: Silverlight supports only ports of range 4502 - 4532
            IMessagingSystemFactory aTcpMessagingFactory = 
                    new TcpMessagingSystemFactory();
            IInputChannel anInputChannel = 
        aTcpMessagingFactory.CreateInputChannel("127.0.0.1:4502");

            // Attach the input channel to the string message receiver 
            // and start listening.
            Console.WriteLine("Receiver is listening.");
            aStringMessageReceiver.AttachInputChannel(anInputChannel);
        }

        static void StringMessageReceived(object sender, StringMessageEventArgs e)
        {
            // Process incoming message.
            Console.WriteLine(e.Message);
        }
    }
}

Silverlight Application

The Silverlight application is responsible for sending text messages. (The communication with the policy server is invoked automatically by Silverlight before the connection is established.)
The whole implementation is very simple:

C#
using System.Windows;
using System.Windows.Controls;
using Eneter.Messaging.EndPoints.StringMessages;
using Eneter.Messaging.MessagingSystems.MessagingSystemBase;
using Eneter.Messaging.MessagingSystems.TcpMessagingSystem;

namespace SilverlightApplication
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            // Create sender of string messages.
            IStringMessagesFactory aStringMessageReceiverFactory = 
                        new StringMessagesFactory();
            myStringMessageSender = 
            aStringMessageReceiverFactory.CreateStringMessageSender();

            // Create output channel sending via TCP.
            // Note1: Silverlight supports only ports of range 4502 - 4532
            IMessagingSystemFactory aTcpMessagingFactory = 
                        new TcpMessagingSystemFactory();
            IOutputChannel anOutputChannel = 
            aTcpMessagingFactory.CreateOutputChannel("127.0.0.1:4502");

            // Attach the output channel to the string message sender
            // to be able to send messages via TCP.
            myStringMessageSender.AttachOutputChannel(anOutputChannel);
        }

        // Send message when the button is clicked.
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string aMessage = textBox1.Text;
            myStringMessageSender.SendMessage(aMessage);
        }

        private IStringMessageSender myStringMessageSender;
    }
}

I hope you found the article useful. If you have any comments or questions, feel free to ask.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Slovakia Slovakia
My programming path started in 1987 when I got my first computer Sharp MZ-800.
It came with 8 bit CPU Z80, 64Kb RAM and the tape recorder. It was a great machine. I think I still have it somewhere.
I was fascinated and I started to write first programs. Eventually I became developer and software architect. I like innovations and clean nice solutions.

Comments and Discussions

 
GeneralExcellent! Pin
Your Display Name Here2-Nov-10 2:47
Your Display Name Here2-Nov-10 2:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.