计算机网络作业(二)

姓名:马静 学号: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 
#include 
using namespace std;
#include 
#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

你可能感兴趣的:(计算机网络作业(二))