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;
}