첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ 네트워크 상태 조회하기

----------------------------------------------------------------------------------------------------


TestProject.zip


MainForm.cs

 

 

using System;

using System.Drawing;

using System.IO;

using System.Net.NetworkInformation;

using System.Threading;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// IP 글로벌 속성

        /// </summary>

        private IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();

 

        /// <summary>

        /// 스레드

        /// </summary>

        private Thread thread = null;

 

        /// <summary>

        /// 지역 포트

        /// </summary>

        private string localPort;

        

        /// <summary>

        /// 원격 주소

        /// </summary>

        private string remoteAddress;

        

        /// <summary>

        /// 원격 포트

        /// </summary>

        private string remotePort;

 

        /// <summary>

        /// 체크 여부

        /// </summary>

        private bool isChecked = true;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

 

        /// <summary>

        /// 폼 로드시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Form_Load(object sender, EventArgs e)

        {

            this.thread = new Thread(Search);

 

            this.thread.Start();

        }

 

        #endregion

        #region 폼 닫을 경우 처리하기 - Form_FormClosing(sender, e)

 

        /// <summary>

        /// 폼 닫을 경우 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Form_FormClosing(object sender, FormClosingEventArgs e)

        {

            if(this.thread != null)

            {

                this.thread.Abort();

            }

 

            Application.ExitThread();

        }

 

        #endregion

 

        #region 필터 적용 버튼 클릭시 처리하기 - filterButton_Click(sender, e)

 

        /// <summary>

        /// 필터 적용 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void filterButton_Click(object sender, EventArgs e)

        {

            this.localPort     = this.localPortTextBox.Text;

            this.remoteAddress = this.remoteAddressTextBox.Text;

            this.remotePort    = this.remotePortTextBox.Text;

 

            SetEnabled();

        }

 

        #endregion

        #region 저장 버튼 클릭시 처리하기 - saveButton_Click(sender, e)

 

        /// <summary>

        /// 저장 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void saveButton_Click(object sender, EventArgs e)

        {

            if(this.saveFileDialog.ShowDialog() == DialogResult.OK)

            {

                StreamWriter writer = new StreamWriter(this.saveFileDialog.FileName);

 

                writer.WriteLine("파일생성 : " +  DateTime.Now);

                writer.WriteLine();

                writer.WriteLine("로컬주소 로컬포트 외부주소 외부포트 상태");

 

                for(int i =0 ; i < this.listView.Items.Count -1 ; i++)

                {

                    writer.WriteLine

                    (

                        this.listView.Items[i].SubItems[0].Text + " " +

                        this.listView.Items[i].SubItems[1].Text + " " +

                        this.listView.Items[i].SubItems[2].Text + " " +

                        this.listView.Items[i].SubItems[3].Text + " " +

                        this.listView.Items[i].SubItems[4].Text

                    );

                }

 

                writer.WriteLine();

                writer.WriteLine("파일생성 종료 : " + DateTime.Now);

 

                writer.Close();

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 활성화 여부 설정하기 - SetEnabled()

 

        /// <summary>

        /// 활성화 여부 설정하기

        /// </summary>

        private void SetEnabled()

        {

            if(this.isChecked)

            {

                this.localPortTextBox.Enabled     = false;

                this.remotePortTextBox.Enabled    = false;

                this.remoteAddressTextBox.Enabled = false;

                this.filterButton.Enabled         = false;

                this.saveButton.Enabled           = false;

            }

            else

            {

                this.localPortTextBox.Enabled     = true;

                this.remotePortTextBox.Enabled    = true;

                this.remoteAddressTextBox.Enabled = true;

                this.filterButton.Enabled         = true;

                this.saveButton.Enabled           = true;

            }

        }

 

        #endregion

        #region 조회하기 - Search()

 

        /// <summary>

        /// 조회하기

        /// </summary>

        private void Search()

        {

            while(true)

            {

                this.isChecked = true;

 

                SetEnabled();

 

                this.listView.Items.Clear();

 

                TcpConnectionInformation[] tcpConnectionInformationArray = this.ipGlobalProperties.GetActiveTcpConnections();

 

                int index = 0;

 

                foreach(TcpConnectionInformation tcpConnectionInformation in tcpConnectionInformationArray)

                {

                    this.listView.Items.Add(tcpConnectionInformation.LocalEndPoint.Address.ToString());

 

                    this.listView.Items[index].SubItems.Add(tcpConnectionInformation.LocalEndPoint.Port.ToString()    );

                    this.listView.Items[index].SubItems.Add(tcpConnectionInformation.RemoteEndPoint.Address.ToString());

                    this.listView.Items[index].SubItems.Add(tcpConnectionInformation.RemoteEndPoint.Port.ToString()   );

                    this.listView.Items[index].SubItems.Add(tcpConnectionInformation.State.ToString()                 );

 

                    if(tcpConnectionInformation.LocalEndPoint.Port.ToString() == this.localPort)

                    {

                        this.listView.Items[index].SubItems[0].BackColor = Color.GreenYellow;

                    }

 

                    if(tcpConnectionInformation.RemoteEndPoint.Address.ToString() == this.remoteAddress)

                    {

                        this.listView.Items[index].SubItems[0].BackColor = Color.LightPink;

                    }

 

                    if(tcpConnectionInformation.RemoteEndPoint.Port.ToString() == this.remotePort)

                    {

                        this.listView.Items[index].SubItems[0].BackColor = Color.Aqua;

                    }

 

                    index++;

                }

 

                this.isChecked = false;

 

                SetEnabled();

 

                Thread.Sleep(30000);

            }

        }

 

        #endregion

    }

}

 

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요