?? testsock_cntdlg.cpp
字號:
// TestSock_CntDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TestSock_Cnt.h"
#include "TestSock_CntDlg.h"
#include <fstream> //這兩句話的放置位置有講究,不能隨意地放置
using namespace std; //要放置在這里才可以
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
ofstream outfile1("udptime.m");
/*
* 換服務器時,只需要修改客戶端所認識的服務器的IP地址和端口號,就是說
* “只需要修改客戶端,因為客戶端機器沒有發(fā)生變化”
*/
//CString SERVER_ADDR="210.47.165.71"; // 客戶端認識的服務器的IP地址 沈陽農(nóng)業(yè)大學IP
//UINT SERVER_PORT=6666; // 客戶端認識的服務器的端口號
CString SERVER_ADDR="60.0.187.164"; // 客戶端認識的服務器的IP地址 // 石家莊IP 60.0.187.164
UINT SERVER_PORT=6666; // 客戶端認識的服務器的端口號
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CTestSock_CntDlg dialog
CTestSock_CntDlg::CTestSock_CntDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestSock_CntDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestSock_CntDlg)
m_sShowText = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_SocketCnt=NULL;
m_bInit=FALSE;
Source_IP = ""; //="35.99";
Source_PORT = 0; //=8888;
}
void CTestSock_CntDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestSock_CntDlg)
DDX_Text(pDX, IDC_SHOWTEXT, m_sShowText);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestSock_CntDlg, CDialog)
//{{AFX_MSG_MAP(CTestSock_CntDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_EN_UPDATE(IDC_INPUTTEXT, OnUpdateInputtext)
ON_BN_CLICKED(IDC_STARTTIMER, OnStarttimer)
ON_BN_CLICKED(IDC_STOPTIMER, OnStoptimer)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestSock_CntDlg message handlers
BOOL CTestSock_CntDlg::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
// TODO: Add extra initialization here
m_SocketCnt=new CMySocket(this);
if (m_SocketCnt!=NULL)
{
if (m_SocketCnt->Create(8888,SOCK_DGRAM))//函數(shù)Create的調(diào)用就是這樣的,
{
/*
* 設置多媒體定時器
*/
m_TimerDelay=2000;//定時間隔
TIMECAPS tc;
if(timeGetDevCaps(&tc,sizeof(TIMECAPS))==TIMERR_NOERROR)
{
m_nCount=0;
GetDlgItem(IDC_SHOWTEXT)->SetWindowText("客戶端套接字和多媒體定時器初始化成功!\r\n");
m_bInit=TRUE;
m_TimerAccuracy=min(max(tc.wPeriodMin,1),tc.wPeriodMax);
timeBeginPeriod(m_TimerAccuracy); //設置最小時間分辨率
}
else
{
GetDlgItem(IDC_SHOWTEXT)->SetWindowText("定時器初始化錯誤!\r\n");
}
}
else
{
GetDlgItem(IDC_SHOWTEXT)->SetWindowText("Create函數(shù)調(diào)用錯誤!\r\n");
}
}
else
{
GetDlgItem(IDC_SHOWTEXT)->SetWindowText("創(chuàng)建套接字對象錯誤!\r\n");
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CTestSock_CntDlg::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 CTestSock_CntDlg::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 CTestSock_CntDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTestSock_CntDlg::OnUpdateInputtext()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function to send the EM_SETEVENTMASK message to the control
// with the ENM_UPDATE flag ORed into the lParam mask.
// TODO: Add your control notification handler code here
if (!m_bInit)
{
AfxMessageBox("客戶端套接字初始化未成功!");
return;
}
else
{
CString in;
GetDlgItem(IDC_INPUTTEXT)->GetWindowText(in);
if(in.GetAt(in.GetLength()-1)=='\n')
{
GetDlgItem(IDC_INPUTTEXT)->SetWindowText("");
if(in.GetLength()>2)//用sendto函數(shù)發(fā)送到服務器端
{
m_SocketCnt->SendTo(in.GetBuffer(0),in.GetLength(),SERVER_PORT,SERVER_ADDR);//服務器的IP地址和端口號
//AfxMessageBox("2134214");
m_dSendTime=timeGetTime();
}
}
}
}
void CTestSock_CntDlg::ProcessPendingRead(CMySocket* pSocket)
{
pSocket->ReceiveFrom(&m_dSendTime,4,Source_IP,Source_PORT);
m_dReceiveTime=timeGetTime();
// m_dSendTime=(m_pReceiveContent[0]<<24)+(m_pReceiveContent[1]<<16)+(m_pReceiveContent[2]<<8)+(m_pReceiveContent[3]);
m_dDelay=m_dReceiveTime-m_dSendTime;//-m_TimerDelay;
// if (m_dDelay<0)
// {
// AfxMessageBox("問題");
// }
outfile1<<m_dDelay<<' ';
m_nCount++;
SetDlgItemInt(IDC_COUNT,m_nCount,FALSE);//函數(shù)SetDlgItemInt直接把整形的值賦給由ID確定的編輯控件
CString m_sSendTime,m_sDelay;
m_sSendTime.Format("%d",m_dSendTime);
m_sDelay.Format("%d",m_dDelay);
m_sSendTime=m_sSendTime+" "+m_sDelay+"\r\n";
m_sShowText+=m_sSendTime;
CEdit* output=NULL;
output=(CEdit*)GetDlgItem(IDC_SHOWTEXT);
output->SetWindowText(m_sShowText);
output->LineScroll(output->GetLineCount());
}
void PASCAL TimerProcedure(UINT wTimerID, UINT msg,DWORD dwUser,DWORD dwl,DWORD dw2)
{
// char m_pSendContent[4];
DWORD m_dCurrentTime=timeGetTime();
// m_pSendContent[0]=HIBYTE(HIWORD(m_dCurrentTime));
// m_pSendContent[1]=LOBYTE(HIWORD(m_dCurrentTime));
// m_pSendContent[2]=HIBYTE(LOWORD(m_dCurrentTime));
// m_pSendContent[3]=LOBYTE(LOWORD(m_dCurrentTime));
// DWORD test =(((m_pSendContent[0])<<24) & 0xff000000)+
// (((m_pSendContent[1])<<16) & 0x00ff0000)
// +(((m_pSendContent[2])<<8) & 0x0000ff00)+((m_pSendContent[3]) & 0x000000ff);
((CTestSock_CntDlg*)AfxGetMainWnd())->m_SocketCnt->SendTo(&m_dCurrentTime,4,SERVER_PORT,SERVER_ADDR);
}
void CTestSock_CntDlg::OnStarttimer()
{
// TODO: Add your control notification handler code here
m_TimerID = timeSetEvent(m_TimerDelay, m_TimerAccuracy,
(LPTIMECALLBACK) TimerProcedure,
(DWORD)this,TIME_PERIODIC);
}
/*
* 總結多媒體定時器的使用方法,在一個函數(shù)中調(diào)用函數(shù)timeSetEvent,設置定時器事件
* 并且要設置TIME_PERIODIC參數(shù),表示是周期性的事件,系統(tǒng)將每隔一個比較精確的時間
* 調(diào)用預先定義的回調(diào)函數(shù),在回調(diào)函數(shù)中完成一些需要定時處理的工作.還要注意調(diào)用函數(shù)
* timeKillEvent(m_TimerID)取消一個指定的時間事件.
*/
void CTestSock_CntDlg::OnStoptimer()
{
// TODO: Add your control notification handler code here
timeKillEvent(m_TimerID);//取消一個指定的時間事件
//timeEndPeriod(m_TimerAccuracy);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -