当前位置:编程学习 > C#/ASP.NET >>

C#基于Udp的分包传输

   因为要远做程摄像头监控,要用到网络数据传输,百度了一下,很多就是基于Tcp的,因为QQ是用Udp,所有我也尝试用Udp。

要用Udp传输数据,就免不了分包和重包,因为Udp最大只能传输64KB的数据!下面给出分包的代码:

   首先定义一个包类:

using System;
using System.Collections.Generic;

namespace Packet_Library
{


    /// <summary>
    /// 包类
    /// </summary>
    public class Packet
    {
        private int  _index;
        private byte[]  _data;
        private int  _state;
        public Packet(int index, byte[] buffer,int state)
        {
            this._index = index;
            this._data = buffer;
            this._state = state;
        }
        /// <summary>
        /// 索引序号
        /// </summary>
        public int Index
        {
            get { return _index; }
            set { _index = value; }
        }
        /// <summary>
        /// 数据
        /// </summary>
        public byte[] Data
        {
            get { return _data; }
            set { _data = value; }
        }
        /// <summary>
        /// 状态,用来记录包的开始和结束。1:开始,2:中间,3:包尾。
        /// </summary>
        public int State
        {
            get { return _state; }
            set { _state = value; }
        }
    }

}

 

再定义一个处理包的类:

using System;
using System.Collections.Generic;

namespace Packet_Library
{
    /// <summary>
    /// 分包
    /// </summary>
    public class PacketSplitter
    {
        private static int defaultPartSize = 1024;
        /// <summary>
        /// 分包
        /// </summary>
        /// <param name="datagram">数据包</param>
        /// <param name="partSize">块大小(小于1024*64)</param>
        /// <returns>分包列表</returns>
        public static List<Packet> Split(byte[] datagram, int partSize)
        {
            defaultPartSize = partSize;
            return Split(datagram);

        }

        /// <summary>
        /// 分包
        /// </summary>
        /// <param name="datagram">数据包(使用默认块大小:1024 byte)</param>
        /// <returns>分包列表</returns>
        public static List<Packet> Split(byte[] datagram)
        {
            List<Packet> packets = new List<Packet>();
            if (datagram == null)
                return null;
            if (datagram.Length <= defaultPartSize)
            {
                packets.Add(new Packet(0, datagram, 1));
                return packets;
            }
            int _length = datagram.Length;
            int counts = _length / defaultPartSize;
            int remainder = _length % defaultPartSize;
            int tatal = counts;
            if (remainder > 0)
                counts++;
            for (int i = 0; i < counts; i++)
            {
                int _size = defaultPartSize;
                if (_length - defaultPartSize * i < defaultPartSize)
                    _size = _length - defaultPartSize * i;
                byte[] tmp = new byte[_size];
                Buffer.BlockCopy(datagram, defaultPartSize * i, tmp, 0, _size);
                int state = 2;
                if (i == 0)
                    state = 1;
                if (i == counts - 1)
                 &nbs

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,