Exemple #1
0
        public static void postFile(Socket clientSocket, string sourceFilePath)
        {
            try
            {
                VerifyHandler.postMessage(clientSocket, CommonStaticVariables.messageBeginEncrypted);
                VerifyHandler.verify(clientSocket);

                FileStream fileToSend = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read);  //注意与receive的filestream的区别

                BinaryReader binaryreader = new BinaryReader(fileToSend);
                byte[] fileBytes = new byte[CommonStaticVariables.constSize];
                int count;
                while ((count = binaryreader.Read(fileBytes, 0, CommonStaticVariables.constSize)) != 0)                 //这个注意是将文件写成流的形式
                {
                    byte[] fileFragmentToSend = EncryptionDecryptionHandler.messageEncrypt(fileBytes);
                    clientSocket.Send(fileFragmentToSend, count, SocketFlags.None);           //发送文件流到目标机器
                }

                binaryreader.Close();
                fileToSend.Close();
                System.Threading.Thread.Sleep(200);
                VerifyHandler.postMessage(clientSocket, CommonStaticVariables.messageOverEncrypted);
            }
            catch (Exception ex)
            {
            }
        }
Exemple #2
0
        public static void receiveFile(string fullPath, DateTime lastWriteTime, Socket clientSocket)
        {
            try
            {
                byte[] receiveBytes = new byte[CommonStaticVariables.constSize];
                // 接收从客户端传来的文件
                if (File.Exists(fullPath))
                {
                    File.Delete(fullPath);
                }
                FileStream fileToReceive = new FileStream(fullPath, FileMode.Append, FileAccess.Write);
                BinaryWriter binarywrite = new BinaryWriter(fileToReceive);

                while (true)
                {
                    if (clientSocket.Poll(100, SelectMode.SelectRead))
                    {
                        Array.Clear(receiveBytes, 0, CommonStaticVariables.constSize);
                        int count = clientSocket.Receive(receiveBytes);

                        if (VerifyHandler.verify(receiveBytes, CommonStaticVariables.messageBeginEncrypted, count))
                        {
                            VerifyHandler.postMessage(clientSocket, CommonStaticVariables.messageDoneEncrypted);
                            continue;
                        }
                        else if (VerifyHandler.verify(receiveBytes, CommonStaticVariables.messageOverEncrypted, count))
                        {
                            binarywrite.Flush();
                            binarywrite.Close();
                            fileToReceive.Close();
                            break;
                        }
                        else
                        {
                            //将接收的流用写成文件
                            byte[] fileFragmentToWrite = EncryptionDecryptionHandler.messageDecrypt(receiveBytes);
                            binarywrite.Write(fileFragmentToWrite, 0, count);
                        }
                    }
                    else
                    {
                        if (clientSocket.Poll(100, SelectMode.SelectError))
                        {
                            break;
                        }
                    }
                }
                File.SetLastWriteTime(fullPath, lastWriteTime);
            }
            catch (Exception ex)
            {
            }
        }
