void EndReadStream(IAsyncResult ra)
        {
            socketSendFile = (SocketFileInfo)ra.AsyncState;
            int currentLen = socketSendFile.FileStream.EndRead(ra);

            if (currentLen != 0)
            {
                socketSendFile.CurrentLen    = currentLen;
                socketSendFile.CurrentOffset = socketSendFile.FileStream.Position - currentLen;

                byte[] sendBuff = SocketFileInfo.GetSendByte(socketSendFile);
                ars.Reset();
                socketSendFile.Client.BeginSend(
                    sendBuff,
                    0,
                    sendBuff.Length,
                    SocketFlags.None, null, null);
                ars.WaitOne();
                socketSendFile.FileStream.BeginRead(socketSendFile.CurrentByte, 0, socketSendFile.MaxLength, (AsyncCallback)EndReadStream, socketSendFile);
            }
            else
            {
                socketSendFile.FileStream.Close();
                socketSendFile.FileStream.Dispose();
                socketSendFile.Client.EndReceive(resultIAsync);
                socketSendFile.Client.Close();
            }
        }
        void ProcessMess(ReviceBuffer buff)
        {
            byte[] byt  = buff.ReviceByte;
            string Mess = Encoding.ASCII.GetString(byt).Trim().Trim((char)'\0');

            if (Mess.StartsWith("GetFile ", StringComparison.InvariantCultureIgnoreCase))
            {
                string FileName = Mess.Replace("GetFile ", "").Trim().Trim((char)0);

                SendFile(FileName, buff.Client);
            }
            else if (Mess.Equals("Success", StringComparison.InvariantCultureIgnoreCase))
            {
                ars.Set();
            }
            else if (Mess.Equals("Error", StringComparison.InvariantCultureIgnoreCase))
            {
                byte[] sendBuff = SocketFileInfo.GetSendByte(socketSendFile);
                ars.Reset();
                socketSendFile.Client.BeginSend(
                    sendBuff,
                    0,
                    sendBuff.Length,
                    SocketFlags.None, null, null);
            }
            else if (Mess.Equals("SynchroGetFile ", StringComparison.InvariantCultureIgnoreCase))
            {
                string FileName = Mess.Replace("SynchroGetFile ", "").Trim().Trim((char)0);
            }
        }
예제 #3
0
        void SynchroSendFile(string FileName)
        {
            FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);

            SocketFileInfo socketSendFile = new SocketFileInfo(client, fs, FileName);

            socketSendFile.FileLength = fs.Length;
            int currentLen = fs.Read(socketSendFile.CurrentByte, 0, socketSendFile.MaxLength);

            byte[] MessByte = new byte[1024];

            while (currentLen > 0)
            {
                socketSendFile.CurrentLen    = currentLen;
                socketSendFile.CurrentOffset = socketSendFile.FileStream.Position - currentLen;
                byte[] Buff = SocketFileInfo.GetSendByte(socketSendFile);
                while (true)
                {
                    client.Send(Buff, 0, Buff.Length, SocketFlags.None);
                    client.Receive(MessByte, SocketFlags.None);
                    string Mess = Encoding.ASCII.GetString(MessByte).Trim().Trim((char)'\0');
                    if (Mess.Equals("Success!", StringComparison.InvariantCultureIgnoreCase))
                    {
                        break;
                    }
                }
                currentLen = fs.Read(socketSendFile.CurrentByte, 0, socketSendFile.MaxLength);
            }
            fs.Close();
            fs.Dispose();
            client.Close();
        }
        public void SendFile(string FileName, Socket client)
        {
            FileStream     fs         = new FileStream(FileName, FileMode.Open, FileAccess.Read);
            SocketFileInfo socketFile = new SocketFileInfo(client, fs, FileName);

            socketFile.FileLength = fs.Length;
            fs.BeginRead(socketFile.CurrentByte, 0, socketFile.MaxLength, (AsyncCallback)EndReadStream, socketFile);
        }
예제 #5
0
        public static byte[] GetSendByte(SocketFileInfo socketInfo)
        {
            MemoryStream    ms        = new MemoryStream();
            BinaryFormatter formatter = new BinaryFormatter();

            formatter.Serialize(ms, socketInfo);
            byte[] byt = ms.ToArray();
            ms.Close();
            ms.Dispose();
            byte[] MessLength = BitConverter.GetBytes((uint)byt.Length);
            byte[] SendByte   = new byte[MessLength.Length + byt.Length];
            Array.Copy(MessLength, 0, SendByte, 0, 4);
            Array.Copy(byt, 0, SendByte, 4, byt.Length);

            return(SendByte);
        }
        void SynchroSendFile(string FileName, Socket Client)
        {
            FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);

            byte[]         sendBuff       = new byte[10240];
            int            currentLen     = fs.Read(sendBuff, 0, sendBuff.Length);
            SocketFileInfo socketSendFile = new SocketFileInfo(client, fs, FileName);

            while (currentLen > 0)
            {
                socketSendFile.CurrentLen    = currentLen;
                socketSendFile.CurrentOffset = socketSendFile.FileStream.Position - currentLen;
                byte[] Buff = SocketFileInfo.GetSendByte(socketSendFile);
                Client.Send(Buff, 0, Buff.Length, SocketFlags.None);
                currentLen = fs.Read(sendBuff, 0, sendBuff.Length);
            }
            fs.Close();
            fs.Dispose();
        }
예제 #7
0
        public void DownFile(string FileName)
        {
            SendComand("SynchroGetFile " + FileName);
            byte[]      byt       = new byte[20400];
            int         reviceLen = ClientSocket.Receive(byt);
            SocketError flags;

            while (reviceLen > 0)
            {
                try
                {
                    SocketFileInfo fileInfo = SocketFileInfo.DeSerialize(byt);
                    using (FileStream fs = new FileStream("Synchro" + fileInfo.FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
                    {
                        fs.Position = fileInfo.CurrentOffset;
                        fs.Write(fileInfo.CurrentByte, 0, fileInfo.CurrentLen);
                        fs.Flush();
                        fs.Close();
                        fs.Dispose();
                    }
                    long downSize = fileInfo.CurrentOffset + fileInfo.CurrentLen;
                    int  offset   = (int)(downSize * 100 / fileInfo.FileLength);
                    AsynchronousData.Invoke(downSize, offset);
                    SendComand("Success!");
                    if (downSize == fileInfo.FileLength)
                    {
                        break;
                    }
                }
                catch
                {
                    SendComand("Error!");
                }
                reviceLen = ClientSocket.Receive(byt, 0, byt.Length, SocketFlags.None, out flags);
            }
        }