Exemple #1
0
 public AdapterInfo(string P, string N, NetworkInterface NI, BandwidthCounter In, BandwidthCounter Out, NetworkAdapter na)
 {
     pointer         = P;
     this.deviceName = N;
     ni       = na.InterfaceInformation;
     this.In  = In;
     this.Out = Out;
     this.na  = na;
 }
Exemple #2
0
 public Stats()
 {
     sc = new BandwidthCounter();
     rc = new BandwidthCounter();
 }
Exemple #3
0
 public Stats()
 {
     sc = new BandwidthCounter();
     rc = new BandwidthCounter();
 }
Exemple #4
0
        public void sendFile()
        {
            //BinaryReader bin = null;
            FileInfo file = null;

            // attempt to get read from file
            Console.WriteLine("Attempting to read from file...");
            try
            {
                // get file info
                file = new FileInfo(filePath);

                // only open file again if it has not already been opened
                // if the binaryReader is already set, continue where it left off
                if (bin == null)
                {
                    bin = new BinaryReader(File.OpenRead(filePath));
                }
            }
            catch (Exception error)
            {
                Console.WriteLine("Error reading file: " + error.Message);
                return;
            }

            // attempt to connect
            Console.WriteLine("Attempting to connect...");
            try
            {
                // Create a TCP socket.
                sendingSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //sendingSocket.SendBufferSize = 8192;
                sendingSocket.SendBufferSize = 10240;
                //sendingSocket.SendBufferSize = 65536;

                // Don't allow another socket to bind to this port.
                // Should perhaps consider this one
                //listener.ExclusiveAddressUse = true;

                // Connect the socket to the remote endpoint.
                sendingSocket.Connect(endPoint);
            }
            catch (SocketException socketError)
            {
                Console.WriteLine("Error connecting: " + socketError.Message);

                // cannot connect, attempt to reconnect
                // reconnect every x ms, 1000 ms = 1 sec

                // this method will call itself basically (recursive)
                attemptToReconnect();
                return;
            }
            catch (Exception error)
            {
                Console.WriteLine("Error: " + error.Message);
                return;
            }

            Console.WriteLine(String.Format("Connected to {0}:{1}", endPoint.Address.ToString(), endPoint.Port));

            if (isFirstPacket)
            {
                // send prebuffer
                Console.WriteLine("Calculating md5 hash of file...");
                sendingSocket.Send(getPreBuffer());
            }

            Console.WriteLine("Sending file.");

            // trigger event
            FileTransferEvents.TransferStarted = file.Name;

            // set amount of bytes to be receieved
            totalBytesToBeSent = (ulong)file.Length;

            // start bandwith counter
            if (counter == null)
            {
                totalBytesSent = 0;
                counter        = new BandwidthCounter();
            }

            // start timer to trigger kb/s mb/s progress event
            timer_calc_speed = new System.Timers.Timer()
            {
                Interval = 1000, Enabled = true
            };
            timer_calc_speed.Elapsed += timer_calc_speed_Elapsed;
            timer_calc_speed.Start();

            // create buffer
            //byte[] buff = new byte[2048];
            byte[] buff = new byte[10240];

            // loop through file
            while (totalBytesSent < totalBytesToBeSent)
            {
                try
                {
                    // TODO:
                    // if part of the file has been transfered already
                    // then the connection dropped and then a reconnect happened
                    // make sure the transfer continue where it left off and don't
                    // just start from the beginning again.


                    //sending buff's length.
                    totalBytesSent += (ulong)buff.Length;

                    //p.Sock.Send(bin.ReadBytes(buff.Length));
                    sendingSocket.Send(bin.ReadBytes(buff.Length));

                    // add to counter
                    counter.AddBytes((uint)buff.Length);
                }
                catch (SocketException socketError)
                {
                    Console.WriteLine("Error connecting: " + socketError.Message);
                    attemptToReconnect();

                    return;
                }
                catch (Exception error)
                {
                    Console.WriteLine("Error transfering: " + error.Message);

                    // close file
                    if (bin != null)
                    {
                        bin.Close();
                    }

                    resetConnection();
                    return;
                }
                finally
                {
                }
            }

            // close file
            if (bin != null)
            {
                bin.Close();
            }

            resetConnection();

            FileTransferEvents.Percentage = 100;
            Console.WriteLine("File sent");
        }