📄 cenet.cs
字号:
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Threading;
namespace Wholesale
{
/// <summary>
/// CENet 的摘要说明。
/// </summary>
public class CENet
{
private IPEndPoint iIpep;
private Socket sClient;
/// <summary>
/// 初始化通讯
/// </summary>
/// <param name="IP">传入的服务器IP地址</param>
/// <param name="Port">传入的服务器端口</param>
public CENet(string IP,int Port)
{
//
// TODO: 在此处添加构造函数逻辑
//
iIpep=new IPEndPoint(IPAddress.Parse(IP),Port);
sClient=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
sClient.Connect(iIpep);
if(!sClient.Connected)
{
sClient.Shutdown(SocketShutdown.Both);
sClient.Close();
}
}
//发送文件 true:发送成功,false:发送失败
//FileName:传入的需要发送的文件名
//HeaderMsg:传入的头文件信息,长度为128位
public bool SendFile(string FileName,string HeaderMsg)
{
if(!sClient.Connected)
{
return false;
}
byte[] Header=new byte[128];
//byte[] End=new byte[6];
Header=Encoding.GetEncoding("GB2312").GetBytes(HeaderMsg);
NetworkStream ns=new NetworkStream(sClient);
StreamReader sr=new StreamReader(FileName,Encoding.GetEncoding("gb2312"));
byte[] buf=Encoding.GetEncoding("gb2312").GetBytes(sr.ReadToEnd().ToString());
//发送上载文件命令;
//sClient.Send(Encoding.GetEncoding("gb2312").GetBytes("SedDt"));
//添加文件的路径
FileInfo fi=new FileInfo(FileName);
//发送文件大小
long FileSize=fi.Length;
byte[] fs=new byte[10];
fs=Encoding.Default.GetBytes(string.Concat("SedDt",BC(FileSize.ToString(),10),CB(FileName,49)));
//发送大小
sClient.Send(fs);
if(ns.CanWrite)
{
//发送文件头信息
ns.Write(Header,0,Header.Length);
//发送数据信息
ns.Write(buf,0,buf.Length);
//发送结束信息
//ns.Write(End,0,End.Length);
ns.Flush();
}
else
{
return false;
}
sr.Close();
ns.Close();
return true;
}
//下载文件 true:下载成功,false:下载失败
//FileName:下载保存的文件名
//
public string DownFile(string PDAID)
{
if(!sClient.Connected)
{
return "Error";
}
NetworkStream ns=new NetworkStream(sClient);
//发送请求下载文件命令
sClient.Send(Encoding.GetEncoding("gb2312").GetBytes("GetDt"+CB(PDAID,59)));
string sFileName="";
string sMD5code="";
string sCiKu="";
if(ns.CanRead)
{
byte[] buf=new byte[1024];
string TempFile="";
int size=0;
int ToTalSize=0;
Thread.Sleep(200);
while(true)
{
if(ns.DataAvailable)
{
if(sClient.Poll(15000,SelectMode.SelectRead))
{
size=ns.Read(buf,0,buf.Length);
ToTalSize+=size;
}
else
{
break;
}
TempFile=string.Concat(TempFile,Encoding.GetEncoding("gb2312").GetString(buf,0,size));
if(TempFile.Length>=128 )
{
string sSize=TempFile.Substring(26,10);
int iSize=Int32.Parse(sSize);
/*
for(int i=0;i<10;i++)
{
string iszero=sSize.Substring(i,1);
if(iszero.CompareTo("0")!=0)
{
iSize=Int32.Parse(sSize.Substring(i));
break;
}
}
*/
if(ToTalSize-128==iSize)
break;
}
Thread.Sleep(100);
}
}
if(TempFile.Length==0)
{
ns.Close();
return CB("Empty",21);
}
//截取文件名
sFileName=TempFile.Substring(0,21);
sCiKu=TempFile.Substring(21,5);
//截取md5码
sMD5code=TempFile.Substring(96,32);
byte[] FileBuf=Encoding.GetEncoding("gb2312").GetBytes(TempFile);
//生成文件流
FileStream fs=new FileStream(sFileName,FileMode.OpenOrCreate,FileAccess.ReadWrite);
//从128位以后写入文件
fs.Write(FileBuf,128,FileBuf.Length-128);
fs.Flush();
fs.Close();
MD5 md5=new MD5();
if(md5.MDFile(sFileName).CompareTo(sMD5code)!=0)
{
//效验失败,返回Error
//return "Error"+sFileName;
ns.Close();
return CB("Error",21);
}
}
else
{
ns.Close();
return CB("Error",21);
//return "Error"+sFileName;
}
ns.Close();
//成功,返回保存的文件名
return sFileName+sCiKu;
//return "Suces"+sFileName;
}
//发送退出命令
public void Close()
{
sClient.Shutdown(SocketShutdown.Both);
sClient.Close();
}
//发送终端ID
//固定30位,不足在前面补0
//返回值:true为发送成功,false为失败
public bool SendPKID(string PKID)
{
if(!sClient.Connected)
{
return false;
}
string sTemp=string.Concat("CFile",CB(PKID,59));
if(sClient.Send(Encoding.GetEncoding("gb2312").GetBytes(sTemp))<=0)
return false;
else
return true;
}
/// <summary>
/// 发送命令
/// </summary>
/// <param name="cmd">传入的命令(64位:命令(5位)+文件名(21位)+0(38位))</param>
public void SendCmd(string cmd)
{
sClient.Send(Encoding.Default.GetBytes(cmd));
}
/// <summary>
/// 接收命令
/// 返回接收到字符串
/// </summary>
/// <returns>返回接收到字符串</returns>
public string RecCmd()
{
string TempCmd="";
byte[] buf=new byte[128];
sClient.Receive(buf);
TempCmd=Encoding.Default.GetString(buf,0,64);
return TempCmd;
}
/// <summary>
/// 前端补零
/// </summary>
/// <param name="sName"></param>
/// <param name="i"></param>
/// <returns></returns>
static string BC(string sName,int i)
{
while(sName.Length<i)
{
sName="0"+sName;
}
return sName;
}
/// <summary>
/// 末尾补零
/// </summary>
/// <param name="sName"></param>
/// <param name="i"></param>
/// <returns></returns>
static string CB(string sName,int i)
{
while(sName.Length<i)
{
sName=sName+"0";
}
return sName;
}
/// <summary>
/// 详查命令
/// </summary>
/// <param name="BarCode">BarCode:输入的128位查询信息</param>
/// <returns>接收到的查询数据,string型</returns>
public string LookP(string BarCode)
{
if(BarCode.Length==0)
return "";
sClient.Send(Encoding.Default.GetBytes("LookP"+CB(BarCode,59)));
string sTemp="";
NetworkStream ns=new NetworkStream(sClient);
if(RecCmd().CompareTo("Error")!=0)
{
if(ns.CanRead)
{
byte[] buf=new byte[512];
int size=0;
do
{
size=ns.Read(buf,0,buf.Length);
sTemp=string.Concat(sTemp,Encoding.GetEncoding("gb2312").GetString(buf,0,size));
}while(ns.DataAvailable);
}
}
ns.Close();
return sTemp;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -