使用C#的非基于服务器的机器间通信 [英] Non server-based Inter-machine communication using C#

查看:90
本文介绍了使用C#的非基于服务器的机器间通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

这可能是一个愚蠢的问题,但是是否有任何简单的方法来编写可以在另一台计算机上与其自身进行通信的C#,而无需在两者之间使用服务器?

我正在写一个非常简单的纸牌游戏.我想与玩家2交流玩家1的举动,反之亦然.使用标准的客户端/服务器模型,这样做不会有任何问题.在这种情况下,我想知道是否可以仅使用客户端/客户端.

如果是这种情况,WCF的通信机制是否在这种情况下?还是回到简单的基础上,为每个客户端编写一个双向套接字并向其发送数据,会更直接吗?

在这种情况下,我是否要处理一个应用程序,其中应用程序本身既是客户端*又是服务器?

我只是度过了漫长的一天而脑子沉迷于自己吗?

任何想法都将受到欢迎:)

在SAKryukov发表以下评论后,这里有一些更多详细信息:

要求是这样的:
轮到玩家1了.玩家可以将自己的任何一张牌都翻过来.
翻牌后,或者玩家结束回合(通过单击按钮)后,应该将已翻转的卡组发送给玩家2.每个玩家看到自己的卡,而其他玩家看到的是在桌子上''-因此在每位玩家转过身之后,必须将转过的纸牌发送给另一位玩家,以便另一位玩家看到与第一位玩家相同的事物.本质上:

玩家1:选择一组纸牌进行转牌.
将该信息发送到Player2.
等待玩家2发回他们转过的纸牌或赢得游戏"命令.
玩家2:等待玩家1的卡片.
选择一组卡牌.
将该数据发送回播放器1

...恶心...

显然,一个恶意的人将能够操纵客户以发送获胜条件,将本不该被转过的卡翻过来,依此类推,但是在这种情况下,我不在乎.如果中央服务器正在协调游戏,则可以消除此问题,但此应用程序不需要此问题.

理想情况下,一旦我想出了交流信息,我便想编写游戏并将其发布到CodeProject上,因为它使用WPF来显示卡片,依此类推,这本身就是一些WPF的不错的教程正如SA指出的那样,这在很大程度上取决于要求.当您对WCF感兴趣时,如果它包含解决方案所需的要素,也许可以看一下自托管服务:
- http://msdn.microsoft.com/en-us/library/bb332338.aspx [ ^ ]
- http://msdn.microsoft.com/en-us/library/ms731758.aspx [ ^ ]


您可以编写该应用程序以使其以客户端或服务器的身份运行,并且当该应用程序的一个实例尝试连接到已运行的应用程序的另一个实例时,联系的应用程序将以服务器的身份运行,并且联系应用程序以客户端身份运行.您可能还希望使该应用程序足够智能,以不允许客户端连接到客户端实例,并且如果这样做,则被联系的客户端会自动将连接的客户端重定向到当前服务器.


不幸的是,如果没有更多细节,就不可能为您的应用程序提供确定的架构.我什至不能说是否需要服务部分,或者您可以开发点对点(P2P)游戏.

请查看可以使用哪些不同的级别以及涉及的内容: http://en.wikipedia.org/Wiki/Inversion_of_control [^ ].您的设计应取决于您如何提供控制反转(如果有的话).如果这是一个有两个或两个以上人类玩家的游戏,则将需要它.

有可能在任何层次上解决您的问题.在我看来,WCF倾向于在简单和最典型的情况下提供一种非常快速且强大的解决方案,而在需要更大灵活性(就逻辑和结构复杂性而言)的情况下,它失去了与经典"远程处理相比的优势.通常,在灵活性方面,具有System.Net.Sockets.TcpListener/System.Net.Sockets.TcpClient的TCP级别已足够.您将不需要较低的级别.同样,这取决于.

—SA


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.

解决方案

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[^]


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.


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


这篇关于使用C#的非基于服务器的机器间通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