using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
public class ReceiveDataHandle
{
//包头占2个字节
public const int HeadLength = 2;
//协议类型
public const int MsgType = 2;
//默认缓冲区大小(不包括包头,协议类型)
public const int BufferMaxSize = 1024;
public Socket client;
private byte[] bytes;
public byte[] Bytes
{
get { return bytes; }
set { bytes = value; }
}
private int curBufferSize;
/// <summary>
/// 当前接收到的字节
/// </summary>
public int CurBufferSize
{
get { return curBufferSize; }
set { curBufferSize = value; }
}
private int startPos;
/// <summary>
/// 起始位置
/// </summary>
public int StartPos
{
get { return startPos; }
set { startPos = value; }
}
private int endPos;
/// <summary>
/// 结束位置
/// </summary>
public int EndPos
{
get { return endPos; }
set { endPos = value; }
}
public int recvDataLen;
public ReceiveDataHandle(Socket _socket)
{
recvDataLen = 0;
client = _socket;
InitBuffer();
}
public void InitBuffer()
{
startPos = 0;
endPos = BufferMaxSize;
curBufferSize = 0;
bytes = new byte[BufferMaxSize];
}
/// <summary>
/// 获取包头
/// </summary>
/// <returns></returns>
public int GetMsgLen()
{
try
{
byte[] temp = new byte[HeadLength];
Array.Copy(bytes, 0, temp, 0, HeadLength);
return System.BitConverter.ToUInt16(temp, 0);
}
catch
{
//非法数据
return -1;
}
}
/// <summary>
/// 获取消息类型
/// </summary>
/// <returns></returns>
public int GetMsgType()
{
try
{
byte[] temp = new byte[MsgType];
Array.Copy(bytes, 2, temp, 0, MsgType);
return System.BitConverter.ToUInt16(temp, 0);
}
catch
{
//非法数据
return -1;
}
}
public void CloseConn()
{
if (client == null || client.Connected == false)
return;
client.Shutdown(SocketShutdown.Both);
client.Close();
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
public class ReceiveDataHandle
{
//包头占2个字节
public const int HeadLength = 2;
//协议类型
public const int MsgType = 2;
//默认缓冲区大小(不包括包头,协议类型)
public const int BufferMaxSize = 1024;
public Socket client;
private byte[] bytes;
public byte[] Bytes
{
get { return bytes; }
set { bytes = value; }
}
private int curBufferSize;
/// <summary>
/// 当前接收到的字节
/// </summary>
public int CurBufferSize
{
get { return curBufferSize; }
set { curBufferSize = value; }
}
private int startPos;
/// <summary>
/// 起始位置
/// </summary>
public int StartPos
{
get { return startPos; }
set { startPos = value; }
}
private int endPos;
/// <summary>
/// 结束位置
/// </summary>
public int EndPos
{
get { return endPos; }
set { endPos = value; }
}
public int recvDataLen;
public ReceiveDataHandle(Socket _socket)
{
recvDataLen = 0;
client = _socket;
InitBuffer();
}
public void InitBuffer()
{
startPos = 0;
endPos = BufferMaxSize;
curBufferSize = 0;
bytes = new byte[BufferMaxSize];
}
/// <summary>
/// 获取包头
/// </summary>
/// <returns></returns>
public int GetMsgLen()
{
try
{
byte[] temp = new byte[HeadLength];
Array.Copy(bytes, 0, temp, 0, HeadLength);
return System.BitConverter.ToUInt16(temp, 0);
}
catch
{
//非法数据
return -1;
}
}
/// <summary>
/// 获取消息类型
/// </summary>
/// <returns></returns>
public int GetMsgType()
{
try
{
byte[] temp = new byte[MsgType];
Array.Copy(bytes, 2, temp, 0, MsgType);
return System.BitConverter.ToUInt16(temp, 0);
}
catch
{
//非法数据
return -1;
}
}
public void CloseConn()
{
if (client == null || client.Connected == false)
return;
client.Shutdown(SocketShutdown.Both);
client.Close();
}
}