计算机网络作业(二)

姓名:马静 学号:1505060226 班级:软件工程二班

一、实验目的

在windows或linux环境下利用c/c++或java语言获取本机网卡基本信息。并用MFC对话框来显示。应实现的功能如下。
![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)
二、实验内容
实验的源代码如下

// 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 );
        
            //除掉一些没必要的显示  没有找到字串时添加
            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));//设置文字颜色
    // 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);//设置
        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;
}

//详细
void CNetInfoDlg::OnButton2() 
{   
    CString  bigson;
    GetDlgItemText(IDC_COMBO1,bigson);  //读取combo box控件中的内容写入到bigson中
    
    //查询子项并按次序写入
    string son1="IPv4";
    string son2="子网掩码";
    string son3="默认网关";
    string son4="本地链接";
    DealFind(bigson,son1,1);
    DealFind(bigson,son2,2);
    DealFind(bigson,son3,3);
    DealFind(bigson,son4,4);

    // 为恢复做准备
    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)); //设置字体颜色
    }

    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);
        
        //进行选择
        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;
}

//恢复到默认值
void CNetInfoDlg::OnButton5() 
{
    m_edit2.SetWindowText(m_editedit2); 
    m_edit3.SetWindowText(m_editedit3);
    m_edit4.SetWindowText(m_editedit4);
    m_edit5.SetWindowText(m_editedit5);
}

//功能开关,禁止
void CNetInfoDlg::OnButton6() 
{
    GetDlgItem(IDC_EDIT2)->EnableWindow(FALSE);
    GetDlgItem(IDC_EDIT3)->EnableWindow(FALSE);
    GetDlgItem(IDC_EDIT4)->EnableWindow(FALSE);
    GetDlgItem(IDC_EDIT5)->EnableWindow(FALSE);
}

//功能开关,启用
void CNetInfoDlg::OnButton7() 
{
    GetDlgItem(IDC_EDIT2)->EnableWindow(TRUE);
    GetDlgItem(IDC_EDIT3)->EnableWindow(TRUE);
    GetDlgItem(IDC_EDIT4)->EnableWindow(TRUE);
    GetDlgItem(IDC_EDIT5)->EnableWindow(TRUE);
    
}

过程演示图:


nic.gif.gif

实现步骤:
在终端中查看自己的网卡信息,然后新建一个文本文档将网卡信息存入其中

![%KVSWG}Q{93WQ9XQCOFD.png

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

![KHJUVVVKD(TO$YW%KUO_1K.png

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容