net_040421205_clientdlg.cpp

来自「一款小型的C/S模式网络同步校时系统」· C++ 代码 · 共 282 行

CPP
282
字号
// NET_040421205_ClientDlg.cpp : implementation file
//

#include "stdafx.h"
#include "NET_040421205_Client.h"
#include "NET_040421205_ClientDlg.h"
#include "Windows.h"
#include "stdio.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()

/////////////////////////////////////////////////////////////////////////////
// CNET_040421205_ClientDlg dialog

CNET_040421205_ClientDlg::CNET_040421205_ClientDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNET_040421205_ClientDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CNET_040421205_ClientDlg)
	m_ServerTime = _T("");
	m_Link = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CNET_040421205_ClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNET_040421205_ClientDlg)
	DDX_Control(pDX, IDC_IPADDRESS1, m_ServerIP);
	DDX_Text(pDX, IDC_EDIT1, m_ServerTime);
	DDX_Text(pDX, IDC_EDIT2, m_Link);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CNET_040421205_ClientDlg, CDialog)
	//{{AFX_MSG_MAP(CNET_040421205_ClientDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_TIMER()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnStartServer)
	ON_BN_CLICKED(IDC_BUTTON2, OnStopServer)
	ON_NOTIFY(IPN_FIELDCHANGED, IDC_IPADDRESS1, OnFieldchangedIpaddress1)
	ON_BN_CLICKED(IDC_BUTTON4, OnSetTime)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNET_040421205_ClientDlg message handlers

BOOL CNET_040421205_ClientDlg::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_ServerIP.SetAddress(192,168,84,93);
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CNET_040421205_ClientDlg::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 CNET_040421205_ClientDlg::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.
char buff[100];


UINT thread(LPVOID p)
{
	
	CNET_040421205_ClientDlg * dlg=(CNET_040421205_ClientDlg *)AfxGetApp()->GetMainWnd ();

    if((dlg->conn=connect(dlg->sock ,(sockaddr *)&(dlg->serv ),sizeof(dlg->serv)))!=SOCKET_ERROR)
	{
		     dlg->m_Link = "服务器连接成功";

	}
	else
	{
             dlg->m_Link = "服务器连接错误";
		     return 0;
	}
	recv(dlg->sock ,buff,15,0);
	AfxEndThread(0);
	
	return 0;
	
}


HCURSOR CNET_040421205_ClientDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CNET_040421205_ClientDlg::OnStartServer() 
{
	// TODO: Add your control notification handler code here
	serv.sin_addr.s_addr=htonl(ip);
	serv.sin_family =AF_INET;
	serv.sin_port =htons(5000);
	sock=socket(AF_INET,SOCK_STREAM,0);
	UpdateData(true);
	m_Link ="服务器连接成功";
	UpdateData(false);
	AfxBeginThread(thread,0);
		
}

void CNET_040421205_ClientDlg::OnStopServer() 
{

	KillTimer(1);
	UpdateData(true);
	m_Link ="服务器连接断开";
	closesocket(sock);
	UpdateData(false);
	
}

void CNET_040421205_ClientDlg::OnFieldchangedIpaddress1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	m_ServerIP.GetAddress(ip);
	*pResult = 0;
}

void CNET_040421205_ClientDlg::OnTimer(UINT nIDEvent)
{
    closesocket(sock);
	CString time;
	CString year,month,day,hour,minute,second;
	time= time+"服务器时间为:"+buff[0]+buff[1]+buff[2]+buff[3]+"年"+buff[4]+buff[5]+"月"+buff[6]+buff[7]+"日"+buff[8]+ buff[9]+":"+buff[10]+ buff[11]+":"+ buff[12]+ buff[13];

	m_ServerTime = time;
	UpdateData(false);
    //设置本机socket地址
	serv.sin_addr.s_addr=htonl(ip);
	serv.sin_family =AF_INET;
	serv.sin_port =htons(5000);
	sock=socket(AF_INET,SOCK_STREAM,0);//创建socket
	UpdateData(true);
	m_Link =m_Link+"          "+ "开始自动更新";
	UpdateData(false);
	AfxBeginThread(thread,0);
	for(int i=0;i<4;i++)  year+=buff[i];
	for(i=4;i<6;i++)  month+=buff[i];
	for(i=6;i<8;i++)  day+=buff[i];
	for(i=8;i<10;i++) hour+=buff[i];
	for(i=10;i<12;i++) minute+=buff[i];
	for(i=12;i<14;i++) second+=buff[i];
	GetLocalTime(&systemtime);
	systemtime.wYear = (WORD)atoi(year);
	systemtime.wMonth = (WORD)atoi(month);
	systemtime.wDay = (WORD)atoi(day);
	systemtime.wHour = (WORD)atoi(hour);
	systemtime.wMinute = (WORD)atoi(minute);
	systemtime.wSecond = (WORD)atoi(second);
	SetLocalTime(&systemtime);
	
	CDialog::OnTimer(nIDEvent);

}


void CNET_040421205_ClientDlg::OnSetTime() 
{
  	SetTimer(1,3000,NULL);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?