简体   繁体   English

最小化通过UDP传输数据的最有效方法是什么?

[英]What is the most effective way to minimize data for transfer over UDP?

I'm not into bits and bytes too much, so my apologies if this question is stupid. 我没有太多的细节,所以我很抱歉这个问题很愚蠢。

I have some experience with sockets, usually for transferring small amounts of plain text. 我对套接字有一些经验,通常用于传输少量的纯文本。 This time however I have to transfer much more data that I'm able to break apart via UDP. 但是这次,我必须传输更多的数据,以便能够通过UDP进行分解。

I'm developing a game, where the server is written in Java and I want to send an array of objects to the client. 我正在开发一个游戏,其中的服务器是用Java编写的,我想将对象数组发送给客户端。 Since I'm a webdeveloper JSON is the first thing that pops up for transferring data though I highly doubt this is the most effective way in networking. 由于我是一名Web开发人员,因此JSON是传输数据时弹出的第一件事,尽管我高度怀疑这是联网中最有效的方法。

In case of JSON: 如果是JSON:

{
    "players": [
        {
            "id": "884B0EAA2C7F16BA",   // hex string
            "name": "some guy",         // string
            "x": 0,                     // double
            "y": 0,                     // double
            "xv": 0,                    // double
            "yv": 0,                    // double
            "angle": 0,                 // double
        }, {
            "id": "AB61F7C2AAE0B488",   // hex string
            "name": "some other guy",   // string
            "x": 0,                     // double
            "y": 0,                     // double
            "xv": 0,                    // double
            "yv": 0,                    // double
            "angle": 0,                 // double
        }
    ]
}

Now, I could just add a custom delimiter to split data per player and another delimiter to split data. 现在,我可以添加一个自定义分隔符以按玩家分割数据,并添加另一个分隔符以分割数据。 In my optics, one of the most effective ways would be to put the values of the player per line, and put a predefined character sequence between the values. 在我的光学系统中,最有效的方法之一是每行放置播放器的值,并在这些值之间放置预定义的字符序列。

884B0EAA2C7F16BA[|]some guy[|]0[|]0[|]0[|]0[|]0
AB61F7C2AAE0B488[|]some other guy[|]0[|]0[|]0[|]0[|]0

Though this will work for me, there are still some security risks with names and such. 尽管这对我有用,但名称等仍存在一些安全隐患。 I hardly can tell the client that a display name containing "[|]" is illegal. 我很难告诉客户包含“ [|]”的显示名称是非法的。

What is the most effective way to minimize data for transfer? 最小化传输数据的最有效方法是什么?

Any additional information about encryption will be much appreciated! 任何有关加密的其他信息将不胜感激!

Consider using Google Protocol Buffer. 考虑使用Google协议缓冲区。 You define a message (in a .proto file), and it generates code for you in different programming languages. 您定义一条消息(在.proto文件中),它会以不同的编程语言为您生成代码。

The generated code is then capable of encoding/decoding your messages without any need for you to think about how the data is encoded. 这样,生成的代码便能够对消息进行编码/解码,而无需您考虑数据的编码方式。

The encoded messages will be very small, and very fast to encode/decode. 编码的消息将非常小,并且编码/解码非常快。 Protocol buffer is really good at that. 协议缓冲区确实擅长于此。

There is some good documentation about it on the website: https://developers.google.com/protocol-buffers/ 网站上有一些很好的文档: https : //developers.google.com/protocol-buffers/

If your goal is to minimize data for transfer, you could always try a compression algorithm which is not too complicated (we don't want to spend a lot of time compressing and decompressing each packet). 如果您的目标是最大程度地减少要传输的数据,则可以始终尝试一种不太复杂的压缩算法(我们不想花很多时间来压缩和解压缩每个数据包)。 You could have a look at this question or search an external library such as this one . 你可以看看这个问题,或搜索外部库,如这一个

Unless you can know for sure the length of each field of data, you must use some kind of delimiter. 除非您可以确定每个数据字段的长度,否则必须使用某种分隔符。 Avoiding some characters use in player names is used almost in every game out there, why can't you do that? 几乎在所有游戏中都避免在玩家名称中使用某些字符,为什么不这样做呢?

If you still want to allow any character at player names, send all of them together through a TCP socket, and build a table in each client with the pairs ID-Player name, and use unique IDs given by the server in your UDP sentences. 如果您仍想在播放器名称上允许任何字符,请通过TCP套接字将所有字符一起发送,并在每个客户端中使用ID-Player名称对构建一个表,并使用服务器在UDP句子中提供的唯一ID。 In that way, you could use the delimiter safely. 这样,您可以安全地使用定界符。

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

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