[英]How to structure custom binary protocol TCP request
I'm working on a code challenge which, in short, provides a server that encodes/decodes the data sent in the request. 我正在处理一个代码挑战,总之,它提供了一个对请求中发送的数据进行编码/解码的服务器。 It is a TCP/IP server with the following custom binary request/response structures:
它是具有以下自定义二进制请求/响应结构的TCP / IP服务器:
I need to create a desktop client that will send requests and receive responses, and display data accordingly. 我需要创建一个桌面客户端,该客户端将发送请求和接收响应,并相应地显示数据。 I'm having trouble understanding how the request would be formed, and how I would send the request to the TCP/IP server (using C#).
我在理解如何形成请求以及如何将请求发送到TCP / IP服务器(使用C#)时遇到麻烦。 Could someone provide perhaps an example of what this might look like?
有人可以提供一个例子来说明这个样子吗?
not sure about your checksum, but constructing a packet would look something like 不确定您的校验和,但是构造一个数据包看起来像
byte[] CreatePacket(string s, Operation op)
{
var packet = new List<byte>();
packet.AddRange(BitConverter.GetBytes((UInt16) 0x1092));
packet.Add(1);
var data = Encoding.ASCII.GetBytes(s);
packet.AddRange(BitConverter.GetBytes((UInt32) (9 + data.Length)));
packet.Add((byte) (op == Operation.Encode ? 1 : 2));
packet.AddRange(data);
packet.Add(CheckSum(packet));
return packet.ToArray();
}
given 给定
enum Operation
{
Encode,
Decode,
}
and 和
server.Send(CreatePacket("test string", Operation.Encode))
Also with the checksum spec... 还有校验和规范...
byte CheckSum(List<byte> packet)
{
return (byte)(packet.Select((b, i) => (value: b, index: i))
.Sum(o => (o.value & (1 << (o.index % 8))) > 0 ? 1 :0) % 256);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.