/// <summary> /// 发送 /// </summary> public void StartSend() { //创建一个文件对象 FileInfo EzoneFile = new FileInfo(filename); //打开文件流 FileStream EzoneStream = EzoneFile.OpenRead(); //包的大小 int PacketSize = 100000; //包的数量 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); //连接到发送端 client.Connect(ipep); //获得客户端节点对象 IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint; //获得客户端的IP地址 //this.textBox7.Text=clientep.Address.ToString(); //发送[文件名]到客户端 TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes(genfile)); //发送[包的大小]到客户端 TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes(PacketSize.ToString())); //发送[包的总数量]到客户端 TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes(PacketCount.ToString())); //发送[最后一个包的大小]到客户端 TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes(LastDataPacket.ToString())); //数据包 byte[] data = new byte[PacketSize]; //开始循环发送数据包 for (int i = 0; i < PacketCount; i++) { //从文件流读取数据并填充数据包 EzoneStream.Read(data, 0, data.Length); //发送数据包 TransferFiles.SendVarData(client, data); } //如果还有多余的数据包,则应该发送完毕! if (LastDataPacket != 0) { data = new byte[LastDataPacket]; EzoneStream.Read(data, 0, data.Length); TransferFiles.SendVarData(client, data); } //关闭套接字 client.Close(); //关闭文件流 EzoneStream.Close(); }
public void StartReceive() { //创建一个网络端点 IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(ip), port); //创建网络监听 lisner = new TcpListener(ipep); lisner.Start(); while (true) { ////确认连接 if (!lisner.Pending()) { Thread.Sleep(1000); continue; } flag = false; Socket client = lisner.AcceptSocket(); //获得客户端节点对象 IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint; //获得[文件名] string SendFileName = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client)); //获得[包的大小] string bagSize = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client)); //获得[包的总数量] int bagCount = int.Parse(System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client))); //获得[最后一个包的大小] string bagLast = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client)); //创建一个新文件 string fileFullName = filename + SendFileName; FileStream MyFileStream = new FileStream(fileFullName, FileMode.Create, FileAccess.Write, FileShare.Read); //已发送包的个数 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(); flag = true; } }
/// <summary> /// 向后台发送文件 /// </summary> /// <param name="address"></param> /// <param name="port"></param> private void readFileStream(String address, int port) { Socket client = null; FileStream EzoneStream = null; try { // 读取文件流 string path = txtDocUpload.Text; FileInfo EzoneFile = new FileInfo(path); EzoneStream = EzoneFile.OpenRead(); // 包大小 int PacketSize = 100000; // 共有多少个包 int PacketCount = (int)(EzoneStream.Length / ((long)PacketSize)); // 最后一个包大小 int LastDataPacket = (int)(EzoneStream.Length - ((long)(PacketSize * PacketCount))); // 建立socket IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(address), port); client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); client.Connect(ipep); // 发送文件到后台 byte[] data = new byte[PacketSize]; for (int i = 0; i < PacketCount; i++) { EzoneStream.Read(data, 0, data.Length); TransferFiles.SendVarData(client, data); } if (LastDataPacket != 0) { data = new byte[LastDataPacket]; EzoneStream.Read(data, 0, data.Length); TransferFiles.SendVarData(client, data); } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { if (client != null) { client.Close(); } if (EzoneStream != null) { EzoneStream.Close(); } } }