0

I must implement an asynchronous communication between client and server, I made a test application that does it through delegates (where the client subscribes to server’s instantiated delegate, that invokes every x seconds). Server and client are running on the same solution, and now I want to put the server on a VM and keep the communication. Is it doable through Delegates? If so, how?

Delegate:

public delegate void ClientStartedEventHandler(String s);

Client:

private void OnStartClick(object sender, EventArgs e)
{
      Server.ClientStarted += ClientStarted;
      Server.start();
}

private void ClientStarted(String s)
{
      MessageBox.Show(s);
}

Server:

public static class Server
    {
        public static event ClientStartedEventHandler ClientStarted;

        public static void start()
        {
            Timer myTimer = new System.Timers.Timer();
            myTimer.Elapsed += new ElapsedEventHandler(exe);
            myTimer.Interval = 2000;    
            myTimer.Enabled = true;
        }

        private static void exe(object source, ElapsedEventArgs e)
        {
            String strNow = DateTime.Now.ToString();
            if (ClientStarted != null)
            {
                ClientStarted.Invoke(strNow);
            }
        }
    }
  • 1
    No, you can't. Take a look at various Inter process communication(IPC) methods available. Named pipes, Sockets, Message queues, WCF, etc. WCF is recommended. Please do a little research on this, you'll find it. – Sriram Sakthivel Oct 16 '14 at 18:22
  • No. The client and server would have to [ideally] be different projects. Right now you're probably adding a reference to your server project. What this means is that the client always uses a local copy of the server's dll and not the one that actually resides on the server. As suggested above, you need to use an IPC mechanism to connect the client to the server. As for the callback, WCF offers what is known as "Duplex Services", but take that with a grain of salt since it has its own issues. – Arian Motamedi Oct 16 '14 at 18:35
  • [SignalR](https://code.msdn.microsoft.com/windowsdesktop/Using-SignalR-in-WinForms-f1ec847b) – L.B Oct 16 '14 at 18:38

0 Answers0