简体   繁体   English

C# - 2个客户端程序交易变量信息 - 有更好的方法吗?

[英]C# - 2 clientside programs trading variable information — is there a better way?

I am attempting to send player information from my Game to my network client to then be sent off to the server. 我试图将我的游戏中的玩家信息发送到我的网络客户端,然后被发送到服务器。

Currently the ClientNetwork -> ClientGame relationship is held with XML files. 目前ClientNetwork - > ClientGame关系是用XML文件保存的。 They read/write back and forth at very high speeds. 它们以非常高的速度来回读/写。 If you use just one XML file for this trade, one will "hog" the file at times, making a kind of lag when one cannot read because the other is viciously writing and rewriting. 如果您只使用一个XML文件进行此交易,那么有时候会“占用”该文件,当一个人无法读取时会产生一种滞后,因为另一个是恶意编写和重写。

To fix this I have 2 of each of my XML files. 为了解决这个问题,我有两个XML文件。 If it cannot read one, it will read the other. 如果它不能读取一个,它将读取另一个。 In theory they should be using both of them, since it'd be a tradeoff from one to another. 从理论上讲,他们应该同时使用它们,因为它是从一个到另一个的权衡。 Not working up to par. 没达到标准杆。

But my main problem is just the usage in general of XML is very sloppy, dozens of try-catch statements to make sure they're all happy (and my personal favorite, try catches within try catches -- WE HAVE TO GO DEEPER ) 但我的主要问题只是XML的一般用法非常草率,数十个try-catch语句以确保它们都很开心(而且我个人最喜欢的,尝试抓住尝试捕获 - 我们必须更深入

I am just curious of if there is a better way to be doing this. 我很好奇是否有更好的方法来做这件事。 I need a static point of variables that can be accessed by both client side programs. 我需要一个可由两个客户端程序访问的静态变量点。 I'm afraid someone is going to say databases... 我担心会有人说数据库......

I'd like to state for anyone who is looking into this as well and stumbled across this page that Shared Memory is awesome. 我想对任何正在研究这个问题的人说明并偶然发现共享内存非常棒。 Though I have to convert all strings to characters and then to bytes and read them one by one, in the whole it's ALOT better than dealing with things that cannot read/write the same file at the same time. 虽然我必须将所有字符串转换为字符然后转换为字节并逐个读取它们,但总体来说它比处理无法同时读取/写入同一文件的内容要好得多。 If you wish to further understand it rather than just use it, go to this link , it explains a lot of the messaging varieties and how to use them. 如果您希望进一步了解它而不是仅仅使用它,请转到此链接 ,它解释了许多消息传递类型以及如何使用它们。

Yes there is! 就在这里!

The term you are looking for is interprocess communication - communication between two processes on the same machine. 您正在寻找的术语是进程间通信 - 同一台机器上两个进程之间的通信。

There are various methods which allow two processes on the same machine to communicate with each other, including: 有多种方法允许同一台机器上的两个进程相互通信,包括:

  • Named pipes 命名管道
  • Shared memory 共享内存
  • Sockets 套接字
  • HTTP HTTP

Fortunately C# applications can simply use the WCF framework to perform IPC (interprocess communication) using one of the above, and let the WCF framework take care of the difficult bits! 幸运的是,C#应用程序可以简单地使用WCF框架来使用上面的一个来执行IPC(进程间通信),并让WCF框架处理困难的部分! Here are a couple of guides to get you started (there are many more): 以下是一些可以帮助您入门的指南(还有更多):

Also, one of the neat things about WCF is that you can also use it to communicate between different machines simply by changing the "Transport" (ie the communication method) to one which works over a network, (eg HTTP). 此外,关于WCF的一个巧妙之处在于,您还可以使用它在不同机器之间进行通信,只需将“传输”(即通信方法)更改为通过网络工作的(例如HTTP)。

If you are targetting .Net 2.0 then you should look into either .Net remoting or web services instead. 如果您的目标是.Net 2.0,那么您应该研究.Net远程处理或Web服务。

A simple TCP stream jumps out at me. 一个简单的TCP流向我跳出来。 Have the network client open a listening TCP socket, and have the game connect to the network client. 让网络客户端打开一个侦听TCP套接字,并让游戏连接到网络客户端。 You could continue to send the same XML data you're already writing, if you like. 如果愿意,您可以继续发送您已编写的相同XML数据。

I agree with the tcp/ip socket answer proposed by David. 我同意David提出的tcp / ip socket答案。 I would simply submit the data to a socket on the local pc and have the other application listen to the socket. 我只是将数据提交到本地PC上的套接字,让另一个应用程序监听套接字。 You can transmit data easily and quickly using this method and it will work no matter what version of the .net framework you are targeting. 您可以使用此方法轻松快速地传输数据,无论您要定位的.net框架版本如何,它都可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM