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

📄 testclientdlg.cpp

📁 同学的毕业设计。基于Hpc密码的选择重传协议的数据传输。希望能供大家参考。谢谢 !
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// TestclientDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Testclient.h"
#include "TestclientDlg.h"
#include "Mysocket.h"
#include "HPC.h"
#include <afxmt.h>
//#include <atltime.h>
//#include "Winbase.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CMutex cMutex(FALSE,NULL);

static UINT Sender(LPVOID pParam)
{
CTestclientDlg *pDlg=(CTestclientDlg*)pParam;
CString SendData;
while(pDlg->event != 0)
{
	while(pDlg->nbuffered<3)
	{	cMutex.Lock();
		if(pDlg->next_frame_to_send < pDlg->m_strMsg.GetLength()/16)
		{pDlg->nbuffered = pDlg->nbuffered + 1;
		SendData.Format(_T("共%d帧-第%d帧-%d类帧-%s"), pDlg->m_strMsg.GetLength()/16,pDlg->next_frame_to_send,1,pDlg->m_strMsg.Mid(pDlg->next_frame_to_send*16,16)); //拼凑一帧信息:帧号*信息总长度*数据
		  int iLen = SendData.GetLength();
		 
          pDlg->m_ctrStatus.AddString(pDlg->strStatusMsg+" 发送帧:" + SendData);  //写日志
		  pDlg->m_ctrStatus.SetCurSel(pDlg->m_ctrStatus.GetCount()-1);                 //垂直滚动到底部
		  int iSent = pDlg->m_sConnectSocket.Send(LPCTSTR(SendData),iLen);
		  pDlg->next_frame_to_send++;
		  if (iSent == SOCKET_ERROR)
		  {SetTimer(pDlg->m_hWnd,1000,2000, NULL);}              // 发送失败或超时
		  else 
		  {SetTimer(pDlg->m_hWnd,pDlg->next_frame_to_send-1,2000, NULL);}                    // 看对应返回是否超时
	  }	cMutex.Unlock();
	}
}
//AfxEndThread();
return 0;
}
/*
static UINT Recver(LPVOID pParam)
{
	CTestclientDlg *pDlg=(CTestclientDlg*)pParam;
	cMutex.Lock();
	int iSent1 ;
	int iLen1;
	CString cBuf;
	int count,num,kind;
	CString SendAck;
	char *pBuf = new char[1025];

	int iBufSize = 1024;
	int iRcvd;
	pDlg->theTime = CTime::GetCurrentTime();
    pDlg->strStatusMsg = pDlg->theTime.Format("%H:%M:%S");   //记录事件发生的时间

	iRcvd = pDlg->m_sConnectSocket.Receive(pBuf,iBufSize);
	pBuf[iRcvd] = NULL;                          //收到的字符串,char 型
    pDlg->strBuf = pBuf;                               //收到的字符串,CString型

	if(iRcvd == SOCKET_ERROR)
	{
		pDlg->m_ctrStatus.AddString(pDlg->strStatusMsg + " 网络传输有误!");
		pDlg->m_ctrStatus.SetCurSel(pDlg->m_ctrStatus.GetCount()-1);   //垂直滚动到底部
	}
	else
	{
	    KillTimer(pDlg->m_hWnd,1002); //证明收到了回应,关掉定时器
       	if(pDlg->iConnStatus == 1 || pDlg->strRecvd == "client ok.server ok.") //当系统状态为: 等待客户端联接
		{
			pDlg->iConnStatus = 2;        //设置成功联接客户端标志
			
	        //GetDlgItem(IDC_BSEND)->EnableWindow(TRUE); //启用“发送消息”按钮
			//GetDlgItem(IDC_EMSG)->EnableWindow(TRUE);  //启用“发送消息”文本框
	        pDlg->m_ctrStatus.AddString(pDlg->strStatusMsg+" 服务器成功响应.");         //写日志
			pDlg->m_ctrStatus.SetCurSel(pDlg->m_ctrStatus.GetCount()-1);                //垂直滚动到底部
		}
		else if(pDlg->iConnStatus == 2) //当系统状态为: 准备就绪, 并接收到第一帧数据
		{ 
	    sscanf(pDlg->strBuf, "共%d帧-第%d帧-%d类帧-%s", &count, &num,&kind,cBuf); //提取出收到的帧中的编号和字符,以*分隔开。
	switch(kind)
	{
	case 1:	if(pDlg->frame_expected == num)
			{
				SendAck.Format(_T("共%d帧-第%d帧-%d类帧-%s"), count,num,2,"返回帧");
				iLen1 = SendAck.GetLength();
				iSent1 = pDlg->m_sConnectSocket.Send(LPCTSTR(SendAck),iLen1);
				if (iSent1 == SOCKET_ERROR)
				{SetTimer(pDlg->m_hWnd,1000,2000, NULL);}              // 发送失败或超时
				else
				{
					if(count == num+1)
					{
						pDlg->m_ctrStatus.AddString(pDlg->strStatusMsg+" 收到最后一帧:"+pDlg->strBuf);         //写日志
						pDlg->m_ctrStatus.SetCurSel(pDlg->m_ctrStatus.GetCount()-1);    
						pDlg->strRecvd.Insert(num*16, cBuf);
						pDlg->strRecvd.Insert((num+1)*16, '\0'); 
						pDlg->frame_expected++;
						pDlg->m_recvmi.SetWindowText(pDlg->strRecvd);
					}
					else
					{
						pDlg->m_ctrStatus.AddString(pDlg->strStatusMsg+" 收到帧:"+pDlg->strBuf);         //写日志
						pDlg->m_ctrStatus.SetCurSel(pDlg->m_ctrStatus.GetCount()-1);    
						pDlg->strRecvd.Insert(num*16, cBuf);
						pDlg->strRecvd.Insert((num+1)*16, '\0'); 
						pDlg->frame_expected++;
						pDlg->m_recvmi.SetWindowText(pDlg->strRecvd);
					}
				}
			}
			break;
	case 2: if(pDlg->ack_expected == num)
			{
				if(count == num +1)
				{
					pDlg->event = 0;
					pDlg->m_ctrStatus.AddString(pDlg->strStatusMsg+" 收到最后一帧的返回帧:"+pDlg->strBuf);         //写日志
					pDlg->m_ctrStatus.SetCurSel(pDlg->m_ctrStatus.GetCount()-1); 
					pDlg->ack_expected++;
					pDlg->nbuffered--;
					KillTimer(pDlg->m_hWnd,pDlg->frame_expected-1);
				}
				else
				{
					pDlg->m_ctrStatus.AddString(pDlg->strStatusMsg+" 收到返回帧:"+pDlg->strBuf);         //写日志
					pDlg->m_ctrStatus.SetCurSel(pDlg->m_ctrStatus.GetCount()-1); 
					pDlg->ack_expected++;
					pDlg->nbuffered--;
					KillTimer(pDlg->m_hWnd,pDlg->frame_expected-1);
				}
			}
	
	}}}
	
  	cMutex.Unlock();
return 0;
}*/
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CTestclientDlg dialog

