同一应用程序既充当客户端又充当服务器
我想在两个玩家之间构建一个网络国际象棋游戏。 当白方采取行动时,它必须将其行动发送给黑方 反之亦然。一开始我必须找到一些方法来动态确定哪个 应用程序等待请求并成为服务器。如果两个应用程序都等待,我就会遇到问题。我的代码基于 this< /a>
我该如何从那里开始?
提前致谢
I want to build a networked chess game between two players.
when white makes a move it has to send its move to black
and vice versa. At the beginning I have to find some way to dynamically determine which
application waits for requests and being the server. If both applications wait I have a problem. I based my code on this
How can I start from there?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确实需要混合客户端/服务器设计,但不使用您正在考虑的示例代码。它是同步的,使用阻塞套接字调用,在继续之前等待响应。
为了避免客户端/服务器应用程序都被阻塞等待接受彼此的连接的情况,您需要执行非阻塞、异步套接字 I/O。例如,您可以使用 Java NIO 库 http://rox-xmlrpc.sourceforge.net/niotut/
You do want a hybrid client/server design, but not using the sample code you are considering. It is synchronous, using blocking socket calls that wait for a response before proceeding.
To avoid the situation where both client/server apps are blocked waiting to accept a connection from each other, you need to do non-blocking, asynchronous socket i/o. For example, you could use the Java NIO library http://rox-xmlrpc.sourceforge.net/niotut/