通过TCP发送数据包类! [英] Sending a packet class over TCP!

查看:167
本文介绍了通过TCP发送数据包类!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Codeproject!



我们假设我们有这样的课程:



  public   class  MovedToPositionPacket:Packet 
{
public int nx { get ; set ;}
public int ny { get ; set ;}

public MovedToPositionPacket()
{
}
}





我们然后将此类转换为字节数组。然后将字节数组复制到另一个大小为4096的字节数组(Buffer),而Packet Class只有96字节大。



如果我们发送该字节array(object)如何在不使用其他4000个空字节的情况下将其转换回同一个类?

解决方案

我在看过你的解决方案之后发布了这个发布(解决方案3)。



多年来,我一直参与指定我的二进制线协议。我的建议是不要这样做,除非你的要求可能需要最小的通信包。如果这只是您正在创建的一个程序,那么请务必继续沿着这条路走下去,因为您很可能会从中学到很多东西。



这几天有这么多工具可以使这样的事情在未来更容易,更快速地开发和维护。我建议您考虑使用JSON或XML序列化。

www.json.org [ ^ ]

http://www.w3resource.com/JSON/introduction.php [ ^ ]



一些CodeProject文章:

fastJSON [ ^ ]

fastBinaryJSON [ ^ ]



www.codeproject.com/KB/IP/#General [ ^ ]



Soren Madsen

接收器如何知道这个数据属于哪个类?如果没有某种元数据,它就不可能知道,所以你将不得不在8字节旁边发送其他东西。



说真的,你想尝试拿出你自己的通信协议??


解决方案:



我将不得不发送一个字节数组,其中数组的两个第一个字节构成类的类型(例如,更改播放器的位置),另一个字节将告诉服务器类的字节数是多长。



例如



0 = ChangeLocationPacket



从视觉上看,这看起来很像:



([0] [5字节长] [2] [3] [4] [5] [6 ])



^这将是新的字节数组,数组中的每个字节都用[和a]分割。



然后你会先看看它正在使用哪种类型以及if或switch语句来读出它:



  if (array [ 0 ] ==  0 
type = ChangeLocationPacket.GetType();





然后,你会查看多少字节长度为:



  int 长度=( int )array [ 1 ]; 





您甚至可以使用两个或三个字节来添加它们gether,或者在codeproject中使用一些神奇的公式。



接下来,你会读出它,在这种情况下,只有一个字节告诉如何读它的代码是这样的:



 Array.Copy(array, 2 ,newarray, 0 ,长度); 





然后你可以返回对象和类型,以便将它转换回类。


Hello Codeproject!

Let's assume we have a class like so:

public class MovedToPositionPacket : Packet
{
 public int nx { get; set;}
 public int ny { get; set;}

 public MovedToPositionPacket()
 {
 }
}



And we then convert this class to a byte array. The byte array then is copied to another byte array(Buffer) with the size of 4096, while the Packet Class is only 96 bytes big.

If we send that byte array(object) how can we convert it back to the same class without using the other 4000 empty bytes?

解决方案

I am posting this after having seen the Solution you have posted (Solution 3).

I have been involved with specifying my share of binary wire protocols like this through the years. My advice is don't do this unless your requirements call for the smallest communication packets possible. If this is just a program you are creating to learn from, then by all means continue down this path as you will most likely learn a lot from it.

These days there are so many tools that make things like this easier and faster to develop and maintain in the future. I recommend you look into using JSON or XML serialization.
www.json.org[^]
http://www.w3resource.com/JSON/introduction.php[^]

Some CodeProject articles:
fastJSON[^]
fastBinaryJSON[^]

www.codeproject.com/KB/IP/#General[^]

Soren Madsen


How is the receiver going to know what class this data belongs in?? Without some kind of metadata, it's impossible for it to know, so you're going to have to send something else beside 8 bytes.

Seriously, are you trying to come up with your own communication protocol??


The solution:

I will have to send a byte array in which the two first bytes of the array make up the type of the class(For instance a change of location of the player) and another byte that will tell the server how long the class's byte array is.

For instance

0 = ChangeLocationPacket

Visually, it would look lik this:

( [0] [5 bytes long] [2] [3] [4] [5] [6] )

^ this will be the new byte array, every byte in the array splitted with a [ and a ].

You would then read it out by firstly checking out what type of class it is using either and if or a switch statement:

if(array[0] == 0)
 type = ChangeLocationPacket.GetType();



Then, you would check out how many bytes in length it is:

int Length = (int)array[1];



You could even use two or three bytes to add them up together, or use some magical formula lieing around here in codeproject.

Next, you would read it out, and in this case, there is only one byte which tells how long it is so the code to read it out would be:

Array.Copy(array, 2, newarray, 0, Length);



And afterwards you could return the object and the type in order to convert it back to the class.


这篇关于通过TCP发送数据包类!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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