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

📄 testsock_cntdlg.cpp

📁 网络延时测试程序客户端
💻 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地址和端口号,就是说
 *  “只需要修改客户端,因为客户端机器没有发生变化”
 */
//CString SERVER_ADDR="210.47.165.71"; // 客户端认识的服务器的IP地址 沈阳农业大学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))//函数Create的调用就是这样的,
		{
			/*
			 *	设置多媒体定时器
			 */
			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函数调用错误!\r\n");
		}
	}
	else
	{
		GetDlgItem(IDC_SHOWTEXT)->SetWindowText("创建套接字对象错误!\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函数发送到服务器端
			{
				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);//函数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);                        
}
/*
 *	总结多媒体定时器的使用方法,在一个函数中调用函数timeSetEvent,设置定时器事件
 *  并且要设置TIME_PERIODIC参数,表示是周期性的事件,系统将每隔一个比较精确的时间
 *  调用预先定义的回调函数,在回调函数中完成一些需要定时处理的工作.还要注意调用函数
 *  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 + -