CTestclientDlg::CTestclientDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestclientDlg::IDD, pParent)
	, m_strServerIP(_T("127.0.0.1"))
	, m_iPort(11923)
	, iConnStatus(0)
	, theTime(0)
	, strRecvd(_T(""))
	, strSendMsgBuffer(_T(""))
	, iFramePos(0)
	, strStatusMsg(_T(""))
	, iReSend(0)
{
	//{{AFX_DATA_INIT(CTestclientDlg)
	m_strMsg = _T("");
	m_miyao = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestclientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestclientDlg)
	
	//}}AFX_DATA_MAP
	DDX_Text(pDX, IDC_ServerIP, m_strServerIP);
	DDV_MaxChars(pDX, m_strServerIP, 15);
	DDX_Text(pDX, IDC_ServerPort, m_iPort);
	DDV_MinMaxInt(pDX, m_iPort, 1024, 65536);
	DDX_Control(pDX, IDC_STATUS, m_ctrStatus);
	DDX_Control(pDX, IDC_EDIT_RECVMI, m_recvmi);
	DDX_Text(pDX, IDC_EDIT_SENDMI, m_strMsg);
}

BEGIN_MESSAGE_MAP(CTestclientDlg, CDialog)
	//{{AFX_MSG_MAP(CTestclientDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_Connect, OnConnect)
	ON_BN_CLICKED(IDC_BSEND, OnBsend)
	ON_BN_CLICKED(IDC_DisConnect, OnDisConnect)
	ON_BN_CLICKED(IDC_BUTTON_OK, OnButtonOk)
	ON_BN_CLICKED(IDC_BUTTON_JIAMI, OnButtonJiami)
	ON_BN_CLICKED(IDC_BUTTON_JIEMI, OnButtonJiemi)
	ON_BN_CLICKED(IDC_BUTTON_SETMIYAO, OnButtonSetmiyao)
	ON_BN_CLICKED(IDC_BUTTON_SETMIYAO2, OnButtonSetmiyao2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestclientDlg message handlers

BOOL CTestclientDlg::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_sConnectSocket.SetParent(this);
	m_sListenSocket.SetParent(this);
	GetDlgItem(IDC_DisConnect)->EnableWindow(FALSE);
	memset(spice,0,64);
	next_frame_to_send =0;
	 ack_expected= 0;
	 frame_expected = 0;
	 nbuffered= 0;

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

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

void CTestclientDlg::OnConnect() 
{   CString strTime;
    int iSent;
	// TODO: Add your control notification handler code here
	if (!AfxSocketInit())
	{
		AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
		return ;
	}
	m_sConnectSocket.Create();

	//更新界面数据
	UpdateData(TRUE);
	theTime = CTime::GetCurrentTime();
	strTime = theTime.Format("%H:%M:%S");

	//开始判断是否可以启动联接
	if(m_strServerIP != "" && m_iPort != 0)
	{	iSent = m_sConnectSocket.Connect(m_strServerIP, m_iPort);
	    if(iSent == SOCKET_ERROR)
	   {
		m_ctrStatus.AddString(strTime + " 连接失败");      //写日志
		m_ctrStatus.SetCurSel(m_ctrStatus.GetCount()-1);   //垂直滚动到底部
		return;
	   }

     iConnStatus = 1;
	 m_ctrStatus.AddString(strTime + " 已向服务端发起连接");          //禁用"建立联接"按钮,启用"中断联接"
	 m_ctrStatus.SetCurSel(m_ctrStatus.GetCount()-1);   //垂直滚动到底部
	 GetDlgItem(IDC_Connect)->EnableWindow(FALSE);
	 GetDlgItem(IDC_DisConnect)->EnableWindow(TRUE);
     iSent = m_sConnectSocket.Send("client ok.",10);       //发送准备信息.
	}
	else //未输入服务端IP和端口
	{ 
		MessageBox("请输入正确的IP地址!");
		return;
	}
}

⌨️ 快捷键说明

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