計(jì)算機(jī)網(wǎng)絡(luò)作業(yè)(二)

姓名:馬靜 學(xué)號(hào):1505060226 班級(jí):軟件工程二班

一搂橙、實(shí)驗(yàn)?zāi)康?/p>

在windows或linux環(huán)境下利用c/c++或java語(yǔ)言獲取本機(jī)網(wǎng)卡基本信息。并用MFC對(duì)話框來(lái)顯示态兴。應(yīng)實(shí)現(xiàn)的功能如下。
![E{I}RJT~9_2`1NOQ5]1243C.png](http://upload-images.jianshu.io/upload_images/7922855-a38e330dc9c5eec6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
二疟位、實(shí)驗(yàn)內(nèi)容
實(shí)驗(yàn)的源代碼如下

// NetInfoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "NetInfo.h"
#include "NetInfoDlg.h"
#include <stdio.h>
#include <string>
using namespace std;
#include <windows.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
    CAboutDlg();

// Dialog Data
    //{{AFX_DATA(CAboutDlg)
    enum { IDD = IDD_ABOUTBOX };
    //}}AFX_DATA

    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CAboutDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL

// Implementation
protected:
    //{{AFX_MSG(CAboutDlg)
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
    //{{AFX_DATA_INIT(CAboutDlg)
    //}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    
    //{{AFX_DATA_MAP(CAboutDlg)
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    //{{AFX_MSG_MAP(CAboutDlg)
        // No message handlers
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNetInfoDlg dialog

CNetInfoDlg::CNetInfoDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CNetInfoDlg::IDD, pParent)
{

    //{{AFX_DATA_INIT(CNetInfoDlg)
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CNetInfoDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CNetInfoDlg)
    DDX_Control(pDX, IDC_STATIC2, m_static3);
    DDX_Control(pDX, IDC_STATIC3, m_static2);
    DDX_Control(pDX, IDC_STATIC1, m_static1);
    DDX_Control(pDX, IDC_EDIT5, m_edit5);
    DDX_Control(pDX, IDC_EDIT4, m_edit4);
    DDX_Control(pDX, IDC_EDIT3, m_edit3);
    DDX_Control(pDX, IDC_EDIT2, m_edit2);
    DDX_Control(pDX, IDC_COMBO1, m_cbControl);
    //}}AFX_DATA_MAP

    system("ipconfig >ip.txt");

    FILE * fp=fopen("ip.txt","r");
    if(fp==NULL){
        MessageBox("gdgdg","ggdgd",1);
        exit(0);
    }


    char strSize[80];
    fgets(strSize,sizeof(strSize),fp);
    int i=0;
    while(!feof(fp)){
        fgets(strSize,sizeof(strSize),fp);
        fgets(strSize,sizeof(strSize),fp);
        string stt(strSize);
        if(strSize[0]!=' '&&strSize[0]!='\n'){
            TCHAR* tdr=strSize;
            string son="Bluetooth";
            string daughter="本地連接";
            string dad(strSize);
            string::size_type idx = dad.find( son );
        
            //除掉一些沒(méi)必要的顯示  沒(méi)有找到字串時(shí)添加
            if ( idx == string::npos ){
                string::size_type idd = dad.find( daughter );
                if ( idd == string::npos ){
                    m_cbControl.AddString(tdr);
                }
            }
        }
    }
    
    fclose(fp);
    delete fp;
//  m_edit2.SetWindowText("");
}

BEGIN_MESSAGE_MAP(CNetInfoDlg, CDialog)
    //{{AFX_MSG_MAP(CNetInfoDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
    ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
    ON_WM_CTLCOLOR()
    ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
    ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
    ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CNetInfoDlg message handlers

BOOL CNetInfoDlg::OnInitDialog()
{
    CDialog::OnInitDialog();
    
//  pDC->SetTextColor(RGB(255,0,0));//設(shè)置文字顏色
    // Add "About..." menu item to system menu.

    // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);

    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        CString strAboutMenu;
        strAboutMenu.LoadString(IDS_ABOUTBOX);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }

    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);         // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon
    
    // TODO: Add extra initialization here
    
    return TRUE;  // return TRUE  unless you set the focus to a control
}

void CNetInfoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
        CAboutDlg dlgAbout;
        dlgAbout.DoModal();
    }
    else
    {
        CDialog::OnSysCommand(nID, lParam);
    }
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CNetInfoDlg::OnPaint() 
{
    if (IsIconic())
    {
        CPaintDC dc(this); // device context for painting

        SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

        // Center icon in client rectangle
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        // Draw the icon
        dc.SetTextColor(    0x0000AA);//設(shè)置
        dc.DrawIcon(x, y, m_hIcon);
        
    }
    else
    {
        CDialog::OnPaint();
    }
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CNetInfoDlg::OnQueryDragIcon()
{
    return (HCURSOR) m_hIcon;
}

//詳細(xì)
void CNetInfoDlg::OnButton2() 
{   
    CString  bigson;
    GetDlgItemText(IDC_COMBO1,bigson);  //讀取combo box控件中的內(nèi)容寫(xiě)入到bigson中
    
    //查詢子項(xiàng)并按次序?qū)懭?    string son1="IPv4";
    string son2="子網(wǎng)掩碼";
    string son3="默認(rèn)網(wǎng)關(guān)";
    string son4="本地鏈接";
    DealFind(bigson,son1,1);
    DealFind(bigson,son2,2);
    DealFind(bigson,son3,3);
    DealFind(bigson,son4,4);

    // 為恢復(fù)做準(zhǔn)備
    GetDlgItemText(IDC_EDIT2,m_editedit2);
    GetDlgItemText(IDC_EDIT3,m_editedit3);
    GetDlgItemText(IDC_EDIT4,m_editedit4);
    GetDlgItemText(IDC_EDIT5,m_editedit5);
//  MessageBox(m_editedit2,"OKOK",1);
}

//保存
void CNetInfoDlg::OnButton4() 
{
    CString str1,str2,str3,str4;
    GetDlgItemText(IDC_EDIT2,str1);
    GetDlgItemText(IDC_EDIT3,str2);
    GetDlgItemText(IDC_EDIT4,str3);

    GetDlgItemText(IDC_EDIT5,str4);
    if(str1==str2||str3==str4){
        MessageBox("保存失斦叭蟆!","保存",MB_OK);
    }
    else{
        MessageBox("保存成功甜刻!","保存",MB_OK);
    }
}

//顏色控制
HBRUSH CNetInfoDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    
    // TODO: Change any attributes of the DC here
    if(pWnd->GetDlgCtrlID()==IDC_STATIC1 || pWnd->GetDlgCtrlID()==IDC_STATIC2
        || pWnd->GetDlgCtrlID()==IDC_STATIC3 ){
        pDC->SetTextColor(RGB(156,84,14)); //設(shè)置字體顏色
    }

    return hbr;
}

void CNetInfoDlg::DealFind(CString bigson,string son,int num){
    
    //讀取文件
    FILE * fp=fopen("ip.txt","r");
    if(fp==NULL){
        exit(0);
    }

    char strSize[80];
    fgets(strSize,sizeof(strSize),fp);
    int i=0;
    bool flag=false;

    while(!feof(fp)){
        fgets(strSize,sizeof(strSize),fp);
        fgets(strSize,sizeof(strSize),fp);

        //查詢獲取IP
        string dad(strSize);
    
        const char* cc=son.c_str();
        
        string::size_type res1 = dad.find(bigson);
        
        //進(jìn)行選擇
        if ( res1  != string::npos ){
            flag = true;
            while(!feof(fp)){
                string grpa(strSize);
                
                string::size_type res2 = grpa.find(son);
                if ( res2  != string::npos ){
                    char rst[40]="";
                    int t=0;
                    int i = grpa.find(":");
                    while(true){
                        rst[t] = grpa[++i];
                        if(rst[t]=='\n' || rst=='\0'){
                            rst[t]='\0';
                            break;
                        }
                        t++;
                    }
                    if(num==1){
                        m_edit2.SetWindowText(rst);
                    }
                    if(num==2){
                        m_edit3.SetWindowText(rst);
                    }
                    if(num==3){
                        m_edit4.SetWindowText(rst);
                    }
                    if(num==4){
                        m_edit5.SetWindowText(rst);
                    }
                    break;
                
                }
                fgets(strSize,sizeof(strSize),fp);
            }
        }
    }
    fclose(fp);
    delete fp;
}

//恢復(fù)到默認(rèn)值
void CNetInfoDlg::OnButton5() 
{
    m_edit2.SetWindowText(m_editedit2); 
    m_edit3.SetWindowText(m_editedit3);
    m_edit4.SetWindowText(m_editedit4);
    m_edit5.SetWindowText(m_editedit5);
}

//功能開(kāi)關(guān)绍撞,禁止
void CNetInfoDlg::OnButton6() 
{
    GetDlgItem(IDC_EDIT2)->EnableWindow(FALSE);
    GetDlgItem(IDC_EDIT3)->EnableWindow(FALSE);
    GetDlgItem(IDC_EDIT4)->EnableWindow(FALSE);
    GetDlgItem(IDC_EDIT5)->EnableWindow(FALSE);
}

//功能開(kāi)關(guān),啟用
void CNetInfoDlg::OnButton7() 
{
    GetDlgItem(IDC_EDIT2)->EnableWindow(TRUE);
    GetDlgItem(IDC_EDIT3)->EnableWindow(TRUE);
    GetDlgItem(IDC_EDIT4)->EnableWindow(TRUE);
    GetDlgItem(IDC_EDIT5)->EnableWindow(TRUE);
    
}

過(guò)程演示圖:


nic.gif.gif

實(shí)現(xiàn)步驟:
在終端中查看自己的網(wǎng)卡信息得院,然后新建一個(gè)文本文檔將網(wǎng)卡信息存入其中

![%KVSWG}Q{93WQ9XQCOFD.png

![9LOJA3@SOV(7J5$IO3VGPH.png

![KHJUVVVKD(TO$YW%KUO_1K.png

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末傻铣,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子祥绞,更是在濱河造成了極大的恐慌非洲,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,406評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蜕径,死亡現(xiàn)場(chǎng)離奇詭異两踏,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)兜喻,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門(mén)梦染,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事帕识》捍猓” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,711評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵肮疗,是天一觀的道長(zhǎng)晶姊。 經(jīng)常有香客問(wèn)我,道長(zhǎng)族吻,這世上最難降的妖魔是什么帽借? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,380評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮超歌,結(jié)果婚禮上砍艾,老公的妹妹穿的比我還像新娘。我一直安慰自己巍举,他們只是感情好脆荷,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著懊悯,像睡著了一般蜓谋。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上炭分,一...
    開(kāi)封第一講書(shū)人閱讀 51,301評(píng)論 1 301
  • 那天桃焕,我揣著相機(jī)與錄音,去河邊找鬼捧毛。 笑死观堂,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的呀忧。 我是一名探鬼主播师痕,決...
    沈念sama閱讀 40,145評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼而账!你這毒婦竟也來(lái)了胰坟?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,008評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤笔横,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后咐吼,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,443評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡汽烦,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評(píng)論 3 334
  • 正文 我和宋清朗相戀三年涛菠,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了俗冻。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片迄薄。...
    茶點(diǎn)故事閱讀 39,795評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡涣易,死狀恐怖冶伞,靈堂內(nèi)的尸體忽然破棺而出响禽,到底是詐尸還是另有隱情隆嗅,我是刑警寧澤,帶...
    沈念sama閱讀 35,501評(píng)論 5 345
  • 正文 年R本政府宣布侯繁,位于F島的核電站胖喳,受9級(jí)特大地震影響贮竟,放射性物質(zhì)發(fā)生泄漏禀晓。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評(píng)論 3 328
  • 文/蒙蒙 一坝锰、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧重付,春花似錦顷级、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,731評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至删掀,卻和暖如春翔冀,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背披泪。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,865評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工纤子, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,899評(píng)論 2 370
  • 正文 我出身青樓控硼,卻偏偏與公主長(zhǎng)得像泽论,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子卡乾,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容