첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.Net.NetworkInformation;
using System.Text;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 타임 아웃
        /// </summary>
        private const int TIME_OUT = 120;

        /// <summary>
        /// PING
        /// </summary>
        private Ping ping = new Ping();

        /// <summary>
        /// PING 옵션
        /// </summary>
        private PingOptions pingOptions = new PingOptions();

        /// <summary>
        /// 데이터
        /// </summary>
        private string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

        #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.timeComboBox.Text = "1초";
        }

        #endregion
        #region IP 주소 리스트 박스 선택 인덱스 변경시 처리하기 - ipAddressListBox_SelectedIndexChanged(sender, e)

        /// <summary>
        /// IP 주소 리스트 박스 선택 인덱스 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void ipAddressListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.ipAddressListBox.SelectedItems.Count > 0)
            {
                this.startButton.Enabled = true;
            }
        }

        #endregion
        #region 추가 버튼 클릭시 처리하기 - addButton_Click(sender, e)

        /// <summary>
        /// 추가 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void addButton_Click(object sender, EventArgs e)
        {
            this.ipAddressListBox.Items.Add(this.ipAddressTextBox.Text);
        }

        #endregion
        #region 시간 콤보 박스 선택 인덱스 변경시 처리하기 - timeComboBox_SelectedIndexChanged(sender, e)

        /// <summary>
        /// 시간 콤보 박스 선택 인덱스 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void timeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch(this.timeComboBox.Text)
            {
                case "1초" : this.timer.Interval = 1000; break;
                case "2초" : this.timer.Interval = 2000; break; 
                case "3초" : this.timer.Interval = 3000; break;
                case "4초" : this.timer.Interval = 4000; break;
                case "5초" : this.timer.Interval = 5000; break;
            }
        }

        #endregion
        #region 시작 버튼 클릭시 처리하기 - startButton_Click(sender, e)

        /// <summary>
        /// 시작 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void startButton_Click(object sender, EventArgs e)
        {
            if(this.ipAddressListBox.SelectedItems.Count == 0)
            {
                MessageBox.Show("조회할 IP 주소를 선택해 주시기 바랍니다.", "INFORMATION", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return;
            }

            if(this.startButton.Text == "시작")
            {
                this.timer.Enabled = true;

                this.ipAddressListBox.Enabled = false;

                this.startButton.Text = "중지";
            }
            else
            {
                this.timer.Enabled = false;

                this.ipAddressListBox.Enabled = true;

                this.startButton.Text = "시작";
            }
        }

        #endregion
        #region 타이머 틱 처리하기 - timer_Tick(sender, e)

        /// <summary>
        /// 타이머 틱 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void timer_Tick(object sender, EventArgs e)
        {
            SendPingMessage();
        }

        #endregion

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

        #region PING 메시지 송신하기 - SendPingMessage()

        /// <summary>
        /// PING 메시지 송신하기
        /// </summary>
        private void SendPingMessage()
        {
            try 
            {
                Byte[] bufferArray = Encoding.ASCII.GetBytes(data);

                this.pingOptions.DontFragment = true;

                foreach(object item in this.ipAddressListBox.SelectedItems)
                {
                    PingReply pingReply = this.ping.Send(item.ToString(), TIME_OUT, bufferArray, this.pingOptions);

                    string result = null;

                    if(pingReply.Status == IPStatus.Success)
                    {
                        result = string.Format("[{0}] Replied from {1} : Bytes={2}, RoundTripTime={3}, TTL={4}",
                            DateTime.Now.ToString("HH:mm:ss"), item, pingReply.Buffer.Length, pingReply.RoundtripTime, pingReply.Options.Ttl);

                        this.resultListBox.Items.Add(result);
                    }
                    else
                    {
                        result = string.Format("[{0}] Failed to ping {1}.", DateTime.Now.ToString("HH:mm:ss"), item.ToString());

                        this.resultListBox.Items.Add(result);
                    }
                }

                this.resultListBox.Items.Add(string.Empty);
            }
            catch(Exception exception)
            {
                MessageBox.Show("네트워크 오류가 발생했습니다. :\n" + exception.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요