问题将在评论中提出 [英] The question will be asked in the comments

查看:49
本文介绍了问题将在评论中提出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码是对我之前的问题的一点改进..http://www.codeproject.com/Questions/448236/Monitoring-Network-Traffic

问题是,通过数据卡建立连接后(即当我使用调制解调器连接到Internet时),我无法获得正确数量的发送和接收的数据包..

请帮帮我..

就像在控制面板下的网络连接中,状态显示了很多信息一样;如何在我的应用程序中获取它?



This code is a little progress to my previous question .. http://www.codeproject.com/Questions/448236/Monitoring-Network-Traffic

The problem is, I was not able to get the correct number of packets sent and received when the connection is established though my datacard (i.e. when I am connected to the internet usnig a modem)..

Kindly help me out..

Just like, in the network connections under control panel, the status shows so many info; how do I get it in my 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;

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;

        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;
                    //System.Net.NetworkInformation.
                    txtboxInfo.Text = "";
                    txtboxInfo.Text += "IP Address: " + addr[addr.Length - 1].ToString() + Environment.NewLine;
                    txtboxInfo.Text += Environment.NewLine + "Is Network Available: " + _isNetworkOnline.ToString() + Environment.NewLine;                    
                    txtboxInfo.Text += Environment.NewLine + "Starting Received Packets (in mb): " + 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 (in mb): " + start_r_packets.ToString() + Environment.NewLine;
            txtboxInfo.Text += Environment.NewLine + "Ending Received Packets (in mb): " + end_r_packets.ToString() + Environment.NewLine;
            end_r_packets = end_r_packets - start_r_packets;
            txtboxInfo.Text += Environment.NewLine + "Total Received Packets (in mb): " + end_r_packets.ToString() + Environment.NewLine;

            writetoFile();
        }

        protected void writetoFile()
        {
            string path_file = @"C:\\Documents and Settings\\user\\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();
        }
    }
}

推荐答案

选中此项.
将Winpcap函数移植到.NET库的数据包嗅探 [ ^ ]
Check this.
Packet Sniffing with Winpcap Functions Ported to a .NET Library[^]


我发现这很有用.

http://social.msdn.microsoft.com/论坛/en-US/csharplanguage/thread/c3f3355f-b134-41fa-b93f-bb2b7f9c5855 [
I found this useful.

http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/c3f3355f-b134-41fa-b93f-bb2b7f9c5855[^]


如果您准备好研究现有的想法,请执行以下操作:

SharpPcap-.NET的数据包捕获框架 [ ^ ]

P调用了开源WinPCap,并创建了SharpPCap.尝试看看您是否可以获得任何好处或使您的代码更有效.
If you are ready to look at existing ideas:

SharpPcap - A Packet Capture Framework for .NET[^]

Open Source WinPCap is PInvoked and created SharpPCap. Try to see if you can gain anything or make your code more efficient.


这篇关于问题将在评论中提出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