套接字交互(java) [英] Sockets interaction (java)

查看:131
本文介绍了套接字交互(java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发实时多人游戏,我想在客户端和服务器套接字之间创建快速,优化的界面。

I'm developing the real-time multiplayer game and I want to create fast and optimized interface between client and server sockets.

客户端将向服务器发送下一个信息via socket:

Client will send to server next information via socket:


  1. 授权信息,如登录名,密码;

  1. authorization information like login, password;
  2. simple commands like:


  • moveToPoint(coords)

  • shootInDirection(coords)

  • dropItem(item,coords)etc ...

请求)必须由服务器处理并传输到其他客户端;

These commands(requests) have to be handled by server and transmitted to other clients;

主要问题是:如何设计客户端将以高频率发送的命令的结构? (json,string或numbers)
我想,有两个因素可以影响速度和性能:

The main question is: How to design the structure of commands which client will send with a high frequency? (json, string or numbers) I think, there are 2 factors that can affect speed and performance:


  • 命令长度
  • command length (shorter -> faster)
  • complexity of parsing

我应该以何种方式解析这些命令? (其中一个java库或简单拆分)?

In which way should I parse these commands? (one of the java libraries or simple split)?

如何在流行的多人游戏

推荐答案

您可以给我一些建议或文章吗? >

至于客户端/服务器通信,我建议您查看 Netty ,它似乎满足

As far as the client/server communicating, I would recommend taking a look at Netty, it seems to meet your requirements.


  • 它具有完整的SSL / TLS支持

  • 处理线程池

  • 资源消耗低

  • ...以及更多

  • It has complete SSL/TLS support
  • Handles things like thread pools for you
  • Low resource consumption
  • ...And lots more

对于初学者,我建议您查看示例来查看如何发送字符串,尽管在您的情况下,最好发送 POJOs ,正如文章解释的 P lain O ld J ava O 对象,可能包含位置字段等。

For starters, I would recommend checking out the examples to see how to do things such as sending strings, though in your case it might be a better idea to send POJOs, which as the article explains are Plain Old Java Objects, perhaps containing fields for location, etc.

这篇关于套接字交互(java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