TestDlg.cpp
上传用户:huazhu2004
上传日期:2022-06-27
资源大小:1929k
文件大小:10k
- // TestDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Test.h"
- #include "liaotian.h"
- #include "TestDlg.h"
- #include <afxsock.h>
- #include <time.h>
- #include <fstream.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- #define ID_TIMER_SENDPROXY WM_USER
- #define ID_TIMER_CHAT WM_USER
- 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()
- /////////////////////////////////////////////////////////////////////////////
- // CTestDlg dialog
- CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CTestDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CTestDlg)
- m_Info = _T("");
- m_Output = _T("");
- m_Input = _T("");
- m_Connect = _T("");
- m_IPAddress = _T("");
- m_Port = 0;
- m_Status = -1;
- m_time = _T("");
- m_time2 = _T("");
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CTestDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CTestDlg)
- DDX_Text(pDX, IDC_OUTPUTEDIT, m_Output);
- DDX_Text(pDX, IDC_INPUTEDIT, m_Input);
- DDX_Text(pDX, IDC_CONNECTEDIT, m_Connect);
- DDX_Text(pDX, IDC_IPADDRESS, m_IPAddress);
- DDV_MaxChars(pDX, m_IPAddress, 15);
- DDX_Text(pDX, IDC_PORT, m_Port);
- DDX_Radio(pDX, IDC_SERVERRADIO, m_Status);
- DDX_Text(pDX, IDC_TIME, m_time);
- DDX_Text(pDX, IDC_time2, m_time2);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
- //{{AFX_MSG_MAP(CTestDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_CONNECTBUTTON, OnConnect)
- ON_BN_CLICKED(IDC_DISCONNECTBUTTON, OnDisconnect)
- ON_BN_CLICKED(IDC_SENDBUTTON, OnSend)
- ON_BN_CLICKED(IDC_SERVERRADIO, OnServerradio)
- ON_BN_CLICKED(IDC_CLIENTRADIO, OnClientradio)
- ON_BN_CLICKED(IDC_SENDCLEARBUTTON, OnSendclear)
- ON_BN_CLICKED(IDC_RECEIVECLEARBUTTON, OnReceiveclear)
- ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
- ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CTestDlg message handlers
- BOOL CTestDlg::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);
-
- 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
-
- m_Status=-1;
- m_ServerSocket=NULL;
- m_ClientSocket=NULL;
- m_arIn=NULL;
- m_arOut=NULL;
- m_file=NULL;
- m_Connect="";
- m_IPAddress="192.168.153.1";
- m_Port=5000;
- m_time="开始时间";
- m_time2="结束时间";
- a=0;b=0;c=0;d=0;
- GetDlgItem(IDC_IPADDRESS)->EnableWindow(FALSE);
- GetDlgItem(IDC_PORT)->EnableWindow(FALSE);
- UpdateData(FALSE);
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CTestDlg::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 CTestDlg::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 CTestDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CTestDlg::OnConnect()
- {
- CString msg;
- UpdateData(TRUE);
- if (m_Status==0 ) //server
- {
- if ( m_ServerSocket!=NULL)
- {
- m_Connect="Please disconnect!";
- UpdateData(FALSE);
- }
- else
- {
- m_Connect="Waiting for Client...";
- UpdateData(FALSE);
- if(!AfxSocketInit())
- {
- MessageBox("WindowsSocket initial failed!","Send",MB_ICONSTOP);
- return;
- }
- m_ServerSocket=new CNewSocket;
- m_ServerSocket->m_Status=m_Status;
- m_ServerSocket->GetDlg(this);
- if(!m_ServerSocket->Create(m_Port))
- MessageBox("SendSocket create failed!", "Send",MB_ICONSTOP);
- else
- {
- m_ServerSocket->Listen();
- }
- }
- }
- else
- {
- if (m_Status==1)
- {
- if (m_ClientSocket!=NULL)
- {
- m_Connect="Please disconnect!";
- UpdateData(FALSE);
- }
- else
- {
- m_Connect="Connect to the Server...";
- UpdateData(FALSE);
- if(!AfxSocketInit())
- {
- MessageBox("WindowsSocket initial failed!", "Receive",MB_ICONSTOP);
- return;
- }
- m_ClientSocket=new CNewSocket;
- m_ClientSocket->GetDlg(this);
- m_ClientSocket->m_Status=m_Status;
- if(!m_ClientSocket->Create())
- {
- MessageBox("ReceiveSocket create failed!","Receive",MB_ICONSTOP);
- return;
- }
- else
- {
- if (!m_ClientSocket->Connect(m_IPAddress,m_Port))
- {
- CString str=m_Connect;
- SocketReset();
- m_Connect=str;
- m_Connect+="Error!";
- UpdateData(FALSE);
- }
- else
- {
- m_Connect+="OK!";
- ontimer(1);
- m_file=new CSocketFile(m_ClientSocket);
- m_arIn=new CArchive(m_file, CArchive::load);
- m_arOut=new CArchive(m_file, CArchive::store);
- }
- UpdateData(FALSE);
- }
- }
- }
-
- }
- if (m_Status==-1)
- {
- msg="Please choose the status!";
- AfxMessageBox(msg);
- }
- }
- void CTestDlg::OnSend()
- {
- if (m_arOut)
- {
- if (m_Status==0)
- {
- UpdateData(TRUE);
- *m_arOut<<m_Output;
- m_arOut->Flush();
- time_t t3 = time(0);
- char tmp3[64];
- strftime( tmp3, sizeof(tmp3), "%X",localtime(&t3));
- fstream in("1.txt",ios::app);
- in<<"sever: "<<tmp3;
- in<<m_Output<<endl;
- in.close();
- }
- else
- {
- UpdateData(TRUE);
- *m_arOut<<m_Output;
- m_arOut->Flush();
- time_t t3 = time(0);
- char tmp3[64];
- strftime( tmp3, sizeof(tmp3), "%X",localtime(&t3));
- fstream in("1.txt",ios::app);
- in<<"client: "<<tmp3;
- in<<m_Output<<endl;
- in.close();
- }
- }
- else AfxMessageBox("Not connected!");
- }
- void CTestDlg::OnAccept()
- {
- m_Connect+="OK!";
- ontimer(1);
- UpdateData(FALSE);
-
- m_ClientSocket=new CNewSocket;
- m_ClientSocket->GetDlg(this);
- m_ServerSocket->Accept(*m_ClientSocket);
- m_ClientSocket->m_Status=m_ServerSocket->m_Status;
- m_file=new CSocketFile(m_ClientSocket);
- m_arIn=new CArchive(m_file, CArchive::load);
- m_arOut=new CArchive(m_file, CArchive::store);
-
- }
- void CTestDlg::OnReceive()
- {
- *m_arIn>>m_Input;
- UpdateData(FALSE);
- }
- void CTestDlg::OnDisconnect()
- {
- if (m_arOut!=NULL)
- {
- SocketReset();
- m_Connect="Disconnected!";
- ontimer(2);
- UpdateData(FALSE);
- c++;
-
-
- }
- }
- void CTestDlg::OnClose()
- {
- if (m_ClientSocket->m_Status==0) m_Connect="Client ";
- else m_Connect="Server ";
- m_Connect+="has disconnected!";
- ontimer(2);
- UpdateData(FALSE);
-
- }
- void CTestDlg::SocketReset()
- {
- if (m_arIn!=NULL)
- {
- delete m_arIn;
- m_arIn=NULL;
- }
- if (m_arOut!=NULL)
- {
- delete m_arOut;
- m_arOut=NULL;
- }
- if (m_file!=NULL)
- {
- delete m_file;
- m_file=NULL;
- }
- if (m_ClientSocket!=NULL)
- {
- delete m_ClientSocket;
- m_ClientSocket=NULL;
- }
- if (m_ServerSocket!=NULL)
- {
- delete m_ServerSocket;
- m_ServerSocket=NULL;
- }
- m_Connect="";
- UpdateData(FALSE);
- }
- void CTestDlg::OnServerradio()
- {
- UpdateData(TRUE);
- GetDlgItem(IDC_IPADDRESS)->EnableWindow(FALSE);
- GetDlgItem(IDC_PORT)->EnableWindow(TRUE);
- UpdateData(FALSE);
- }
- void CTestDlg::OnClientradio()
- {
- UpdateData(TRUE);
- GetDlgItem(IDC_IPADDRESS)->EnableWindow(TRUE);
- GetDlgItem(IDC_PORT)->EnableWindow(TRUE);
- UpdateData(FALSE);
- }
- void CTestDlg::OnSendclear()
- {
- m_Output="";
- UpdateData(FALSE);
- }
- void CTestDlg::OnReceiveclear()
- {
- m_Input="";
- UpdateData(FALSE);
- }
- void CTestDlg::ontimer(int bb)
- {
- // TODO: Add your message handler code here and/or call default
- //更新时间
- if(bb==1)
- {
- time_t t = time(0);
- char tmp[64];
- strftime( tmp, sizeof(tmp), "%X",localtime(&t));
- m_time=tmp;
- }
- if(bb==2)
- {
- time_t t2 = time(0);
- char tmp2[64];
- strftime( tmp2, sizeof(tmp2), "%X",localtime(&t2));
- m_time2=tmp2;
- }
- }
-
- void CTestDlg::OnButton1()
- {
- // TODO: Add your control notification handler code here
- fstream a("1.txt",ios::trunc);
- UpdateData(FALSE);
- }
- void CTestDlg::OnButton2()
- {
- // TODO: Add your control notification handler code here
- Cliaotian a;
- a.DoModal();
- }