public void ReceiveFiles(Socket client) { //获得[文件名] string ReceiveFileName = 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 fullPath = Path.Combine(DownLoadSavePath, ReceiveFileName); FileStream MyFileStream = new FileStream(fullPath, FileMode.Create, FileAccess.Write); //已收到包的个数 int ReceivedCount = 0; while (true) { byte[] data = TransferFiles.ReceiveVarData(client); if (data.Length == 0) { break; } else { ReceivedCount++;//将接收到的数据包写入到文件流对象 MyFileStream.Write(data, 0, data.Length); } } //关闭文件流 MyFileStream.Close(); //关闭套接字 client.Close(); SetText("\r\n" + ReceiveFileName + "下载完毕!"); }
public void UpdataFiles(Object Cnt) { ConnectInfo connect = (ConnectInfo)Cnt; IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(connect.IP), connect.Port); //指向远程服务端节点 Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { client.Connect(ipep); //连接到发送端 } catch { MessageBox.Show("连接服务器失败!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } //获得服务器端节点对象,clientep用于存储对象节点信息,没有其他作用 IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint; //发送操作命令 TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes("UpdataFile")); //等带接收返回数据 int filecount = int.Parse(System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client))); string[] filelist = new string[100]; for (int i = 0; i <= filecount - 1; i++) { filelist[i] = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client)); } client.Close(); listView1.Items.Clear(); for (int i = 0; i <= filecount - 1; i++) { string[] stArray = filelist[i].Split('$'); //listView1.Items[i].SubItems[0].Text = stArray[0]; //listView1.Items[i].SubItems[1].Text = (Convert.ToInt64(stArray[1]) / 1024.0).ToString() + "KB"; //listView1.Items[i].SubItems[2].Text = ("..."); //listView1.Items[i].SubItems[3].Text = (DateTime.Now.ToString()); //listView1.Items[i].SubItems[4].Text = ("Ok"); ListViewItem fileItem = listView1.Items.Add(stArray[0]); fileItem.Name = stArray[0]; fileItem.SubItems.Add((Convert.ToInt64(stArray[1])).ToString() + "Byte"); fileItem.SubItems.Add("..."); fileItem.SubItems.Add(DateTime.Now.ToString()); fileItem.SubItems.Add("Ok"); } }
public void AskMonitor(object Cnt) { ConnectInfo connect = (ConnectInfo)Cnt; IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(connect.IP), connect.Port); //指向远程服务端节点 Socket AskSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { AskSocket.Connect(ipep); //连接到发送端 } catch { MessageBox.Show("连接服务器出错!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } //获得服务器端节点对象 IPEndPoint clientep = (IPEndPoint)AskSocket.RemoteEndPoint; //发送[操作命名]到服务器端 TransferFiles.SendVarData(AskSocket, System.Text.Encoding.Unicode.GetBytes(MonitorServer.Cmd)); //发送文件信息 string UpLoadFileInfo = MonitorServer.FileName + "$" + (MonitorServer.FileLength.ToString()); TransferFiles.SendVarData(AskSocket, System.Text.Encoding.Unicode.GetBytes(UpLoadFileInfo)); string IPstr = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(AskSocket)); string[] IPstrArray = IPstr.Split(new string[] { "@", "/" }, StringSplitOptions.RemoveEmptyEntries); if (IPstrArray.Length > 2)//文件大于2G开始分块处理 { //接收多组IP信息,IP格式:@192.168.3.32/20000@ TargetSever[1].IP = IPstrArray[0]; TargetSever[1].Port = Convert.ToInt32(IPstrArray[1]); TargetSever[2].IP = IPstrArray[2]; TargetSever[2].Port = Convert.ToInt32(IPstrArray[3]); TargetSever[3].IP = IPstrArray[4]; TargetSever[3].Port = Convert.ToInt32(IPstrArray[5]); AskSocket.Close();//关闭套接字 if (0 == string.Compare(MonitorServer.Cmd, "UpLoad")) { //文件分快处理 SplitFile(MonitorServer.FullPath.Substring(0, MonitorServer.FullPath.Length - MonitorServer.FileName.Length) + "\\", MonitorServer.FullPath); SetText("\r\n" + TargetSever[1].FullPath + " " + TargetSever[2].FullPath + " " + TargetSever[3].FullPath); SetText("\r\n" + TargetSever[1].IP + "/" + TargetSever[1].Port); SetText("\r\n" + TargetSever[2].IP + "/" + TargetSever[2].Port); SetText("\r\n" + TargetSever[3].IP + "/" + TargetSever[3].Port); Thread uploadThread1 = new Thread(UpLoadFiles); uploadThread1.Start(TargetSever[1]); Thread uploadThread2 = new Thread(UpLoadFiles); uploadThread2.Start(TargetSever[2]); Thread uploadThread3 = new Thread(UpLoadFiles); uploadThread3.Start(TargetSever[3]); uploadThread1.Join(); uploadThread2.Join(); uploadThread3.Join(); DeleteFile(TargetSever[1].FullPath); DeleteFile(TargetSever[2].FullPath); DeleteFile(TargetSever[3].FullPath); SetText("\r\n文件上传完成!"); StauesModify(MonitorServer.FileName, "Ok"); //三个文件全部传输完成则删除文件 } else if (0 == string.Compare(MonitorServer.Cmd, "DownLoad")) { string[] TempExtra = MonitorServer.FileName.Split('.'); TargetSever[1].FileName = TempExtra[0] + "_" + 1.ToString() + "." + TempExtra[1]; TargetSever[2].FileName = TempExtra[0] + "_" + 2.ToString() + "." + TempExtra[1]; TargetSever[3].FileName = TempExtra[0] + "_" + 3.ToString() + "." + TempExtra[1]; SetText("\r\n" + TargetSever[1].FileName + " " + TargetSever[2].FileName + " " + TargetSever[3].FileName); Thread downloadThread1 = new Thread(DownLoadFiles); downloadThread1.Start(TargetSever[1]); Thread downloadThread2 = new Thread(DownLoadFiles); downloadThread2.Start(TargetSever[2]); Thread downloadThread3 = new Thread(DownLoadFiles); downloadThread3.Start(TargetSever[3]); downloadThread1.Join(); downloadThread2.Join(); downloadThread3.Join(); //等待文件接收完成则合并文件 string[] Path_List = { Path.Combine(DownLoadSavePath, TargetSever[1].FileName), Path.Combine(DownLoadSavePath, TargetSever[2].FileName), Path.Combine(DownLoadSavePath, TargetSever[3].FileName) }; CombinFile(Path_List, Path.Combine(DownLoadSavePath, MonitorServer.FileName)); SetText("\r\n文件下载完成!"); StauesModify(MonitorServer.FileName, "Ok"); } } else { //只接收一组IP信息 @192.168.3.32/20000 TargetSever[1].IP = IPstrArray[0]; TargetSever[1].Port = Convert.ToInt32(IPstrArray[1]); SetText(IPstrArray[0]); SetText("/" + IPstrArray[1]); AskSocket.Close();//关闭套接字 if (0 == string.Compare(MonitorServer.Cmd, "UpLoad")) { TargetSever[1].FullPath = MonitorServer.FullPath; Thread uploadThread = new Thread(UpLoadFiles); uploadThread.Start(TargetSever[1]); uploadThread.Join(); SetText("\r\n文件上传完成!"); StauesModify(MonitorServer.FileName, "Ok"); } else if (0 == string.Compare(MonitorServer.Cmd, "DownLoad")) { TargetSever[1].FileName = MonitorServer.FileName; Thread downloadThread = new Thread(DownLoadFiles); downloadThread.Start(TargetSever[1]); downloadThread.Join(); SetText("\r\n文件下载完成!"); StauesModify(MonitorServer.FileName, "Ok"); } } }