Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi All,

This might be a stupid question, but is there any straightforward way to write a C# that can communicate with itself on another machine - without having a server inbetween the two?

I am writing a very simple card game. I want to communicate player 1's moves with player 2 and vice versa. I would have no problems doing this with a standard Client/Server model. In this case I am wondering whether it is possible to use just client/client.

If this is the case are the WCF mechanisms for communication in this case? Or would it be more straightforward to go back to basics and just write a two way socket for each client and send the data down them?

In this case am I dealing with an application where the application itself is both the client *and* the server?

Have I just had too long a day and braingoosed myself?

Any thoughts would be welcome on this one :)

After SAKryukov's Comment below, here are some more details:

The requirements would be this:
It is player 1's turn. Player one can turn any of his cards over.
Either immediately after turning the card, or after the player ends his turn (via a button click) the set of cards that have been turned over should be sent to Player 2. Each player sees their own and the other players cards that are 'on the table' - therefore after each players turn the turned cards must be sent to the other player so that the other player is seeing the same thing as the first player. Essentially:

Player 1: Chooses a set of cards to turn.
Sends that info to Player 2.
Waits for Player 2 to send back the cards that they turned OR the 'won game' command.
Player 2: Waits for player 1's cards.
Chooses a set of cards to turn.
Sends that data back to player 1

...ad nauseum...

Obviously a malicious individual would be able to manipulate the client to send over the win condition, turn cards that shouldn't be able to be turned and so on, but in this circumstance I don't care. If a central server was coordinating the game then this issue could be obviated but is not needed for this app.

Ideally once I've come up with the communications I'd like to write up the game and post it on the CodeProject, as it uses WPF to display the cards and so on, which in itself is a nice tutorial on some WPF techniques.
Posted
Updated 5-Apr-11 6:31am
v4
Comments
Sergey Alexandrovich Kryukov 5-Apr-11 11:07am    
The Question is not dumb at all. An example of dump question would be "can I do this and this with that and that?". You have enough sense not to ask a question containing a (wrong) answer in it. Also, too many people (and developers) fail to understand limitations of client-server.
The problem is that it is impossible to suggest one single model until you suggest requirements.
Therefore, I vote 4.
--SA

As SA pointed out this depends largely on the requirements. As you were interested in WCF, perhaps self hosted service is something you could have a look at if it contains the necessary elements for your solution:
- http://msdn.microsoft.com/en-us/library/bb332338.aspx[^]
- http://msdn.microsoft.com/en-us/library/ms731758.aspx[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Apr-11 11:31am    
Wika, I agree on self-hosted WCF; and the references are good. My 5.
However, when I did research on different architectures to use, I quickly came across the situations, where WCF flexibility was not enough or would result in unreasonably complex (and hence less reliable) customization. It's really highly oriented toward straightforward tasks.
--SA
Wendelius 5-Apr-11 14:20pm    
Yes, WCF isn't too simple in complex tasks. However depending on the requirements it may be sufficient. One good advantage over web services is event handling...
Sergey Alexandrovich Kryukov 5-Apr-11 14:35pm    
Agreed. Again, the problem should be given a spec, this is the key now.
--SA
You could write the app to run as either the client or the server, and when an instance of the app tries to connect to another instance of the app that's already running, the contacted app runs as server, and the contacting app runs as client. You'd probably also want to make the app smart enough to not allow clients to connected to a client instance, and that if it does, the contacted client would automatically redirect the connecting client to the current server.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Apr-11 11:32am    
That's true. My 5.
--SA
Unfortunately, it is impossible to give you a definitive schema for your application without much more detail on in from your side. I cannot even say if the service part is needed, or you can develop P2P (point-to-point) game.

Please see what are different possible level can be used and what's involved: Communication b/w two Windows applications on LAN.[^].

The client-server model is, strictly and technically speaking, is not the opposite to client-to-client. The limitation of client-server model is the lack of Inversion of Control, something you should understand: http://en.wikipedia.org/wiki/Inversion_of_control[^]. Your design should depend on how do you provide Inversion of Control, if any at all. If this is a game with two or more human players, you will need it.

Chances are, it it possible to solve your problem on any of the layers. In my opinion, WCF tends to provide a very fast and robust solution in simple and most typical cases, while looses its benefits over "classic" remoting where much greater flexibility is needed (in terms of logic and structural complexity). Usually, the level of TCP with System.Net.Sockets.TcpListener/System.Net.Sockets.TcpClient is quite enough in terms of flexibility; you would not need lower level. Again, it depends.

—SA
 
Share this answer
 
v2
Comments
Dave Kerr 5-Apr-11 11:37am    
SAKryukov,

I think understanding the concept of 'Inversion of Control' is crucial to this problem and other areas in general, so reading about this to get a better theoretical understanding would certainly be wise.

Also, as you mention it seems that there are mechanisms to do this in a low level way (TcpClient/TcpListener) as well as a more high-level way (self hosted services) - the requirements can determine the mechanism used, but the design should be handled after a thorough review of the requirements is made.

With these comments and the solution above I think I have plenty to go on - thank you very much for your help :)
Sergey Alexandrovich Kryukov 5-Apr-11 11:45am    
Your're absolutely right. Thank you for understanding.
Anyway, the design for your problem is something to thing about.

Thank you for accepting this Answer.
--SA
Sergey Alexandrovich Kryukov 5-Apr-11 13:45pm    
Thank you.
--SA
SwitcherSoft 5-Apr-11 13:53pm    
5+
Sergey Alexandrovich Kryukov 5-Apr-11 13:58pm    
Thank you very much.
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900