using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using QMFrameWork.Data;
namespace NET.TCP
{
public static class TCPBase
{
///
/// 发送数据流
///
/// 用于发送数据的套接字
/// 授权码
/// 客户端标识
/// 要发送的数据流
public static void SendStream(Socket socket, string license, string clientSN, MemoryStream ms)
{
byte[] tmp = null;
byte[] data = null;
string comp = "0";
//转换数据长度类型
if (ms.Length > 1000)
{
comp = "1";
data = Common.IO.Compression.DeflateCompression.Compress(ms).ToArray();
}
else
{
data = ms.ToArray();
}
socket.Send(Encoding.ASCII.GetBytes(comp), 0, 1, SocketFlags.None);
socket.Send(BitConverter.GetBytes(data.Length), 0, 4, SocketFlags.None);
tmp = Encoding.ASCII.GetBytes(license);
socket.Send(BitConverter.GetBytes(tmp.Length), 0, 4, SocketFlags.None);
socket.Send(tmp, 0, tmp.Length, SocketFlags.None);
tmp = Encoding.ASCII.GetBytes(clientSN);
socket.Send(BitConverter.GetBytes(tmp.Length), 0, 4, SocketFlags.None);
socket.Send(tmp, 0, tmp.Length, SocketFlags.None);
socket.Send(data, 0, data.Length, SocketFlags.None);
}
///
/// 接收数据流
///
/// 用于接收数据的套接字
/// 数据流的总长度
/// 是否压缩
///
public static MemoryStream ReceiveStream(Socket socket, int streamLength, bool compress)
{
//设定以阻塞模式传输
socket.Blocking = true;
//当数据流大于0时
if (streamLength > 0)
{
//创建流
MemoryStream ms = new MemoryStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int recvCount = 0;//一次获取数据的长度
int recvTotal = 0;//获取数据的总长度
try
{
//开始获取数据
while (streamLength > recvTotal)
{
//获取数据
recvCount = socket.Receive(buffer, 0, buffer.Length, SocketFlags.None);
//累加数据长度
recvTotal += recvCount;
//将数据写入流
ms.Write(buffer, 0, recvCount);
//数据获取错误
if (recvCount == 0)
{
ms.Dispose();
ms = null;
throw new Common.Exceptions.SocketTransformException();
}
}
return compress ? Common.IO.Compression.DeflateCompression.Decompress(ms) : ms;
}
catch (Exception ex)
{
if (ms != null)
{
ms.Dispose();
ms = null;
}
throw ex;
}
finally
{
buffer = null;
}
}
return null;
}
///
/// 接收数据头
///
/// 用于接收数据的套接字
/// 数据部分数据流的总长度
/// 许可证码
/// 客户端授权码
/// 压缩标识
public static void ReceiveDataHead(Socket socket, out int streamLength, out string license, out string clientSN, out bool compress)
{
//设定以阻塞模式传输
socket.Blocking = true;
//接收压缩标识
byte[] byteComp = new byte[1];
try
{
socket.Receive(byteComp, 0, 1, SocketFlags.None);
}
catch (Exception ex)
{
}
if (Encoding.ASCII.GetString(byteComp) == "0")
compress = false;
else
compress = true;
//接收数据头
byte[] tmp = new byte[4];
try
{
socket.Receive(tmp, 0, 4, SocketFlags.None);
}
catch (Exception ex)
{
}
streamLength = BitConverter.ToInt32(tmp, 0);
//接收License
tmp = new byte[4];
socket.Receive(tmp, 0, 4, SocketFlags.None);
int licenseLength = BitConverter.ToInt32(tmp, 0);
tmp = new byte[licenseLength];
socket.Receive(tmp, 0, tmp.Length, SocketFlags.None);
license = Encoding.ASCII.GetString(tmp);
//接收clientSN
tmp = new byte[4];
socket.Receive(tmp, 0, 4, SocketFlags.None);
int clientSNLength = BitConverter.ToInt32(tmp, 0);
tmp = new byte[clientSNLength];
socket.Receive(tmp, 0, tmp.Length, SocketFlags.None);
clientSN = Encoding.ASCII.GetString(tmp);
}
}
}