在Java中实现客户端 - 服务器套接字通信的协议

时间:2022-08-26 19:49:12

I'm developing a multi-player turn based game for Android - a poker game. I'm almost finished with the clinet game-play. Now I need to implement the multi-player thing.

我正在开发一款适用于Android的多玩家回合制游戏 - 一款扑克游戏。我几乎完成了clinet游戏。现在我需要实现多玩家的事情。

I thought about using Web Services but I have no experience with that and I undertsand that socket-communication is faster.

我考虑过使用Web服务,但我没有经验,我不认为套接字通信速度更快。

I have some experience with writing socket-communication in Java: Using ObjectOutputStream and ObjectInputStream to exchange data (game-states and turn actions in this case).

我在使用Java编写套接字通信方面有一些经验:使用ObjectOutputStream和ObjectInputStream来交换数据(在这种情况下,游戏状态和转动动作)。

But I have a concern with this approach - the server-side is platform-dependent. If I want to have an iPhone developer create the app for iPhone, he could not do this because the server expects Java code. Am I right?

但我担心这种方法 - 服务器端依赖于平台。如果我想让iPhone开发人员为iPhone创建应用程序,他就无法做到这一点,因为服务器需要Java代码。我对吗?

And another question please: Can I implement normal-socket communication and then later easily change it to secure-socket communication? It's just that I need to learn how to use SSL and I don't want to go into it right now.

还有一个问题:我可以实现普通套接字通信,然后轻松将其更改为安全套接字通信吗?这只是我需要学习如何使用SSL而我现在不想进入它。

Thanks!

1 个解决方案

#1


0  

This is an old question but relevant for me and hopefully others so here goes. I'm currently working on a project that involves multiple computers on different platforms using Java sockets. I'm using sockets because eventually I can use any language as long as I adhere to the protocol I wrote. I too have some predefined integer constants that control what data is expected in what order. For example, before I start some particular function, I send a particular integer, followed by the expected data. Anything that doesn't come in in the expected order results in an error. So as long as the language/runtime offers the ability to write integers and strings over a TCP socket, it should work. I think that covers the vast majority of major programming languages.

这是一个古老的问题,但对我来说很有意义,希望其他人也这样。我目前正在开发一个项目,该项目涉及使用Java套接字在不同平台上的多台计算机。我正在使用套接字因为最终我可以使用任何语言,只要我遵守我写的协议。我也有一些预定义的整数常量来控制按什么顺序预期的数据。例如,在我启动某个特定函数之前,我发送一个特定的整数,然后是预期的数据。任何未按预期顺序进入的内容都会导致错误。因此,只要语言/运行时提供了通过TCP套接字写入整数和字符串的能力,它就可以工作。我认为这涵盖了绝大多数主要的编程语言。

As for SSL sockets, I'm doing that also :) I started out using non-SSL sockets and then refactored to make it work with SSL sockets. I was surprised at how easy it is. I would recommend this git repo for sample code: https://github.com/jawi/ssl-socket-demo.

至于SSL套接字,我也是这样做:)我开始使用非SSL套接字,然后重构以使其与SSL套接字一起使用。我很惊讶它是多么容易。我建议使用这个git repo来获取示例代码:https://github.com/jawi/ssl-socket-demo。

Eventually I plan to have the user who uses my app generate the key store and self-signed certificate locally when they first run the app as part of its initial setup. As long as they import that same key store into the app on every other node that they use in their network, they'll be able to talk to each other. This will ensure that the communication is encrypted in a way that is unique to their network. Of course this will require that they keep the key store file securely stored in their local file systems :)

最终,我计划让使用我的应用程序的用户在首次运行应用程序时在本地生成密钥库和自签名证书,作为其初始设置的一部分。只要他们将相同的密钥库导入到他们在网络中使用的每个其他节点上的应用程序中,他们就能够相互通信。这将确保以对其网络唯一的方式加密通信。当然,这将要求他们将密钥库文件安全地存储在他们的本地文件系统中:)

#1


0  

This is an old question but relevant for me and hopefully others so here goes. I'm currently working on a project that involves multiple computers on different platforms using Java sockets. I'm using sockets because eventually I can use any language as long as I adhere to the protocol I wrote. I too have some predefined integer constants that control what data is expected in what order. For example, before I start some particular function, I send a particular integer, followed by the expected data. Anything that doesn't come in in the expected order results in an error. So as long as the language/runtime offers the ability to write integers and strings over a TCP socket, it should work. I think that covers the vast majority of major programming languages.

这是一个古老的问题,但对我来说很有意义,希望其他人也这样。我目前正在开发一个项目,该项目涉及使用Java套接字在不同平台上的多台计算机。我正在使用套接字因为最终我可以使用任何语言,只要我遵守我写的协议。我也有一些预定义的整数常量来控制按什么顺序预期的数据。例如,在我启动某个特定函数之前,我发送一个特定的整数,然后是预期的数据。任何未按预期顺序进入的内容都会导致错误。因此,只要语言/运行时提供了通过TCP套接字写入整数和字符串的能力,它就可以工作。我认为这涵盖了绝大多数主要的编程语言。

As for SSL sockets, I'm doing that also :) I started out using non-SSL sockets and then refactored to make it work with SSL sockets. I was surprised at how easy it is. I would recommend this git repo for sample code: https://github.com/jawi/ssl-socket-demo.

至于SSL套接字,我也是这样做:)我开始使用非SSL套接字,然后重构以使其与SSL套接字一起使用。我很惊讶它是多么容易。我建议使用这个git repo来获取示例代码:https://github.com/jawi/ssl-socket-demo。

Eventually I plan to have the user who uses my app generate the key store and self-signed certificate locally when they first run the app as part of its initial setup. As long as they import that same key store into the app on every other node that they use in their network, they'll be able to talk to each other. This will ensure that the communication is encrypted in a way that is unique to their network. Of course this will require that they keep the key store file securely stored in their local file systems :)

最终,我计划让使用我的应用程序的用户在首次运行应用程序时在本地生成密钥库和自签名证书,作为其初始设置的一部分。只要他们将相同的密钥库导入到他们在网络中使用的每个其他节点上的应用程序中,他们就能够相互通信。这将确保以对其网络唯一的方式加密通信。当然,这将要求他们将密钥库文件安全地存储在他们的本地文件系统中:)