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

📄 udtrecvdlg.cpp

📁 使用UDP模拟可靠传输协议rdt2.0。使用两个程序
💻 CPP
字号:
// UDTRecvDlg.cpp : implementation file
//

#include "stdafx.h"
#include "UDTRecv.h"
#include "UDTRecvDlg.h"
#include "cmmdef.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


static char dataRcv[1024];
static int datalen=0;

static CString ipstr;
static int curId=0;
/////////////////////////////////////////////////////////////////////////////
// CUDTRecvDlg dialog

CUDTRecvDlg::CUDTRecvDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CUDTRecvDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CUDTRecvDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CUDTRecvDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUDTRecvDlg)
	DDX_Control(pDX, IDC_EDIT_MSGRCV, m_msgshow);
	DDX_Control(pDX, IDC_EDIT_INFO, m_info);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CUDTRecvDlg, CDialog)
	//{{AFX_MSG_MAP(CUDTRecvDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUDTRecvDlg message handlers

BOOL CUDTRecvDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	AfxSocketInit();
	if(!rcvSock.Create(PORT_CLI,SOCK_DGRAM))
	{
		TRACE0("Failed to create sock!");
		return FALSE;
	}
	rcvSock.SetNotifyMsg(CLI_RCV_MSG);
	rcvSock.SetNotifyWnd(GetSafeHwnd());

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CUDTRecvDlg::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 CUDTRecvDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

LRESULT CUDTRecvDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(message==CLI_RCV_MSG)
	{//接收到pkt from srv
		CString txt,str; m_info.GetWindowText(txt);

		SNDPKT spkt;
		rcvSock.Receive(&spkt,sizeof(spkt));

		
		if(spkt.id==0) //接收开始!!
		{
			memcpy(&datalen,spkt.data,sizeof(datalen));
			curId=0;
			ipstr.Format("%d.%d.%d.%d",spkt.data[4],spkt.data[5],spkt.data[6],spkt.data[7]);

			str.Format("receive pkt start:(num=%d)\r\n",datalen); txt+=str;
		}
		else//接收数据
		{
			str.Format("receive pkt from %s:id=%d\t",ipstr,spkt.id); //txt+=str;
			if(spkt.id==curId+1)//接收
			{
				curId=spkt.id;
				memcpy(dataRcv+PD_LEN*(curId-1),spkt.data,PD_LEN);
				
				str+="accepted!!\r\n";
			}
			else
			{
				str+="ignored!!\r\n";
			}
			txt+=str;
			if(curId>=datalen)//显示
			{
				m_msgshow.SetWindowText(dataRcv);
			}

			m_info.SetWindowText(txt); m_info.LineScroll(m_info.GetLineCount());
		}
		REQPKT rpkt;
		rpkt.flag=REQ_ACK;
		rpkt.id=curId;
		rcvSock.SendTo(&rpkt,sizeof(rpkt),PORT_SRV,ipstr);
	}
	return CDialog::WindowProc(message, wParam, lParam);
}

⌨️ 快捷键说明

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