Python 和 C 之间独立于操作系统的程序间通信

发布于 2024-09-04 19:17:59 字数 431 浏览 5 评论 0原文

我几乎不知道我在这里做什么,我以前从未做过这样的事情,但是我和一个朋友正在编写竞争性的国际象棋程序,他们需要能够相互沟通。

他将主要用 C 语言编写,我的大部分内容将用 Python 编写,我可以看到几个选项:

  • 交替写入临时文件或连续的临时文件。由于通信不会以任何方式庞大,这可以工作,但对我来说似乎是一个丑陋的解决方法,程序将必须不断检查更改/新文件,它看起来很丑陋。
  • 找到一些操作管道的方法,即我的.py| 。/他的 。这看起来有点死胡同。
  • 使用插座。但我不知道我要做什么,所以有人可以给我一些阅读材料吗?我不确定是否存在独立于操作系统、独立于语言的方法。是否必须有某种管理服务器程序来管理?
  • 使用某种 HTML 协议,这似乎有些过头了。我不介意这些程序必须在同一台机器上运行。

人们推荐什么?我可以从哪里开始阅读?

I have very little idea what I'm doing here, I've never done anything like this before, but a friend and I are writing competing chess programs and they need to be able to communicate to each other.

He'll be writing mainly in C, the bulk of mine will be in Python, and I can see a few options:

  • Alternately write to a temp file, or successive temp files. As the communication won't be in any way bulky this could work, but seems like an ugly work-around to me, the programs will have to keep checking for change/new files, it just seems ugly.
  • Find some way of manipulating pipes i.e. mine.py| ./his . This seems like a bit of a dead end.
  • Use sockets. But I don't know what I'd be doing, so could someone give me a pointer to some reading material? I'm not sure if there are OS-independent, language independent methods. Would there have to be some kind of supervisor server program to administrate?
  • Use some kind of HTML protocol, which seems like overkill. I don't mind the programs having to run on the same machine.

What do people recommend, and where can I start reading?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

混吃等死 2024-09-11 19:17:59

如果您想要并且需要真正独立于操作系统、独立于语言的进程间通信,套接字可能是最好的选择。

这也将允许两个程序跨机器通信(无需更改代码)。

如需阅读材料,请参阅Python 套接字编程方法

If you want and need truly OS independent, language independent inter process communication, sockets are probably the best option.

This will allow the two programs to communicate across machines, as well (without code changes).

For reading material, here's a Python Socket Programming How To.

善良天后 2024-09-11 19:17:59

两种可能性:

  • 使用 IP 套接字。 Python 文档中有一些示例。 (如果您只使用基本的读/写功能,其实并不难。)另一方面,C 语言中的套接字通常使用起来并不那么简单。

  • 创建第三个应用程序。它使用子进程启动这两个应用程序,并通过管道与这两个应用程序进行通信。国际象棋应用程序必须只能读取/写入标准输入/标准输出。

    这还有一个额外的好处,即该应用程序可以检查移动是否合法。这可以帮助您发现错误并保持游戏公平。

Two possibilities:

  • Use IP sockets. There are some examples in the Python docs. (Really not that hard if you just use the basic read/write stuff.) On the other hand, sockets in C are generally not that simple to use.

  • Create a third application. It launches both applications using subprocess and communicates with both applications through pipes. The chess applications must only be able to read/write to stdin/stdout.

    This has the additional benefit that this application could check if a move is legal. This helps you finding bugs and keeping the games fair.

行雁书 2024-09-11 19:17:59

您可以使用 Protobuf 作为程序间协议,并分别从文件中读取/写入轮流。

您可以每 n 秒读取一次中间文件。

一旦你完成了这项工作,你就可以转而使用套接字,其中每个程序都会启动一个服务器并等待连接。

变化应该很小,因为协议已经是 protobuf 了。因此,唯一需要更改的地方是从套接字或文件读取的位置。

无论哪种情况,您都需要一个交换协议。

编辑

哎呀我看错了,我以为是C++。

无论如何,这是对 protobuf 的 C 支持,但仍在进行中

http://code. google.com/p/protobuf-c/

You can use Protobuf as the inter-program protocol and read/write from a file each one turns.

You may read the intermediate file every n seconds.

Once you have this working, you may move to use sockets, where each program would start a server and wait for connections.

The change should be small, because the protocol would be protobuf already. So, the only place you have to change is where you either read from a socket or from a file.

