简体   繁体   English

具有压缩功能的TCP的客户端服务器

[英]Client-Server with TCP with compression

I have a program that work like a chat. 我有一个像聊天一样工作的程序。
Client and server are connected with 2 TCP sockets, one for incoming messages another for outgoing messages. 客户端和服务器通过2个TCP套接字连接,一个用于传入消息,另一个用于传出消息。
Sometimes the messages can be very big (ex. 2 MByte of text) so I want to compress them before sending over the channel. 有时消息可能很大(例如2 MB的文本),因此我想在通过通道发送之前对其进行压缩。
The problem is that I don't know how to find the start and end of compressed message. 问题是我不知道如何找到压缩消息的开始和结束。
Now I use two special characters to find start and end of message but with compression there can be errors. 现在,我使用两个特殊字符查找消息的开头和结尾,但是使用压缩可能会出现错误。

There is maybe a type of compression that don't use some specific bytes? 可能有一种不使用某些特定字节的压缩方式?
I use C# to open and manage sockets so I need a compression that work under windows. 我使用C#打开和管理套接字,因此我需要在Windows下工作的压缩方式。

Append to start of message it length. 追加到消息开始的长度。 After that you just need to read length, and after that get exactly count of bytes what you need. 之后,您只需要读取长度,然后就可以准确获取所需的字节数。
It will looks like: 它看起来像:

|length|data|..|..|length|data|..|..|..| |长度|数据| .. | .. |长度|数据| .. | .. | .. |

And more exactly 更确切地说

|3|26|125|36|4|12|45|16|34| | 3 | 26 | 125 | 36 | 4 | 12 | 45 | 16 | 34 |

Where 3 and 4 are length. 其中3和4是长度。

You just need an escaping scheme. 您只需要一个转义方案。

  • Send STX (Start of Transmission) at the start. 在开始时发送STX(发送开始)。
  • Send ETX (End of Transmission) at the end. 最后发送ETX(传输结束)。
  • If an STX or ETX appears in the data, prefix it with ESC (escape). 如果数据中出现STX或ETX,请在其前面加上ESC(转义)。
  • If an ESC appears in the data, prefix it with ESC. 如果数据中出现ESC,请在其前面加上ESC。

At the receiver: 在接收方:

  • The first byte should be STX, otherwise you have a bug. 第一个字节应该是STX,否则您将遇到错误。 Discard it. 丢弃它。
  • After that, if a byte is ESC, discard it and accept the next byte, whatever it is. 之后,如果一个字节是ESC,则将其丢弃并接受下一个字节,无论它是什么。
  • Otherwise, if the next byte is ETX, discard it and stop reading. 否则,如果下一个字节是ETX,则将其丢弃并停止读取。

The problem with the length-word prefix suggested in another answer is that you can't know the length without doing the compression first, which costs time and space. 另一个答案中建议的长度字前缀的问题是,如果不先进行压缩就无法知道长度,这会浪费时间和空间。

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

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