天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
5.8 KiB

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
{
/// <summary>
/// 发送数据流
/// </summary>
/// <param name="socket">用于发送数据的套接字</param>
/// <param name="license">授权码</param>
/// <param name="clientSN">客户端标识</param>
/// <param name="ms">要发送的数据流</param>
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);
}
/// <summary>
/// 接收数据流
/// </summary>
/// <param name="socket">用于接收数据的套接字</param>
/// <param name="streamLength">数据流的总长度</param>
/// <param name="compress">是否压缩</param>
/// <returns></returns>
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;
}
/// <summary>
/// 接收数据头
/// </summary>
/// <param name="socket">用于接收数据的套接字</param>
/// <param name="streamLength">数据部分数据流的总长度</param>
/// <param name="license">许可证码</param>
/// <param name="clientSN">客户端授权码</param>
/// <param name="compress">压缩标识</param>
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);
}
}
}