如何设计TCP数据报并发送数据?
How to design TCP datagram and send data?
大家好,我有服务器ip和端口号,然后我想用Socket TcpClient对象连接服务器发送和接收数据,但是我不知道如何自定义tcp-ip数据报.
这里是数据包的描述
name bit datatype
sync_tag 16 uint(16) const:0xAA 0x55
version 8 uint(8) const:0x01
packet_length 16 uint(16) ?
payload_id 8 uint(8) 0x01,0x02,0x03,0x04...(now it is 0x01)
for(int i=0;i<length-8;i++){
payload_data 8 byte(1) ?
}
CRC16 16 uint(16) ?
packet_length:从sync_tag的第一个字节到最后一个字节CRC16
payload_data :现在结构如下
syntax bit datatype
username 264 char(33)
password 264 char(33)
是否有任何方法可以了解什么是 "length-8" 以及如何计算 packet_length?
最后我将使用 TcpClient 和 NetwordStream 发送 byte[]?
>here is the codes I attempt to write
>I don't know how to send the packet
[1]: chinafilm.org.cn/uploads/soft/140117/2_1140448191.pdf
public static void TestTcpClient()
{
TcpClient client = new TcpClient("58.62.144.227", 18080);
NetworkStream stream = client.GetStream();
client.LingerState = new LingerOption(true, 30);
if (!client.Connected)
{
Console.WriteLine("Connect Error!");
return;
}
byte[] buffer = new byte[1024];
//I don't konw what is packet_length,so packetlength is a test sample
byte[] packetlength = new byte[2];
//Like packetlength,It is a test sample;
byte payloadData = byte.MinValue;
//CRC16 algorithm,I can find some example
byte[] crc16 = new byte[2];
buffer[0] = 0xAA; //sync_tag;
buffer[1] = 0x55;
buffer[2] = 0x01; //versin
buffer[3] = packetlength[0]; //packet_length;
buffer[4] = packetlength[1];
buffer[5] = 0x01; //payload_id : If I use the username and password,It is 0x01
buffer[6] = payloadData;//Contains username and password
buffer[7] = crc16[0];
buffer[8] = crc16[1];
stream.Write(buffer, 0, buffer.Length);
// Buffer to store the response bytes.
byte[] data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
// Close everything.
stream.Close();
client.Close();
}
您可以使用 MemoryStream
和 BinaryWriter
轻松构建包
MemoryStream bufferStream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(bufferStream);
writer.Write(new byte[] { 0xAA, 0x55, 0x01 });
writer.Write((ushort)66 + 8); // size of username/password, plus all overheads
writer.Write((byte) 0x01);
writer.Write("foouser".PadRight(33, '[=10=]')) // Padding to 33 characters
writer.Write("foooassword".PadRight(33, '[=10=]')) // Padding to 33 characters
// calculate CRC
ushort crc16 = 999;
writer.Write(crc16);
// get result
byte[] result = bufferStream.ToArray();
有关填充的更多信息:C# padding string with n bytes and writing it out
对于 CRC,您可以尝试 this NuGet package,虽然我从未使用过它。
大家好,我有服务器ip和端口号,然后我想用Socket TcpClient对象连接服务器发送和接收数据,但是我不知道如何自定义tcp-ip数据报.
这里是数据包的描述
name bit datatype
sync_tag 16 uint(16) const:0xAA 0x55
version 8 uint(8) const:0x01
packet_length 16 uint(16) ?
payload_id 8 uint(8) 0x01,0x02,0x03,0x04...(now it is 0x01)
for(int i=0;i<length-8;i++){
payload_data 8 byte(1) ?
}
CRC16 16 uint(16) ?
packet_length:从sync_tag的第一个字节到最后一个字节CRC16 payload_data :现在结构如下
syntax bit datatype
username 264 char(33)
password 264 char(33)
是否有任何方法可以了解什么是 "length-8" 以及如何计算 packet_length? 最后我将使用 TcpClient 和 NetwordStream 发送 byte[]?
>here is the codes I attempt to write
>I don't know how to send the packet
[1]: chinafilm.org.cn/uploads/soft/140117/2_1140448191.pdf
public static void TestTcpClient()
{
TcpClient client = new TcpClient("58.62.144.227", 18080);
NetworkStream stream = client.GetStream();
client.LingerState = new LingerOption(true, 30);
if (!client.Connected)
{
Console.WriteLine("Connect Error!");
return;
}
byte[] buffer = new byte[1024];
//I don't konw what is packet_length,so packetlength is a test sample
byte[] packetlength = new byte[2];
//Like packetlength,It is a test sample;
byte payloadData = byte.MinValue;
//CRC16 algorithm,I can find some example
byte[] crc16 = new byte[2];
buffer[0] = 0xAA; //sync_tag;
buffer[1] = 0x55;
buffer[2] = 0x01; //versin
buffer[3] = packetlength[0]; //packet_length;
buffer[4] = packetlength[1];
buffer[5] = 0x01; //payload_id : If I use the username and password,It is 0x01
buffer[6] = payloadData;//Contains username and password
buffer[7] = crc16[0];
buffer[8] = crc16[1];
stream.Write(buffer, 0, buffer.Length);
// Buffer to store the response bytes.
byte[] data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
// Close everything.
stream.Close();
client.Close();
}
您可以使用 MemoryStream
和 BinaryWriter
轻松构建包
MemoryStream bufferStream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(bufferStream);
writer.Write(new byte[] { 0xAA, 0x55, 0x01 });
writer.Write((ushort)66 + 8); // size of username/password, plus all overheads
writer.Write((byte) 0x01);
writer.Write("foouser".PadRight(33, '[=10=]')) // Padding to 33 characters
writer.Write("foooassword".PadRight(33, '[=10=]')) // Padding to 33 characters
// calculate CRC
ushort crc16 = 999;
writer.Write(crc16);
// get result
byte[] result = bufferStream.ToArray();
有关填充的更多信息:C# padding string with n bytes and writing it out
对于 CRC,您可以尝试 this NuGet package,虽然我从未使用过它。