첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.Text;
using System.Threading;
using System.Windows.Forms;

using NativeWifi;

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

        #region Field

        /// <summary>
        /// WLAN 클라이언트
        /// </summary>
        private WlanClient wlanClient = new WlanClient();

        /// <summary>
        /// 스레드
        /// </summary>
        private Thread thread;

        #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.IsBackground = true;

            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

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

        #region MAC 주소 구하기 - GetMACAddress(macByteArray)

        /// <summary>
        /// MAC 주소 구하기
        /// </summary>
        /// <param name="macByteArray">MAC 바이트 배열</param>
        /// <returns>MAC 주소</returns>
        private string GetMACAddress(byte[] macByteArray)
        {
            string macString = string.Empty;

            for(int i = 0; i < 6; i++)
            {
                macString += macByteArray[i].ToString("X2") + "-";
            }

            return macString.Substring(0, macString.Length - 1);
        }

        #endregion

        #region SSID 문자열 구하기 - GetSSIDString(wlanAvailableNetwork)

        /// <summary>
        /// SSID 문자열 구하기
        /// </summary>
        /// <param name="wlanAvailableNetwork">WLAN 이용 가능 네트워크</param>
        /// <returns>SSID 문자열</returns>
        private string GetSSIDString(Wlan.WlanAvailableNetwork wlanAvailableNetwork)
        {
            Wlan.Dot11Ssid ssid = wlanAvailableNetwork.dot11Ssid;

            return Encoding.ASCII.GetString(ssid.SSID, 0, (int)ssid.SSIDLength);
        }

        #endregion
        #region 채널 구하기 - GetChannel(wlanAvailableNetwork)

        /// <summary>
        /// 채널 구하기
        /// </summary>
        /// <param name="wlanAvailableNetwork">WLAN 이용 가능 네트워크</param>
        /// <returns>채널</returns>
        private int GetChannel(Wlan.WlanAvailableNetwork wlanAvailableNetwork)
        {
            string name = GetMACAddress(wlanAvailableNetwork.dot11Ssid.SSID);

            Wlan.WlanBssEntry[] wlanBssEntryArray = this.wlanClient.Interfaces[0].GetNetworkBssList();

            foreach(Wlan.WlanBssEntry wlanBssEntry in wlanBssEntryArray)
            {
                if(GetMACAddress(wlanBssEntry.dot11Ssid.SSID) == name)
                {
                    uint chCenterFrequency = wlanBssEntry.chCenterFrequency;

                    switch(chCenterFrequency)
                    {
                        case 2412000 : return 1;
                        case 2417000 : return 2;
                        case 2422000 : return 3;
                        case 2427000 : return 4;
                        case 2432000 : return 5;
                        case 2437000 : return 6;
                        case 2442000 : return 7;
                        case 2447000 : return 8;
                        case 2452000 : return 9;
                        case 2457000 : return 10;
                        case 2462000 : return 11;
                        case 2467000 : return 12;
                        case 2472000 : return 13;
                    }
                }
            }

            return -1;
        }

        #endregion
        #region MAC 주소 구하기 - GetMACAddress(wlanAvailableNetwork)

        /// <summary>
        /// MAC 주소 구하기
        /// </summary>
        /// <param name="wlanAvailableNetwork">WLAN 이용 가능 네트워크</param>
        /// <returns>MAC 주소</returns>
        private string GetMACAddress(Wlan.WlanAvailableNetwork wlanAvailableNetwork)
        {
            string name = GetMACAddress(wlanAvailableNetwork.dot11Ssid.SSID);

            Wlan.WlanBssEntry[] wlanBssEntryArray = this.wlanClient.Interfaces[0].GetNetworkBssList();

            foreach(Wlan.WlanBssEntry wlanBssEntry in wlanBssEntryArray)
            {
                if(GetMACAddress(wlanBssEntry.dot11Ssid.SSID) == name)
                {
                    return GetMACAddress(wlanBssEntry.dot11Bssid);
                }
            }

            return null;
        }

        #endregion

        #region 조회하기 - Search()

        /// <summary>
        /// 조회하기
        /// </summary>
        private void Search()
        {
            while(true)
            {
                this.listView.Items.Clear();

                Wlan.WlanAvailableNetwork[] wlanAvailaleNetworkArray = wlanClient.Interfaces[0].GetAvailableNetworkList(0);

                foreach(Wlan.WlanAvailableNetwork wlanAvailableNetwork in wlanAvailaleNetworkArray)
                {
                    ListViewItem item = new ListViewItem
                    (
                        new string[]
                        { 
                            GetSSIDString(wlanAvailableNetwork),
                            wlanAvailableNetwork.wlanSignalQuality.ToString(), 
                            wlanAvailableNetwork.securityEnabled.ToString(), 
                            GetChannel(wlanAvailableNetwork).ToString(),
                            wlanAvailableNetwork.dot11DefaultCipherAlgorithm.ToString(), 
                            wlanAvailableNetwork.dot11DefaultAuthAlgorithm.ToString(), 
                            GetMACAddress(wlanAvailableNetwork)
                        }
                    );

                    this.listView.Items.Add(item);
                }

                Thread.Sleep(10000);
            }
        }

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

댓글을 달아 주세요