C # Udp-Based Packet Transfer

Source: Internet
Author: User

For remote camera monitoring and network data transmission, many of them are based on Tcp, because QQ uses Udp, and I also try to use Udp.

If you want to use Udp to transmit data, you can avoid subcontracting and repackaging, because Udp can only transmit up to 64 KB of Data! The code for subcontracting is as follows:

First define a package class:

Using System;
Using System. Collections. Generic;

Namespace Packet_Library
{


/// <Summary>
/// Package
/// </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>
/// Index No.
/// </Summary>
Public int Index
{
Get {return _ index ;}
Set {_ index = value ;}
}
/// <Summary>
/// Data
/// </Summary>
Public byte [] Data
{
Get {return _ data ;}
Set {_ data = value ;}
}
/// <Summary>
/// Status, used to record the start and end of the package. 1: Start, 2: middle, 3: End of the package.
/// </Summary>
Public int State
{
Get {return _ state ;}
Set {_ state = value ;}
}
}

}

 

Define another Processing Package class:

Using System;
Using System. Collections. Generic;

Namespace Packet_Library
{
/// <Summary>
/// Subcontract
/// </Summary>
Public class PacketSplitter
{
Private static int defaultPartSize = 1024;
/// <Summary>
/// Subcontract
/// </Summary>
/// <Param name = "datax"> data packet </param>
/// <Param name = "partSize"> block size (less than 1024*64) </param>
/// <Returns> subcontract list </returns>
Public static List <Packet> Split (byte [] datax, int partSize)
{
DefaultPartSize = partSize;
Return Split (datax );

}

/// <Summary>
/// Subcontract
/// </Summary>
/// <Param name = "datax"> data packet (use the default block size: 1024 bytes) </param>
/// <Returns> subcontract list </returns>
Public static List <Packet> Split (byte [] datax)
{
List <Packet> packets = new List <Packet> ();
If (datasync = null)
Return null;
If (datasync. Length <= defaultPartSize)
{
Packets. Add (new Packet (0, datax, 1 ));
Return packets;
}
Int _ length = datalist. 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 (datasync, defaultPartSize * I, tmp, 0, _ size );
Int state = 2;
If (I = 0)
State = 1;
If (I = counts-1)
State = 3;
Packets. Add (new Packet (I, tmp, state ));
}
Return packets;
}
}
}

 

Because Udp is connectionless, I also simulate handshaking for data transmission. Don't say something about handshaking with me. If you really don't know, I will say a few more words. The so-called handshake is a connection method, just like when we make a call, you first pull the number and wait. If the other party receives the call, you can call. Tcp is like this!

In Udp [, We can first define several commands:

Public Enum Commands {

RequestVideoCapital = 1, // request to get the video

RequestSendPacket = 2, // request to send data packets

ResponseSendPacket = 3, // response to the sent packet

ResponseEndPacket = 4 // packet transfer ends

}

With these commands, We can remotely video videos. This is a process like this:

First, the receiver requests the video (requestVideoCapital) --> the sender receives and obtains the image of the camera for subcontracting (ResponseSendPacket), and then sends the first packet --> the receiver receives the packet and requests the next packet (requestSendPacket) --> the sender determines whether the end of the packet is the end of the packet. If the end of the packet is the end of the packet, the sender notifies the customer (ResponseEndPacket) --> the receiver determines whether the end of the packet is the end of the packet. If yes, the receiver duplicates the packet and) form a circular network transmission, which is a real-time video monitoring!

 

Next we will talk about the method of receiving end heavy packet:

First, we define an array of storage packages:

Private byte [] imageBuffer;

When re-request, set imageBuffer = null;

When receiving the package: CopyData (data );


Private void CopyData (byte [] data)
{
If (imageBuffer = null)
{
ImageBuffer = data;
Return;
}
Byte [] buffer = new byte [imageBuffer. Length + data. Length];
Buffer. BlockCopy (imageBuffer, 0, buffer, 0, imageBuffer. Length );
Buffer. BlockCopy (data, 0, buffer, imageBuffer. Length, data. Length );
ImageBuffer = buffer;
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.