本文介绍了监控网络流量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想知道是否可以开发一个应用程序,当我通过无线调制解调器访问互联网时,该应用程序可以记录数据使用情况.

我拥有来自私有ISP的无线数据卡.我通过软件包随附的自己的应用程序连接到Internet.允许我以3.1 mbps的速度使用6GB的数据,超过限制后,网络的速度会降低,当然是免费使用的,而我只为那6GB支付租金.

我觉得6GB的数据使用量很快就完成了.我只想要一个工具来监视数据流量.出于兴趣,我想自己开发一个应用程序,我只需要一点指导.你们能帮我吗?

我的想法是每当我使用应用程序连接到互联网时,将接收到的数据和发送的详细信息记录在一个文本文件中.

Hi,
I was wondering if I could develop an application which would make a log for the data usage whenever I access internet through my wireless modem.

I own a wireless datacard from a private ISP. I connect to the internet through their own application which came with the package. I am allowed to use 6GB of data at 3.1 mbps and after exceeding the limit, the speed of the network decreases which of-course comes under free usage and I pay the rental only for that 6GB.

I felt that the 6GB of data usage gets completed quickly. I just want a tool to monitor the data traffic. As a matter of interest I wanted to develop an application by myself and I just need a little guidance. Could you guys kindly help me out?

My idea is to record the data received and sent details in a text file whenever I connect to the internet using their application.

推荐答案

using System;
using System.Runtime;
using System.Runtime.InteropServices;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using Microsoft.Win32;

namespace NetworkTracer
{
    public partial class Tracer : Form
    {
        private Timer _timer;
        private DateTime _startTime = DateTime.MinValue;
        private bool _isNetworkOnline;
        IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
        IPGlobalStatistics ipstat = null;
        Decimal start_r_packets;
        Decimal end_r_packets;
        NetworkInterface[] fNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
        long start_received_bytes;
        long start_sent_bytes;
        long end_received_bytes;
        long end_sent_bytes;

        public Tracer()
        {
            // check if connection exist or not

            InitializeComponent();

            btnMonitor.Enabled = true;
            btnCancel.Enabled = false;
            _timer = new Timer();
            _timer.Tick += new EventHandler(timerTicker);
        }


        protected void startMonitor(object sender, EventArgs e)
        {
            _startTime = DateTime.Now;
            _timer.Start();
            btnMonitor.Enabled = false;
            btnCancel.Enabled = true;
            ipstat = properties.GetIPv4GlobalStatistics();

            getConnectionInfo();
        }

        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
        public static bool IsConnectedToInternet()
        {
            int Desc;
            return InternetGetConnectedState(out Desc, 0);
        }

        protected void getConnectionInfo()
        {
            try
            {
                string myHost = System.Net.Dns.GetHostName();
                IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(myHost);
                IPAddress[] addr = ipEntry.AddressList;
                NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
                _isNetworkOnline = NetworkInterface.GetIsNetworkAvailable();

                if (addr.Length > 0)
                {
                    start_r_packets = Convert.ToDecimal(ipstat.ReceivedPackets);
                    //start_r_packets = Math.Round(start_r_packets / 1048576 * 100000) / 100000;

                    txtboxInfo.Text = "";
                    txtboxInfo.Text += "IP Address: " + addr[addr.Length - 1].ToString() + Environment.NewLine;

                    NetworkInterface adapter = fNetworkInterfaces[0];

                    start_received_bytes = fNetworkInterfaces[0].GetIPv4Statistics().BytesReceived;
                    start_sent_bytes = fNetworkInterfaces[0].GetIPv4Statistics().BytesSent;

                    txtboxInfo.Text += Environment.NewLine + "Name: " + adapter.Name + Environment.NewLine;
                    txtboxInfo.Text += Environment.NewLine + "Description: " + adapter.Description + Environment.NewLine;
                    txtboxInfo.Text += Environment.NewLine + "Network Type: " + adapter.NetworkInterfaceType + Environment.NewLine;
                    txtboxInfo.Text += Environment.NewLine + "Speed: " + adapter.Speed / 1000000 + " (Mbps)" + Environment.NewLine;
                    txtboxInfo.Text += Environment.NewLine + "Operational Status: " + adapter.OperationalStatus + Environment.NewLine;

                    txtboxInfo.Text += Environment.NewLine + "Reeived: " + start_received_bytes.ToString() + Environment.NewLine;
                    txtboxInfo.Text += Environment.NewLine + "Sent: " + start_sent_bytes.ToString() + Environment.NewLine;

                    start_received_bytes = (start_received_bytes / 1048576 * 100000) / 100000;
                    start_sent_bytes = (start_sent_bytes / 1048576 * 100000) / 100000;

                    txtboxInfo.Text += Environment.NewLine + "Reeived (in MB): " + start_received_bytes.ToString() + Environment.NewLine;
                    txtboxInfo.Text += Environment.NewLine + "Sent (in MB): " + start_sent_bytes.ToString() + Environment.NewLine;

                    txtboxInfo.Text += Environment.NewLine + "Is Network Available: " + _isNetworkOnline.ToString() + Environment.NewLine;
                    txtboxInfo.Text += Environment.NewLine + "Starting Received Packets: " + start_r_packets.ToString() + Environment.NewLine;
                    txtboxInfo.Text += Environment.NewLine + "Is Network up: " + System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable().ToString() + Environment.NewLine;
                    txtboxInfo.Text += Environment.NewLine + "New method output: " + IsConnectedToInternet().ToString();
                }

            }
            catch (Exception ex)
            {
                txtboxInfo.Text = "Error!" + Environment.NewLine + ex.Message.ToString();
                _timer.Stop();
                lblTime.Text = "";
                btnCancel.Enabled = false;
                btnMonitor.Enabled = false;
            }
        }

