Send a message resume file over TCP.

Source: Internet
Author: User

Send a message resume file over TCP.

1. Customize the fixed protocol header. Format: ([head] [body] [filestream])

/// <Summary>
/// Packet header
/// </Summary>
[StructLayout (LayoutKind. Sequential, Pack = 1)]
Public struct ZsyDataPackHead
{
/// <Summary>
/// Client ID
/// </Summary>
Public int ClientID;
/// <Summary>
/// Client type
/// </Summary>
Public int ClientType;
/// <Summary>
/// Command value
/// </Summary>
Public int CommandVal;
/// <Summary>
/// Extension type
/// </Summary>
Public int ExtendType;
/// <Summary>
/// User ID
/// </Summary>
Public int UserID;
/// <Summary>
/// Session ID
/// </Summary>
Public int CookeID;
/// <Summary>
/// Content size
/// </Summary>
Public int BodySize;
/// <Summary>
/// Expand the data size
/// </Summary>
Public int ExtendSize;
}

2. Send messages

Private void SendMsg (ZsyDataPackHead head, byte [] bodyInfo, byte [] bdatas = null)
{
Int extendlen = 0;
If (bodyInfo = null)
Return;
If (bdatas! = Null)
Extendlen = bdatas. Length;

Int totalsize = Utility. HeadSize + bodyInfo. Length + extendlen;
Byte [] senddata = new byte [totalsize];
Byte [] headbyte = Utility. Struct2Bytes <ZsyDataPackHead> (head );
Senddata. InsertArray <byte> (0, headbyte );
Senddata. InsertArray <byte> (Utility. HeadSize, bodyInfo );
If (bdatas! = Null)
Senddata. InsertArray <byte> (Utility. HeadSize + bodyInfo. Length, bdatas );
If (! Client. TrySend (new ArraySegment <byte> (senddata )))
AddItem ("failed to send ");
If (client. ReceiveBufferSize <totalsize)
AddItem ("the packet size is too large ");
}

3. Group packages

List <byte []> lst = new List <byte []> ();
List <ServiceData> lstdata = new List <ServiceData> ();
While (fs2.Position <fs2.Length)
{
Int zsyfilesteamsize = int. Parse (ConfigurationManager. etettings ["packsize"])-int. Parse (ConfigurationManager. etettings ["bodysize"]);
Data = new byte [zsyfilesteamsize]; // byte array of file stream data packets
Int count = fs2.Read (data, 0, data. Length );
If (count! = Data. Length)
Data = data. SubArray <byte> (0, count );
Lst. Add (data );
Data = null;
}

4. Unpack on the server

Using (FileStream fs = new FileStream (filepath, FileMode. Append ))
{
BinaryWriter bw = new BinaryWriter (fs );
Bw. Write (zsy. ExtendData );
Bw. Close ();
Fs. Close ();
Zsy. ExtendData = null;
}

 

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.