⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 net_040421205_serverdlg.cpp

📁 一款小型的C/S模式网络同步校时系统
💻 CPP
字号:
// NET_040421205_ServerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "NET_040421205_Server.h"
#include "NET_040421205_ServerDlg.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_ServerDlg dialog

CNET_040421205_ServerDlg::CNET_040421205_ServerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNET_040421205_ServerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CNET_040421205_ServerDlg)
	m_year = _T("");
	m_month = _T("");
	m_day = _T("");
	m_hour = _T("");
	m_minute = _T("");
	m_second = _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_ServerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNET_040421205_ServerDlg)
	DDX_Text(pDX, IDC_EDIT1, m_year);
	DDX_Text(pDX, IDC_EDIT2, m_month);
	DDX_Text(pDX, IDC_EDIT3, m_day);
	DDX_Text(pDX, IDC_EDIT4, m_hour);
	DDX_Text(pDX, IDC_EDIT5, m_minute);
	DDX_Text(pDX, IDC_EDIT6, m_second);
	DDX_Text(pDX, IDC_EDIT7, m_Link);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CNET_040421205_ServerDlg, CDialog)
	//{{AFX_MSG_MAP(CNET_040421205_ServerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnStopServer)
	ON_BN_CLICKED(IDC_BUTTON3, OnGetTime)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON4, OnTIME)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNET_040421205_ServerDlg message handlers
UINT thread(LPVOID p)
{
//	char buff[100];

//	int recvlen=1;

    CNET_040421205_ServerDlg *dlg=(CNET_040421205_ServerDlg*)AfxGetApp()->GetMainWnd();

	dlg->sock1=accept(dlg->sock,(sockaddr*)&(dlg->serv),&(dlg->addlen));
	if(dlg->sock1==INVALID_SOCKET)
	{
		
		dlg->m_Link="accept error!";
		
	}
	AfxEndThread(0);
	return 0;
}

BOOL CNET_040421205_ServerDlg::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_Link="服务器已经启动...";
	m_year='0';
	m_month='0';
	m_day='0';
	m_hour='0' ;
	m_minute='0' ;
	m_second='0' ;
	UpdateData(false);
		
	return TRUE;  // return TRUE  unless you set the focus to a control
}


void CNET_040421205_ServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}


void CNET_040421205_ServerDlg::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 CNET_040421205_ServerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
//获取服务器时间
void CNET_040421205_ServerDlg::OnGetTime() 
{

    GetLocalTime(&systemtime);

	year.Format("%4d",systemtime.wYear);
	month.Format("%2d", systemtime.wMonth);
	day.Format("%2d", systemtime.wDay );
	hour.Format("%2d",systemtime.wHour);
	minute.Format("%2d",systemtime.wMinute);
	second.Format("%2d",systemtime.wSecond);

	UpdateData(true);
	m_year=year ;
	m_month=month ;
	m_day=day ;
	m_hour=hour ;
	m_minute=minute ;
	m_second=second ;
	UpdateData(false);

}

void CNET_040421205_ServerDlg::OnStopServer() 
{
	KillTimer(1);	
	UpdateData(true);
	m_Link =m_Link+"                                  "+"服务器连接已终止";
	UpdateData(false);
	
}

 void CNET_040421205_ServerDlg::OnTimer(UINT nIDEvent)
{
	
    char buff[50];
	CString time;

	closesocket(sock);
	//设置本机socket地址
	serv.sin_addr .s_addr=htonl(INADDR_ANY);
	serv.sin_family =AF_INET;
	serv.sin_port =htons(5000);
	addlen=sizeof(serv);
	
	sock=socket(AF_INET,SOCK_STREAM,0);//创建socket
    
	GetLocalTime(&systemtime);//获取服务器本地时间
	year.Format("%4d",systemtime.wYear);
	month.Format("%2d", systemtime.wMonth);
	day.Format("%2d", systemtime.wDay );
	hour.Format("%2d",systemtime.wHour);
	minute.Format("%2d",systemtime.wMinute);
	second.Format("%2d",systemtime.wSecond);

	if(bind(sock,(sockaddr *)&serv,addlen))
	{
		m_Link="bind error!";
	}
	else
	{	
	    UpdateData(true);
		m_year=year ;
		m_month=month ;
		m_day=day ;
		m_hour=hour ;
		m_minute=minute ;
		m_second=second ;
		UpdateData(false);
		listen(sock,5);
        AfxBeginThread(thread,0);
	}
	
	time = year + month + day + hour + minute + second;
	strcpy(buff,time);

	send(sock1,(const char *)buff,15,0);
	UpdateData(true);
	m_Link = m_Link+"                                  "+"服务器时间已发送";
	UpdateData(false);
	CDialog::OnTimer(nIDEvent);
}



void CNET_040421205_ServerDlg::OnTIME() 
{
	// TODO: Add your control notification handler code here
	 SetTimer(1,3000,NULL);
}

⌨️ 快捷键说明

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