Ejemplo n.º 1
0
        public void DeleteFiles(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);
            }
            //获得服务器端节点对象,clientep用于存储对象节点信息,没有其他作用
            IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;

            //发送操作命令
            TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes("Delete"));
            //发送需要删除的文件名
            TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes(connect.FileName));

            client.Close();
            SetText("\r\n文件删除完成!");
        }
Ejemplo n.º 2
0
        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");
            }
        }
Ejemplo n.º 3
0
        public void DataProcess(object Cnt)
        {
            ConnectInfo connect = (ConnectInfo)Cnt;

            if (connect.FullPath != null)
            {
                FileInfo   EzoneFile   = new FileInfo(connect.FullPath);          //创建一个文件对象
                FileStream EzoneStream = EzoneFile.OpenRead();                    //打开文件流

                int PacketSize  = 1024 * 1024;                                    //包的大小1M
                int PacketCount = (int)(EzoneStream.Length / ((long)PacketSize)); //包的数量
                //最后一个包的大小
                int LastDataPacket = (int)(EzoneStream.Length - ((long)(PacketSize * PacketCount)));

                IPEndPoint ipep             = new IPEndPoint(IPAddress.Parse(connect.IP), connect.Port); //指向远程服务端节点
                Socket     UploadfileSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    UploadfileSocket.Connect(ipep);  //连接到发送端
                }
                catch
                {
                    MessageBox.Show("连接服务器出错!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    IsTransferOk = false;
                }
                //获得服务器端节点对象
                IPEndPoint clientep = (IPEndPoint)UploadfileSocket.RemoteEndPoint;
                //发送[操作命名]到服务器端
                TransferFiles.SendVarData(UploadfileSocket, System.Text.Encoding.Unicode.GetBytes("DataProcess"));
                //发送[文件名]到服务器端
                TransferFiles.SendVarData(UploadfileSocket, System.Text.Encoding.Unicode.GetBytes(EzoneFile.Name));
                //发送[包的大小]到服务器端
                TransferFiles.SendVarData(UploadfileSocket, System.Text.Encoding.Unicode.GetBytes(PacketSize.ToString()));
                //发送[包的总数量]到服务器端
                TransferFiles.SendVarData(UploadfileSocket, System.Text.Encoding.Unicode.GetBytes(PacketCount.ToString()));
                //发送[最后一个包的大小]到服务器端
                TransferFiles.SendVarData(UploadfileSocket, 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);
                    //发送数据包
                    if (TransferFiles.SendVarData(UploadfileSocket, data) == 3)
                    {
                        IsTransferOk = true;
                        MessageBox.Show("文件传输出错!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        break;  //可有可无?
                    }
                }

                if (LastDataPacket != 0)  //如果还有多余的数据包,则应该发送完毕!
                {
                    data = new byte[LastDataPacket];
                    EzoneStream.Read(data, 0, data.Length);
                    TransferFiles.SendVarData(UploadfileSocket, data);
                }
                SetText("\r\n数据正在处理,请稍等...");
                UploadfileSocket.Close(); //关闭套接字
                EzoneStream.Close();      //关闭文件流
            }
        }
Ejemplo n.º 4
0
        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");
                }
            }
        }