In either case you'll need an interchange protocol.

edit

Ooops I misread and I thought it was C++.

Anyway, here's the C support for protobuf but is still work in progress work

http://code.google.com/p/protobuf-c/

2024-09-11 19:17:59

我想说,只需编写一个包含黑白动作的 xml 文件即可。在一个单独的文件中标记轮到谁,并确保只有轮到的程序才会写入该文件以提交轮到他们的轮次。

这是另一个小组提出的用于存储您的动作的建议 xml 格式的链接
http://www.xml.com/pub/a/ 2004/08/25/tourist.html

I would say just write an xml file that contains moves for black and white. Mark in a separate file who's turn it is and make sure only the program who's turn it is will write to that file to commit their turn.

Here is a link to a proposed xml format for storing your moves that another group came up with
http://www.xml.com/pub/a/2004/08/25/tourist.html

少女净妖师 2024-09-11 19:17:59

具有客户端/服务器模型的套接字...

基本上,您和您的朋友正在创建客户端的不同实现。

本地客户端显示游戏的可视化表示并存储棋子的状态(位置、被杀死/未被杀死)以及关于棋子可以/不能做什么的规则(可以用哪些棋子进行哪些移动以及是否董事会的状态受到检查)。

远程服务器存储有关玩家的状态(轮到谁了、获得的分数、游戏是否获胜)以及已发生的动作列表。

当你采取行动时,你的客户端会根据游戏规则验证该行动,然后向服务器发送一条消息,说:我已经采取了这一行动,轮到你了。

另一个客户端看到回合已经完成,从服务器拉出最后一步,计算移动是否发生在哪里,根据游戏规则验证该移动,并在本地重播该动作。全部完成后,现在允许用户进行下一步操作(如果游戏结束则不允许用户进行下一步操作)。

客户端/服务器游戏通信最重要的部分是向服务器发送尽可能少的数据并在服务器上存储尽可能少的状态。这样您就可以在本地或世界各地播放,几乎没有延迟。只要您的客户端与对手的客户端在同一组规则下运行,一切都应该有效。

如果你想确保没有人可以通过破解他们的客户端版本来作弊,你可以让位置和规则计算全部在服务器上完成,而只让客户端只有简单的播放机制。

套接字是最好的通信媒介的原因是:

  • 跨进程通信的限制几乎与跨节点通信一样困难
  • 所有系统都广泛支持网络
  • 则远程使用它几乎没有或没有进入障碍
  • 如果您选择网络, 强大、灵活且经过验证,

这就是许多主要系统(例如数据库)使用套接字作为网络以及本地通信介质的部分原因。

Sockets with a client/server model...

Basically, you and your friend are creating different implementations of the client.

The local client shows a visual representation of the game and stores the state of the pieces (position, killed/not-killed) and the rules about what the pieces can/can't do (which moves can be made with which pieces and whether the board's state is in check).

The remote server stores state about the players (whose turn it is, points earned, whether the game is won or not), and a listing of moves that have occurred.

When you make a move, your client validates the move against the rules of the game, then sends a message to the server that says, I've made this move, your turn.

The other client sees that a turn has been made, pulls the last move from the server, calculates whether where the movement took place, validates the move against the game rules, and replays the action locally. After that's all done, it's now allows the user to make the next move (or not if the game is over).

The most important part of client/server gaming communication is, send as little data to and store as little state as possible on the server. That way you can play it locally, or across the world with little or no latency. As long as your client is running under the same set of rules as your opponent's client everything should work.

If you want to ensure that no one can cheat by hacking their version of the client, you can make the position and rule calculations all be done on the server and just make the clients nothing but simple playback mechanisms.

The reason why sockets are the best communication medium are:

  • the limitations on cross process communication are almost as difficult as cross node communication
  • networking is widely supported on all systems
  • there's little or no barrier-of-entry to using this remotely if you choose
  • the networking is robust, flexible, and proven

That's part of the reason why many major systems like Databases uses sockets as a networking as-well-as local communication medium.

时光是把杀猪刀 2024-09-11 19:17:59

如果两个应用程序在同一台计算机上运行,​​请使用套接字并将对象序列化到 jsun。否则,请使用 Web 服务和 jsun 或 xml。您可以找到两种语言的 jsun 和 xml 解析器。

if both applications running on same computer, use socket and serialize your objects to jsun. otherwise, use web service and jsun or xml. You can find jsun and xml parser in both languages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文