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)
            {
            }
        }
Beispiel #2
0
        public static byte[] syncNow()
        {
            byte flag = Convert.ToByte(CommonStaticVariables.messageMap["syncNow"]);

            byte[] temp = new byte[3];
            temp[0] = flag;
            temp[1] = 1;
            temp[2] = 0;
            return(EncryptionDecryptionHandler.messageEncrypt(temp));
        }
Beispiel #3
0
        public static byte[] assembleDone()
        {
            byte flag = 2;

            byte[] temp = new byte[4];
            temp[0] = flag;
            temp[1] = 2;
            temp[2] = 0;
            temp[3] = 0;
            return(EncryptionDecryptionHandler.messageEncrypt(temp));
        }
        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)
            {
            }
        }
Beispiel #5
0
        // 对消息的解析
        public static string[] parse(byte[] messageByteEncrypted)
        {
            try
            {
                int i;

                byte[] messageByte = EncryptionDecryptionHandler.messageDecrypt(messageByteEncrypted);

                // 消息类型
                int flagIndex = messageByte[0];
                // 消息段数
                int    segmentCount = messageByte[1];
                string flag         = CommonStaticVariables.messageMap[(object)flagIndex].ToString();

                // 每个段的长度
                int[] eachSize = new int[segmentCount];
                // 每个段的内容
                string[] parameter = new string[segmentCount];
                // 当前段的起始位置
                int curStart = 2 + segmentCount;
                for (i = 0; i < segmentCount; ++i)
                {
                    if (i == 0)
                    {
                        eachSize[i] = messageByte[2 + i];
                        byte[] temp = new byte[eachSize[i]];
                        Array.Copy(messageByte, curStart, temp, 0, eachSize[i]);
                        parameter[i] = Encoding.UTF8.GetString(temp);
                    }
                    else
                    {
                        eachSize[i] = messageByte[2 + i];
                        curStart   += eachSize[i - 1];
                        byte[] temp = new byte[eachSize[i]];
                        Array.Copy(messageByte, curStart, temp, 0, eachSize[i]);
                        parameter[i] = Encoding.UTF8.GetString(temp);
                    }
                }

                string[] returnString = new string[1 + segmentCount];
                returnString[0] = flag;
                for (i = 0; i < segmentCount; ++i)
                {
                    returnString[i + 1] = parameter[i];
                }
                return(returnString);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Beispiel #6
0
        public static byte[] commonSingleFunction(byte flag, string para)
        {
            byte segmentCount = 1;

            byte[] paraByte     = Encoding.UTF8.GetBytes(para);
            byte   lengthOfPara = Convert.ToByte(paraByte.Length);
            int    lengthOfMsg  = 3 + lengthOfPara;

            byte[] message = new byte[lengthOfMsg];
            message[0] = flag;
            message[1] = segmentCount;
            message[2] = lengthOfPara;
            Array.Copy(paraByte, 0, message, 3, lengthOfPara);
            return(EncryptionDecryptionHandler.messageEncrypt(message));
        }
Beispiel #7
0
        public static byte[] commonFunction(byte flag, string path, string lastWriteTime)
        {
            byte segmentCount = 2;

            byte[] para1Byte     = Encoding.UTF8.GetBytes(path);
            byte[] para2Byte     = Encoding.UTF8.GetBytes(lastWriteTime);
            byte   lengthOfPara1 = Convert.ToByte(para1Byte.Length);
            byte   lengthOfPara2 = Convert.ToByte(para2Byte.Length);
            int    lengthOfMsg   = 4 + lengthOfPara1 + lengthOfPara2;

            byte[] message = new byte[lengthOfMsg];
            message[0] = flag;
            message[1] = segmentCount;
            message[2] = lengthOfPara1;
            message[3] = lengthOfPara2;
            Array.Copy(para1Byte, 0, message, 4, lengthOfPara1);
            Array.Copy(para2Byte, 0, message, 4 + lengthOfPara1, lengthOfPara2);
            return(EncryptionDecryptionHandler.messageEncrypt(message));
        }
Beispiel #8
0
        public static byte[] commonTripleFunction(byte flag, string para1, string para2, string para3)
        {
            byte segmentCount = 3;

            byte[] para1Byte     = Encoding.UTF8.GetBytes(para1);
            byte[] para2Byte     = Encoding.UTF8.GetBytes(para2);
            byte[] para3Byte     = Encoding.UTF8.GetBytes(para3);
            byte   lengthOfPara1 = Convert.ToByte(para1Byte.Length);
            byte   lengthOfPara2 = Convert.ToByte(para2Byte.Length);
            byte   lengthOfPara3 = Convert.ToByte(para3Byte.Length);
            int    lengthOfMsg   = 5 + lengthOfPara1 + lengthOfPara2 + lengthOfPara3;

            byte[] message = new byte[lengthOfMsg];
            message[0] = flag;
            message[1] = segmentCount;
            message[2] = lengthOfPara1;
            message[3] = lengthOfPara2;
            message[4] = lengthOfPara3;
            Array.Copy(para1Byte, 0, message, 5, lengthOfPara1);
            Array.Copy(para2Byte, 0, message, 5 + lengthOfPara1, lengthOfPara2);
            Array.Copy(para3Byte, 0, message, 5 + lengthOfPara1 + lengthOfPara2, lengthOfPara3);
            return(EncryptionDecryptionHandler.messageEncrypt(message));
        }