www.pudn.com > mysecP.rar > mysecDlg.cpp
// mysecDlg.cpp : implementation file // #include "stdafx.h" #include "mysec.h" #include "mysecDlg.h" #include#include "Winio.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif const UINT WM_ICON_NOTIFY= WM_USER+10; ///////////////////////////////////////////////////////////////////////////// // 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() ///////////////////////////////////////////////////////////////////////////// // CMysecDlg dialog CMysecDlg::CMysecDlg(CWnd* pParent /*=NULL*/) : CDialog(CMysecDlg::IDD, pParent) { //{{AFX_DATA_INIT(CMysecDlg) //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); select=1; } CMysecDlg::~CMysecDlg() { send(clisock,"Disconnected",MAXBUFFERSIZE,0); } void CMysecDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CMysecDlg) DDX_Control(pDX, IDC_EDIT2, m_edit2); DDX_Control(pDX, IDC_LIST1, m_list); DDX_Control(pDX, IDC_EDIT1, m_edit); DDX_Control(pDX, IDC_BUTTON1, m_button1); DDX_Control(pDX, IDC_BUTTON3, m_disconnect); DDX_Control(pDX, IDC_BUTTON2, m_connect); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CMysecDlg, CDialog) //{{AFX_MSG_MAP(CMysecDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON2, OnButton2) ON_BN_CLICKED(IDC_BUTTON3, OnButton3) ON_BN_CLICKED(IDC_BUTTON1, OnButton1) ON_EN_CHANGE(IDC_EDIT2, OnChangeEdit2) ON_WM_TIMER() ON_WM_CLOSE() ON_COMMAND(ID_APP_EXIT, OnAppExit) ON_COMMAND(ID_MENUSHOW, OnMenushow) ON_BN_CLICKED(IDC_ADDRESSTABLE, OnAddresstable) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMysecDlg message handlers BOOL CMysecDlg::OnInitDialog() { CDialog::OnInitDialog(); // 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); ReadConfig(); 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 if (!m_TrayIcon.Create(NULL, // Parent window WM_ICON_NOTIFY, // Icon notify message to use _T("监测数据接收端"), // tooltip AfxGetApp()->LoadIcon(IDI_OPEN), // Icon to use IDR_POPUP_MENU)) // ID of tray icon return -1; m_message="等待......"; // TODO: Add extra initialization here m_edit.SetLimitText(99); m_list.InsertColumn(0,"消息"); m_list.SetColumnWidth(0,435); m_button1.EnableWindow(FALSE); m_disconnect.EnableWindow(FALSE); m_edit2.SetWindowText(server); count=0; m_WriteData.SetOnTrueAddress(TRUE); if(InitializeWinIo()) { m_edit.SetWindowText("初始化成功"); m_message="初始化成功,等待......";//保险起见,初始化两次,第一次 } Sleep(2000); m_WriteData.init();//初始化,第二次InitializeWinIo GetaAddressTable(); OnButton2(); return TRUE; // return TRUE unless you set the focus to a control } void CMysecDlg::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 CMysecDlg::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.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 CMysecDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CMysecDlg::OnOK() { // TODO: Add extra validation here CDialog::OnOK(); } void CMysecDlg::OnCancel() { // TODO: Add extra cleanup here this->ShowWindow(0); // CDialog::OnCancel(); } void CMysecDlg::OnButton2() //连接 { // TODO: Add your control notification handler code here char ipaddress[35]; m_edit2.GetWindowText(ipaddress,30); cli.sin_addr.s_addr=inet_addr(ipaddress); cli.sin_family=AF_INET; cli.sin_port=sin_port;//htons(5000); //创建socket clisock=socket(AF_INET,SOCK_STREAM,0); ee=1; //启动线程 AfxBeginThread(thread,0); } void CMysecDlg::OnButton3() { // TODO: Add your control notification handler code here ee=0; closesocket(clisock); m_message="等待......"; } void CMysecDlg::OnButton1() { // TODO: Add your control notification handler code here char buff[MAXBUFFERSIZE]; CSize size; size.cx=0; size.cy=30; //获得发送信息 m_edit.GetWindowText(buff,99); m_edit.SetWindowText(""); m_list.InsertItem(count++,buff); m_list.Scroll(size); //发送数据 send(clisock,buff,MAXBUFFERSIZE,0); } //线程 UINT thread(LPVOID v) { char buff[MAXBUFFERSIZE]; char array[25][30]= {"155.245.160.151", "155.245.160.152", "155.245.160.153", "155.245.160.154", "155.245.160.155", "155.245.160.156", "155.245.160.157", "155.245.160.158", "155.245.160.159", "155.245.160.160", "155.245.160.161", "155.245.160.162", "155.245.160.163", "155.245.160.164", "155.245.160.165", "155.245.160.166", "155.245.160.167", "155.245.160.168", "155.245.160.169", "155.245.160.170", "155.245.160.171", "155.245.160.172", "155.245.160.173", "155.245.160.174", "155.245.160.175"}; CSize size; size.cx=0; size.cy=30; int s=1,addcount=0; CMysecDlg *dlg=(CMysecDlg*) AfxGetApp()->GetMainWnd(); dlg->m_connect.EnableWindow(FALSE); dlg->m_disconnect.EnableWindow(TRUE); //连接到服务器 dlg->SetState(0); while(connect(dlg->clisock,(sockaddr*)&(dlg->cli),sizeof(dlg->cli)) && dlg->ee!=0) { dlg->m_edit.SetWindowText(dlg->m_message); //空循环 for (int i=0;i<=65000;i++) for(int j=0;j<=200;j++); if (addcount==25) addcount=0; // dlg->cli.sin_addr.s_addr=inet_addr(array[addcount++]); } if (dlg->ee==1) { // dlg->m_list.InsertItem(dlg->count++,"连接成功"); dlg->m_edit.SetWindowText("连接成功"); dlg->m_button1.EnableWindow(TRUE); dlg->SetForegroundWindow(); dlg->SetHideTimer(); dlg->SetState(1); } //循环获得数据 unsigned short m_data[2100]; while(s!=SOCKET_ERROR && dlg->ee!=0) { //调用recv函数接收数据 // dlg->m_edit.SetWindowText("接收1"); s=recv(dlg->clisock,buff,MAXBUFFERSIZE,0); dlg->SetForegroundWindow(); dlg->SetRun(); // if (s!=SOCKET_ERROR && dlg->ee!=0) dlg->GetData(buff); // dlg->m_list.InsertItem(dlg->count++,buff); // dlg->m_list.Scroll(size); } //发送断开命令 dlg->SetState(0); dlg->m_edit.SetWindowText("断开"); send(dlg->clisock,"Disconnected",MAXBUFFERSIZE,0); dlg->m_button1.EnableWindow(FALSE); dlg->m_connect.EnableWindow(TRUE); dlg->m_disconnect.EnableWindow(FALSE); closesocket(dlg->clisock); AfxEndThread(0); return 0; } void CMysecDlg::OnChangeEdit2() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. // TODO: Add your control notification handler code here } void CMysecDlg::GetData(void *buffer) { char buff[MAXBUFFERSIZE]; struct VALUE{ long address; unsigned short data[4096]; int check; char c; int number; }m_data; memcpy(&m_data,buffer,sizeof(m_data)); unsigned short x; long address=m_data.address; long addressto; int check; check=sizeof(m_data); if(~check!=m_data.check) return; if(address!=0xd8000) return; CString str; str.Format("正在接受数据 %c",m_data.c); m_edit.SetWindowText(str); /* for(int k=0;k<2048;k++) { x=m_data.data[k]; m_WriteData.SetMemory(address,x); address+=2; } */ int k; int number; long m; address=0x800; number=m_data.number; for(k=0;k ShowWindow(0); KillTimer(1); } CDialog::OnTimer(nIDEvent); } void CMysecDlg::OnClose() { // TODO: Add your message handler code here and/or call default CDialog::OnClose(); } void CMysecDlg::OnAppExit() { // TODO: Add your command handler code here CDialog::OnCancel(); } void CMysecDlg::OnMenushow() { this->ShowWindow(1); } void CMysecDlg::SetHideTimer() { m_Timer=this->SetTimer(1,1000,0); } void CMysecDlg::GetThanWrite() { } void CMysecDlg::OnAddresstable() { BOOL flag; int k; FILE *fp; sModify=""; CString TableName="sendaddress"; CString DataBaseName="addresstable.mdb"; flag=OpenDatabase(DataBaseName,TableName); if(flag) { if(sModify=="*MODIFY") { fp=fopen("addresstable.rec","wb"); fwrite(&RecordMax,1,sizeof(RecordMax),fp); for(k=0;k =0xd8000) return addresstemp; else return -1; } void CMysecDlg::GetaAddressTable() { long address=0xd8000; short x; RecordMax=0; int k; FILE *fp; arrayMaster.RemoveAll(); fp=fopen("addresstable.rec","rb"); if(fp==NULL) { k=KillTimer(1); MessageBox("地址表文件不存在!"); return; } fread(&RecordMax,1,sizeof(RecordMax),fp); for(k=0;k =0xdb000) flag=false; if(measure.ADDRESSTO<0xd8000 || measure.ADDRESSTO>=0xdb000) flag=false; } return flag; } BOOL CMysecDlg::DestroyWindow() { // TODO: Add your specialized code here and/or call the base class return CDialog::DestroyWindow(); } void CMysecDlg::SetState(BOOL b_link) { if(b_link) m_TrayIcon.SetIcon(IDI_OPEN); else m_TrayIcon.SetIcon(IDI_CLOSE); } void CMysecDlg::SetRun() { static bool b_run=false; if(b_run) m_TrayIcon.SetIcon(IDI_OPEN); else m_TrayIcon.SetIcon(IDI_RUN); b_run=!b_run; } void CMysecDlg::ReadConfig() { FILE *fp; select=1; fp=fopen("comconfig.txt","rt"); if(fp==NULL) return; fscanf(fp,"%d",&select); fscanf(fp,"%d",&sin_port); fscanf(fp,"%s",&server); fclose(fp); }