        protected void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            _isNetworkOnline = e.IsAvailable;
        }

        protected void closeEverything(object sender, EventArgs e)
        {
            _timer.Stop();
            btnCancel.Enabled = false;
            btnMonitor.Enabled = true;
            txtboxInfo.Text = lblTime.Text + Environment.NewLine;

            ipstat = properties.GetIPv4GlobalStatistics();
            end_r_packets = Convert.ToDecimal(ipstat.ReceivedPackets);
        //    end_r_packets = Math.Round(end_r_packets / 1048576 * 100000) / 100000;

            txtboxInfo.Text += Environment.NewLine + "Starting Received Packets: " + start_r_packets.ToString() + Environment.NewLine;
            txtboxInfo.Text += Environment.NewLine + "Ending Received Packets: " + end_r_packets.ToString() + Environment.NewLine;
            end_r_packets = end_r_packets - start_r_packets;
            txtboxInfo.Text += Environment.NewLine + "Total Received Packets: " + end_r_packets.ToString() + Environment.NewLine;

            txtboxInfo.Text += Environment.NewLine + "Received Start (in MB): " + start_received_bytes.ToString() + Environment.NewLine;
            txtboxInfo.Text += Environment.NewLine + "Sent Start (in MB): " + start_sent_bytes.ToString() + Environment.NewLine;

            end_received_bytes = fNetworkInterfaces[0].GetIPv4Statistics().BytesReceived;
            end_sent_bytes = fNetworkInterfaces[0].GetIPv4Statistics().BytesSent;

            end_received_bytes = (end_received_bytes / 1048576 * 100000) / 100000;
            end_sent_bytes = (end_sent_bytes / 1048576 * 100000) / 100000;

            txtboxInfo.Text += Environment.NewLine + "Received End (in MB): " + end_received_bytes.ToString() + Environment.NewLine;
            txtboxInfo.Text += Environment.NewLine + "Sent End (in MB): " + end_sent_bytes.ToString() + Environment.NewLine;

            txtboxInfo.Text += Environment.NewLine + "Received Total (in MB): " + (end_received_bytes - start_received_bytes).ToString() + Environment.NewLine;
            txtboxInfo.Text += Environment.NewLine + "Sent Total (in MB): " + (end_sent_bytes - start_sent_bytes).ToString() + Environment.NewLine;

            writetoFile();
        }

        protected void writetoFile()
        {
            string path_file = @"C:\\Documents and Settings\\user2\\Desktop\\Test Tracer.txt";
            string empty_line = "===============================================================";
            if (System.IO.File.Exists(path_file))
            {
                System.IO.File.AppendAllText(path_file, empty_line + Environment.NewLine + "Connection opened: " + _startTime + Environment.NewLine + "Connection Closed: " + DateTime.Now.ToString() + Environment.NewLine + Environment.NewLine + txtboxInfo.Text + Environment.NewLine);
            }
        }

        protected void timerTicker(object sender, EventArgs e)
        {
            var timeSinceStartTime = DateTime.Now - _startTime;
            timeSinceStartTime = new TimeSpan(timeSinceStartTime.Hours,
                                              timeSinceStartTime.Minutes,
                                              timeSinceStartTime.Seconds);
            lblTime.Text = "Time: " + timeSinceStartTime.ToString();
        }
    }
}




这篇关于监控网络流量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 02:25