Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Networked Client-Server application advice

Tags:

c#

networking

tcp

I'm trying to design an application that will allow two users over a network to play the prisoner's

dilemma game (http://en.wikipedia.org/wiki/Prisoner%27s_dilemma).

Basically, this involves:

  • Game starts (Round 1).
  • Player 1 chooses to either cooperate, or betray.
  • Player 2 chooses to either cooperate, or betray.
  • Each other's decisions are then displayed
  • Round 2 begins
  • Etc.

I've done some thinking and searching and I think the application should contain the following:

  • Server class that accepts incoming tcp/ip connections
  • Gui clients (Seperate program)
  • For each connection (maximum 2) the server will create a new ConnectedClient class. This class will contain the details of the two player's machines/identities.
  • The Server class and the ConnectedClient class will connect/subscribe events to each so they can alert one another when e.g. server instruction ready to transmit to players, or players have transmitted their inputs to the server.

I'm not sure whether the best approch is to use a single thread to do or the work, or have it multithreaded. Single threaded would obviously be easier, but I'm not sure whether it is possible for this situation - I've never made a application before requiring TCP/IP connections, and I'm not sure if you can listen for two incoming connections on one thread.

I've found the following guide online, but it seems that it opens two clients on two threads, and they communicate directly to each other - bypassing the server (which I will need to control the game logic): http://www.codeproject.com/Articles/429144/Simple-Instant-Messenger-with-SSL-Encryption-in-Cs

I'm very interested and would be grateful on any advice on how you would go about implementing the application (mainly the server class).

I hope I've explained my intentions clearly. Thanks in advance.

like image 774
nf313743 Avatar asked Nov 12 '12 15:11

nf313743


People also ask

What is client/server application in networking?

1 What is a Client/Server Application? In principle, a client/server application consists of a client program that consumes services provided by a server program. The client requests services from the server by calling functions in the server application.

What is the importance of client-server network?

The advantages of a client-server network are greater security of the network, more control e.g. network traffic passing through the network, being able to see what each computer is doing and limiting certain actions and preventing things such as viruses spreading, also the amount of data storage available to each ...

What are some examples of client-server applications?

Examples of computer applications that use the client-server model are email, network printing, and the World Wide Web.


2 Answers

First, as others have said, separate your game logic as much as you can, so the basic funcionality won't depend too much on your comunication infrastructure.

For the communication, WCF can handle the task. You can make your clients send a request to a service hosted in IIS, doing some kind of identification/authentication, and open a Duplex channel from where your service can push results and comunicate the start of new rounds.

Once one client connects, it waits for another. When it happens, it notifies the first client using the Duplex Channel callback and awaits for its choice. Then it asks the second user, awaits for its response. When it comes, it notifies the result to both and restarts the game.

Going a little bit deeper in the implementation:

You will have a service with some operations (like Register, PushDecision, more if needed). You will also define a callback interface, with the operations your service will need to push to the client (NotifyResult, RequestDecision, again, these are examples). You then create proxies for your clients that maps to your service operations and implement the callback operations in a way it expose events and raise them when the service pushs messages.

A use case:

Client A creates the proxy, calls Register on the server. The server receives the call, register the cilent and saves the callback object in a state. A duplex connection will be established. What does that mean? It means that (if you using the PollingDuplexBinding, as you probably will) from now on the proxy object in Client A will be doing long poll requests to the server, checking if there is a callback message. If there isnt, then it long polls again. If there is, it calls the method of the callback in the proxy passing the data the server has push. The callback method in the proxy will tipically raise an event, or execute a delegate, its up to you to choose.

Client B connects (calling Register), does the same as it did to A, and the server, noticing that two clients are connected, requests a response to A through its saved callback. This can happen during the processing of the B's Register call, or it can be triggered to execute in a new thread (or better, run in the ThreadPool or start a new Task) in B's register call.

Client A will receive the server callback requesting its choice. It can then notify the user and get the choice through the UI. A new call is made to the server (PushDecision, for example). The server receives Client A choice, asks B the same way. Once it has both responses, it calculates the result and pushes the outcome to the Clients.

An advantage of using Duplex Channels with PollingDuplex with WPF is that, as it uses long polling, there will be no need to use other ports than 80.

This is by no means a final implementation, is just a little guide to give you some ideas instead of just giving you some misty advices. Of course, there may be a bunch of other ways of doing that with WCF.

We can first assume that the application can handle only two users per time and then, if you want, you can scale up, making your service keep some form of state with a mapping table with locked access, as another example.

Some thoughts on WCF: There is an easy path to start developing with WCF using the Visual Studio tools (svcutil) but I don't like that approach. You don't "get to know" the WCF infrastructure well, you become tied to the verbose magic with which it generates your proxies, and you lose flexibility, especially in special scenarios, like Duplex polling that you may want to use.

The other way, that is to manually create your services and your proxies, is not that hard, though, and gets very interesting once you realize what you can do with it. Related to that I can give you one advice: do everything you can to make your proxy operations use Task-based Async Pattern (you can see the different ways to implement proxy operations here). This will make your code much cleaner and straight forward when combined with the new C# async/await keywords and your UI will be a joy to implement.

I can recommend some links to get you started. Some of them are old, but very didactic.

  • There used to be a fantastic article of WCF in this link but it seems to be now offline. Luckily, I found the content available there in a file in this link.
  • This one covers your hosting options.
  • Topics on WCF infrastructure: link
  • Topics on Duplex Services: link link link
  • Topics on Task-based Async Pattern: link link link
like image 23
Arthur Nunes Avatar answered Oct 01 '22 22:10

Arthur Nunes


My 1st advice would be to forget about TCP/IP and sockets here. You definitely can do it with that technology stack, but you would also get a lot of headache implementing all the things you want. And the reason is it too low level technology for such a class of tasks. I would go with tcp/ip and sockets only for academic interest, or if I need tremendous control over the communication, or if I have very high performance requirements.

So, my 2nd advice would be to look at WCF technology. Don't be afraid if you haven't used it before. It's not that difficult. And if you were ready to use sockets for your app, you can handle WCF definitely. For you task you can create basic communication withing 1-2 hours from scratch using any WCF tutorial.

So, I would create a server WCF service which will have some API functions containing your business logic. It can be hosted within a windows service, IIS, or even a console application. And your clients would use that WCF service, calling their functions like it's functions from another local class in your project. WCF could also help you do the events which you want (it's a little bit more advanced topic though). And you can even forget about threading here, most of the things will be working out of the box.

like image 60
Max Shmelev Avatar answered Oct 01 '22 23:10

Max Shmelev