Exemple #3
0
        public static void handleFunction(object paraClientSocket)
        {
            Socket curClientSocket = (Socket)paraClientSocket;

            // 当前用户
            byte[]     receiveBytes = new byte[CommonStaticVariables.constSize];
            IPEndPoint tempIPE      = (IPEndPoint)curClientSocket.RemoteEndPoint;
            string     ip           = tempIPE.Address.ToString();
            string     port         = tempIPE.Port.ToString();
            string     account      = null;
            string     password     = null;
            // 当前用户主目录
            string home = CommonStaticVariables.homePath;
            // 当前要操作的对象路径
            string objPath = null;

            // 当前操作对象最近修改时间
            DateTime lastWriteTime = new DateTime();

            // 针对两个参数的函数设计的变量
            string para1      = null;
            string para2      = null;
            string para3      = null;
            string sourcePath = null;
            string destPath   = null;

            bool loop = true;

            try
            {
                while (loop)
                {
                    if (curClientSocket.Poll(100, SelectMode.SelectRead))
                    {
                        Array.Clear(receiveBytes, 0, CommonStaticVariables.constSize);
                        int      successReceiveBytes = curClientSocket.Receive(receiveBytes);
                        string[] parMessage;
                        if ((parMessage = MessageParser.parse(receiveBytes)) != null)
                        {
                            string flag = parMessage[0];
                            switch (flag)
                            {
                            case "login":
                                // 登录验证
                                account  = parMessage[1];
                                password = parMessage[2];
                                if (MessageHandler.login(account, password, ip, port))
                                {
                                    home += account + "\\";
                                    insPromoteDelegate.Invoke("用户接入", account, ip, port);
                                    VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageDoneEncrypted);
                                }
                                else
                                {
                                    insPromoteDelegate.Invoke("用户接入失败", account, ip, port);
                                    VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageFailedEncrypted);
                                }
                                break;

                            case "regist":
                                account  = parMessage[1];
                                password = parMessage[2];
                                if (MessageHandler.registration(account, password, ip, port))
                                {
                                    insPromoteDelegate.Invoke("用户注册", account, ip, port);
                                    VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageDoneEncrypted);
                                }
                                else
                                {
                                    insPromoteDelegate.Invoke("用户注册失败", account, ip, port);
                                    VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageFailedEncrypted);
                                }
                                break;

                            case "logout":
                                if (MessageHandler.logout(account, password))
                                {
                                    insPromoteDelegate.Invoke("用户下线", account, ip, port);

                                    loop = false;
                                    clientSocketList.Remove(curClientSocket);
                                    VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageDoneEncrypted);
                                    curClientSocket.Shutdown(SocketShutdown.Both);
                                    curClientSocket.Close();
                                }
                                else
                                {
                                    insPromoteDelegate.Invoke("用户下线失败", account, ip, port);
                                    VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageFailedEncrypted);
                                }
                                break;

                            case "syncOnstart":
                                VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageDoneEncrypted);
                                if (MessageHandler.syncOnstart(home, curClientSocket))
                                {
                                    insPromoteDelegate.Invoke("开机同步完成", account, ip, port);
                                }
                                else
                                {
                                    insPromoteDelegate.Invoke("开机同步失败", account, ip, port);
                                }
                                break;

                            case "syncNow":
                                VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageDoneEncrypted);
                                if (MessageHandler.syncNow(home, curClientSocket))
                                {
                                    insPromoteDelegate.Invoke("同步完成", account, ip, port);
                                }
                                else
                                {
                                    insPromoteDelegate.Invoke("同步失败", account, ip, port);
                                }
                                break;

                            case "createDirectory":
                                para1         = parMessage[1];
                                para2         = parMessage[2];
                                objPath       = home + para1;
                                lastWriteTime = DateTime.Parse(para2);
                                if (MessageHandler.createDirectory(objPath, lastWriteTime))
                                {
                                    insPromoteDelegate.Invoke("创建目录", account, ip, port);
                                    VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageDoneEncrypted);
                                }
                                else
                                {
                                    insPromoteDelegate.Invoke("创建目录失败", account, ip, port);
                                    VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageFailedEncrypted);
                                }
                                break;

                            case "delete":
                                para1   = parMessage[1];
                                objPath = home + para1;
                                if (MessageHandler.delete(objPath))
                                {
                                    insPromoteDelegate.Invoke("删除项目", account, ip, port);
                                    VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageDoneEncrypted);
                                }
                                else
                                {
                                    insPromoteDelegate.Invoke("删除项目失败", account, ip, port);
                                    VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageFailedEncrypted);
                                }
                                break;

                            case "renameDirectory":
                                para1         = parMessage[1];
                                para2         = parMessage[2];
                                para3         = parMessage[3];
                                sourcePath    = home + para1;
                                destPath      = home + para2;
                                lastWriteTime = DateTime.Parse(para3);
                                if (MessageHandler.renameDirectory(sourcePath, destPath, lastWriteTime))
                                {
                                    insPromoteDelegate.Invoke("重命名目录", account, ip, port);
                                    VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageDoneEncrypted);
                                }
                                else
                                {
                                    insPromoteDelegate.Invoke("重命名目录失败", account, ip, port);
                                    VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageFailedEncrypted);
                                }
                                break;

                            case "modifyFile":
                                para1         = parMessage[1];
                                para2         = parMessage[2];
                                objPath       = home + para1;
                                lastWriteTime = DateTime.Parse(para2);
                                VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageDoneEncrypted);
                                if (MessageHandler.modifyFile(objPath, lastWriteTime, curClientSocket))
                                {
                                    insPromoteDelegate.Invoke("修改文件", account, ip, port);
                                    VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageDoneEncrypted);
                                }
                                else
                                {
                                    insPromoteDelegate.Invoke("修改文件失败", account, ip, port);
                                    VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageFailedEncrypted);
                                }
                                break;

                            case "createFile":
                                para1         = parMessage[1];
                                para2         = parMessage[2];
                                objPath       = home + para1;
                                lastWriteTime = DateTime.Parse(para2);
                                if (MessageHandler.createFile(objPath, lastWriteTime))
                                {
                                    insPromoteDelegate.Invoke("创建文件", account, ip, port);
                                    VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageDoneEncrypted);
                                }
                                else
                                {
                                    insPromoteDelegate.Invoke("创建文件失败", account, ip, port);
                                    VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageFailedEncrypted);
                                }
                                break;

                            case "renameFile":
                                para1         = parMessage[1];
                                para2         = parMessage[2];
                                para3         = parMessage[3];
                                sourcePath    = home + para1;
                                destPath      = home + para2;
                                lastWriteTime = DateTime.Parse(para3);
                                if (MessageHandler.renameFile(sourcePath, destPath, lastWriteTime))
                                {
                                    insPromoteDelegate.Invoke("重命名文件", account, ip, port);
                                    VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageDoneEncrypted);
                                }
                                else
                                {
                                    insPromoteDelegate.Invoke("重命名文件失败", account, ip, port);
                                    VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageFailedEncrypted);
                                }
                                break;

                            default:
                                VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageFailedEncrypted);
                                break;
                            }
                        }
                        else
                        {
                            // 客户端传递非法格式的信息
                            VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageFailedEncrypted);
                        }
                    }
                    else
                    {
                        if (curClientSocket.Poll(100, SelectMode.SelectError))
                        {
                            loop = false;
                            clientSocketList.Remove(curClientSocket);
                            curClientSocket.Shutdown(SocketShutdown.Both);
                            curClientSocket.Close();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is SocketException)
                {
                    MessageHandler.logout(account, password);
                    insPromoteDelegate.Invoke("非正常断开链接", account, ip, port);
                    clientSocketList.Remove(curClientSocket);
                    curClientSocket.Shutdown(SocketShutdown.Both);
                    curClientSocket.Close();
                }
                else
                {
                    VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageFailedEncrypted);
                }
            }
        }