简体   繁体   English

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

[英]Implementing a protocol for a client-server socket communication in Java

I'm developing a multi-player turn based game for Android - a poker game. 我正在为Android开发基于多人回合的游戏-扑克游戏。 I'm almost finished with the clinet game-play. 我几乎完成了clinet游戏。 Now I need to implement the multi-player thing. 现在,我需要实现多人游戏。

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. 如果我想让iPhone开发人员为iPhone创建应用程序,他将无法执行此操作,因为服务器需要Java代码。 Am I right? 我对吗?

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! 谢谢!

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. 我目前正在一个项目中,该项目涉及使用Java套接字在不同平台上的多台计算机。 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. 因此,只要语言/运行时提供通过TCP套接字写入整数和字符串的功能,它就应该起作用。 I think that covers the vast majority of major programming languages. 我认为这涵盖了绝大多数主要的编程语言。

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. 至于SSL套接字,我也在这样做:)我开始使用非SSL套接字,然后进行重构以使其可用于SSL套接字。 I was surprised at how easy it is. 我很惊讶它是如此的容易。 I would recommend this git repo for sample code: https://github.com/jawi/ssl-socket-demo . 我会推荐此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 :) 当然,这将要求他们将密钥存储文件安全地存储在本地文件系统中:)

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

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