Exemple #1
0
        /// <summary>
        /// Send file back to the server
        /// </summary>
        /// <param name="netStream"></param>
        /// <param name="myMessage"></param>
        private async Task SendFileBack(NetworkStream netStream, ServerClientMessage myMessage)
        {
            byte[] SendingBuffer = null;
            try
            {
                byte[]      dataSend;
                List <byte> byteList = new List <byte>();

                string FileName = @"C:\Users\tomer\OneDrive\Desktop\FileInServer\Hello.bat";

                // If I want to get the file path from my client.
                //string FileName = Encoding.ASCII.GetString(myMessage.MyData);

                FileStream          Fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
                int                 NoOfPackets = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(Fs.Length) / Convert.ToDouble(1024)));
                int                 FileLength = (int)Fs.Length, CurrentPacketLength;
                ServerClientMessage myReply = new ServerClientMessage(MessageType.DownloadAndExe, FileLength);

                // Run on the file and copy it to the messageReply
                // Todo: this is example for handling large requests, maybe we'll aplly later on the netStream writing
                for (int i = 0; i < NoOfPackets; i++)
                {
                    if (FileLength > 1024)
                    {
                        CurrentPacketLength = 1024;
                        FileLength          = FileLength - CurrentPacketLength;
                    }
                    else
                    {
                        CurrentPacketLength = FileLength;
                    }
                    SendingBuffer = new byte[CurrentPacketLength];
                    Fs.Read(SendingBuffer, 0, CurrentPacketLength);
                    byteList.AddRange(SendingBuffer);
                }

                Fs.Close();

                myReply.MyData = byteList.ToArray();
                dataSend       = myReply.serialize();
                await netStream.WriteAsync(dataSend, 0, dataSend.Length);

                await netStream.FlushAsync();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemple #2
0
        /// <summary>
        /// Send file to the server
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public async Task ClientSendFile(string FileName)
        {
            byte[] SendingBuffer = null;
            try
            {
                if (this.myClient.Connected)
                {
                    List <byte> byteList = new List <byte>();
                    byte[]      dataSend;

                    FileStream Fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
                    int        NoOfPackets = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(Fs.Length) / Convert.ToDouble(1024)));
                    int        TotalLength = (int)Fs.Length, CurrentPacketLength;

                    ServerClientMessage myMessage = new ServerClientMessage(MessageType.DownloadAndExe, TotalLength);

                    for (int i = 0; i < NoOfPackets; i++)
                    {
                        if (TotalLength > 1024)
                        {
                            CurrentPacketLength = 1024;
                            TotalLength         = TotalLength - CurrentPacketLength;
                        }
                        else
                        {
                            CurrentPacketLength = TotalLength;
                        }
                        SendingBuffer = new byte[CurrentPacketLength];
                        await Fs.ReadAsync(SendingBuffer, 0, CurrentPacketLength);

                        byteList.AddRange(SendingBuffer);
                    }

                    Fs.Close();
                    myMessage.MyData = byteList.ToArray();

                    dataSend = myMessage.serialize();

                    await this.netStream.WriteAsync(dataSend, 0, dataSend.Length);

                    await this.netStream.FlushAsync();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemple #3
0
        private async Task SendClientRunShell(NetworkStream netStream)
        {
            try
            {
                byte[]      dataSend;
                List <byte> byteList = new List <byte>();

                ServerClientMessage myReply = new ServerClientMessage(MessageType.RunShell, 0, new byte[0]);

                dataSend = myReply.serialize();
                await netStream.WriteAsync(dataSend, 0, dataSend.Length);

                await netStream.FlushAsync();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemple #4
0
        public async Task AskforShell()
        {
            try
            {
                if (this.myClient.Connected)
                {
                    byte[] data = new byte[0];
                    ServerClientMessage myMessage = new ServerClientMessage(MessageType.RunShell, data.Length, data);

                    data = myMessage.serialize();

                    await this.netStream.WriteAsync(data, 0, data.Length);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
            }
        }
Exemple #5
0
        /// <summary>
        /// Ask file from the server
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public async Task AskFileFromServer(string FileName)
        {
            try
            {
                if (this.myClient.Connected)
                {
                    byte[] data = Encoding.ASCII.GetBytes(FileName);
                    ServerClientMessage myMessage = new ServerClientMessage(MessageType.AskForFile, data.Length, data);

                    data = myMessage.serialize();

                    await this.netStream.WriteAsync(data, 0, data.Length);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
            }
        }