using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Configuration;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace DllFileSoc
{
public class DllSocket
{
//public bool UploadFiles(string ip,string port, string clientfilename, string clientpath,ref string msg)
//{
// Thread threadClient = null; // 创建用于接收服务端消息的 线程;
// IPEndPoint endPoint = null;
// Socket sockClient = null;
// try
// {
// sockClient = RefSocket(ip, port,ref endPoint, ref msg);
// if (sockClient==null)
// {
// LogBLL.Err(msg);
// return false;
// }
// else//与服务器建立通信
// {
// try
// {
// sockClient.Connect(endPoint);
// }
// catch (SocketException se)
// {
// LogBLL.Err(sockClient.LocalEndPoint.ToString(), se);
// return false;
// }
// LogBLL.Err("与服务器连接成功!" + sockClient.LocalEndPoint.ToString());
// //threadClient = new Thread(RecMsg);
// //threadClient.IsBackground = true;
// //threadClient.Start();
// return true;
// }
// }
// catch (Exception ex)
// {
// LogBLL.Err(msg, ex);
// return false;
// }
//}
///
/// 上传文件保存到服务器
///
/// 服务监听IP地址
/// 服务监听端口
/// 上传文件的文件夹目录
/// 需要上传的文件名
/// 消息
///
public static bool SendFile(string IP, int Port,string path, string filename ,string macid,string userid, ref string msg)
{
//string serverfile = "D:\\SocketServerFile\\WeChatSetup.exe";
//创建一个文件对象
FileInfo EzoneFile = new FileInfo(path+filename);
//打开文件流
FileStream EzoneStream = EzoneFile.OpenRead();
long size = EzoneStream.Length;
byte[] array = new byte[size];//文件
EzoneStream.Read(array, 0, array.Length);
string md5 = GetMD5HashFromFile(array);
//包的大小
int PacketSize = 1024*1024*2;
//包的数量
int PacketCount = (int)(EzoneStream.Length / ((long)PacketSize));
//最后一个包的大小
int LastDataPacket = (int)(EzoneStream.Length - ((long)(PacketSize * PacketCount)));
//指向远程服务端节点
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(IP), Port);
//创建套接字
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//连接到发送端
try
{
client.Connect(ipep);
}
catch
{
msg ="服务器连接失败!:"+ IP+":"+Port;
return false;
}
//获得客户端节点对象
IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
//发送[文件名]
//TransferFiles.SendVarData(client, System.Text.Encoding.UTF8.GetBytes(EzoneFile.Name));
TransferFiles.SendVarData(client, System.Text.Encoding.UTF8.GetBytes("<--上传-->"));
TransferFiles.SendVarData(client, System.Text.Encoding.UTF8.GetBytes(filename));
//发送macid\userid\md5
TransferFiles.SendVarData(client, System.Text.Encoding.UTF8.GetBytes(macid));
TransferFiles.SendVarData(client, System.Text.Encoding.UTF8.GetBytes(userid));
TransferFiles.SendVarData(client, System.Text.Encoding.UTF8.GetBytes(md5));
//发送[包的大小]
TransferFiles.SendVarData(client, System.Text.Encoding.UTF8.GetBytes(PacketSize.ToString()));
//发送[包的总数量]
TransferFiles.SendVarData(client, System.Text.Encoding.UTF8.GetBytes(PacketCount.ToString()));
//发送[最后一个包的大小]
TransferFiles.SendVarData(client, System.Text.Encoding.UTF8.GetBytes(LastDataPacket.ToString()));
bool isCut = false;
//数据包
byte[] data = new byte[PacketSize];
//开始循环发送数据包
for (int i = 0; i < PacketCount; i++)
{
//从文件流读取数据并填充数据包
EzoneStream.Read(data, 0, data.Length);
//发送数据包
if (TransferFiles.SendVarData(client, data) == 3)
{
isCut = true;
return false;
//break;
}
}
//如果还有多余的数据包,则应该发送完毕!
if (LastDataPacket != 0)
{
data = new byte[LastDataPacket];
EzoneStream.Read(data, 0, data.Length);
TransferFiles.SendVarData(client, data);
}
//关闭文件流
EzoneStream.Close();
//关闭套接字
client.Close();
if (!isCut)
{
msg = "文件[" + filename + "]上传成功!";
return true;
}
return false;
}
///
/// 从文件服务下载文件
///
/// 服务监听IP地址
/// 服务监听端口
/// 下载文件保存到本地的目录
/// 服务器被下载的文件的完整路径
/// 消息
///
public static bool DownFile(string IP, int Port, string path, string filename, ref string msg)
{
//指向远程服务端节点
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(IP), Port);
//创建套接字
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//连接到发送端
try
{
client.Connect(ipep);
}
catch
{
msg = "服务器连接失败!:" + IP + ":" + Port;
return false;
}
//获得客户端节点对象
IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
//发送[下载指令]
TransferFiles.SendVarData(client, System.Text.Encoding.UTF8.GetBytes("<--下载-->"));
TransferFiles.SendVarData(client, System.Text.Encoding.UTF8.GetBytes(filename));
//获得[文件名]
string SendFileName = System.Text.Encoding.UTF8.GetString(TransferFiles.ReceiveVarData(client));
//获得[包的大小]
string bagSize = System.Text.Encoding.UTF8.GetString(TransferFiles.ReceiveVarData(client));
//获得[包的总数量]
int bagCount = int.Parse(System.Text.Encoding.UTF8.GetString(TransferFiles.ReceiveVarData(client)));
//获得[最后一个包的大小]
string bagLast = System.Text.Encoding.UTF8.GetString(TransferFiles.ReceiveVarData(client));
string fullPath = Path.Combine(Environment.CurrentDirectory, path + SendFileName);
try
{
//创建一个新文件
FileStream MyFileStream = new FileStream(fullPath, FileMode.Create, FileAccess.Write);
//已发送包的个数
int SendedCount = 0;
while (true)
{
byte[] data = TransferFiles.ReceiveVarData(client);
if (data.Length == 0)
{
break;
}
else
{
SendedCount++;
//将接收到的数据包写入到文件流对象
MyFileStream.Write(data, 0, data.Length);
}
}
//关闭文件流
MyFileStream.Close();
//关闭套接字
client.Close();
msg = "文件[" + filename + "]下载成功!";
return true;
}
catch (Exception ex)
{
msg = "文件[" + filename + "]下载失败!"+ex.Message;
return false;
}
}
//Socket RefSocket(string ip,string port,ref IPEndPoint endPoint, ref string msg)
//{
// try
// {
// IPAddress endip = IPAddress.Parse(ip);
// endPoint = new IPEndPoint(endip, int.Parse(port));
// return new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// }
// catch (Exception ex)
// {
// msg = ex.Message;
// return null;
// }
//}
public static string GetMD5HashFromFile(byte[] filedatas)
{
try
{
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(filedatas);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
}
catch (Exception ex)
{
//errorinfo = ex.Message.ToString();
return "";
}
}
}
}