Hassan
Hassan

Reputation: 2843

C# Looking for 2 way communication. (WCF)

My program:

  1. Different Clients connect to the Server (User login)

  2. Then server starts to Push small data ( below 1KB ) to all of them every second (or less).

My questions:

  1. What service should I use? is WCF the right one?
  2. If WCF, then which protocol to use ? http, tcp ... ?

At first I thought WCF is the right thing to go with. I implemented a basic simple. Then suddenly I noticed this is only a Client to Server connection. and Server can't communicate the same way to client. Unless I start the service on client as well which makes it a server.

So in the end, I am asking how should I impelement this 2 way communication between server and client, considering the speed factor I mentioned and the right protocol to use.

UPDATE

Ok let me add some details. This is actually a Teacher client < > Server < > Students client communication program. Teacher draws something on a WPF's inkCanvas. and this drawing is supposed to be pushed to all students clients. That's why this data should be pushed by the server. and the communication has to be 2 way.

Upvotes: 2

Views: 2755

Answers (4)

Jake
Jake

Reputation: 11430

I have written a WCF server hosted on Windows Service using net.tcp and it definitely can push data to multiple clients. The framework does much of the listening, accepting and multiple connections for you so there's definitely a lot of benefits to yield.

The tricky part in your scenario would be sharing data from the teach client with student client. AFAIK, there is no built-in way to transfer data from one client-server connection to another.

One naive but simple way is to store the teacher data to a temp location (like a file or database and have some kind of mutex lock on it) and when it is time to transfer data to student, just read from the temp location.

Or you can change your architecture into Peer-To-Peer.

Upvotes: 0

user786981
user786981

Reputation:

I implemented exactly same functionality to communicate between two WPF application. To answer your question, Yes, in my opinion WCF is the best way to go. I guess you have already implemented One way communication using WCF. To be able to have Server to talk back to clients you need to implement Callbacks, here is a good article to start with.

Upvotes: 1

MarcF
MarcF

Reputation: 3299

One possibility is to look at using a network library. Have a look at this article on using NetworkComms.Net to create a WPF chat application. The client -> server, server -> client relationship is symmetrical as opposed to WCF so push notifications are completely straight forward. You also have achoice of communication protocols, e.g. TCP or UDP, and can easily add your own extensions for processing outgoing/incoming data.

Disclaimer - I'm a developer for this library.

Upvotes: 1

code5
code5

Reputation: 4794

You're better off not to have the Server PUSH data to it's clients but rather have the clients PULL data from the Server. Although, this is possible with duplex, it is not highly recommeneded as most ISP or server environments, will not allow outbound messages from within their firewalls for security reasons.

Here's my suggestion:

Have the client application(s) occationally pull from the server rather than have the server push to it's clients. This way, the server doesn't have to have a constant connection, because it doesn't scale and more importanly communication connections are very expensive resources from one application (i.e. Server).

Redesign your approach.

Upvotes: 0

Related Questions