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

📄 testserverdlg.cpp

📁 同学的毕业设计。基于Hpc密码的选择重传协议的数据传输。希望能供大家参考。谢谢 !
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// TestServerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TestServer.h"
#include "TestServerDlg.h"
#include "HPC.h"
#include "FILEencrypt.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);
int event;
static UINT Sender(LPVOID pParam)
{
CTestServerDlg *pDlg=(CTestServerDlg*)pParam;
CString SendData;
while(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(m_nThreadID);
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()

/////////////////////////////////////////////////////////////////////////////
// CTestServerDlg dialog

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

void CTestServerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestServerDlg)
	
	//}}AFX_DATA_MAP
	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);
	DDX_Text(pDX, IDC_EDIT_MIYAO, m_miyao);
	DDV_MaxChars(pDX, m_miyao, 32);
}

BEGIN_MESSAGE_MAP(CTestServerDlg, CDialog)
	//{{AFX_MSG_MAP(CTestServerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_Connect, OnConnect)
	ON_BN_CLICKED(IDC_DisConnect, OnDisConnect)
	ON_BN_CLICKED(IDC_BSEND, OnBsend)
	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)
	ON_BN_CLICKED(IDC_BUTTON_FILE, OnButtonFile)
	//}}AFX_MSG_MAP
	ON_WM_TIMER()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestServerDlg message handlers

BOOL CTestServerDlg::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;
	 flag =1;
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CTestServerDlg::OnConnect() 
{
	// TODO: Add your control notification handler code here
	CString strTime;
	if (!AfxSocketInit())
	{
		AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
		return ;
	}

	//更新界面数据
	UpdateData(TRUE);
	//创建监听端口
	m_sListenSocket.Create(m_iPort);
	int rete = m_sListenSocket.Listen();
	theTime = CTime::GetCurrentTime();
	strTime = theTime.Format("%H:%M:%S");

	if (rete == SOCKET_ERROR)
	{
		m_ctrStatus.AddString(strTime + " 监听端口失败.");
		m_ctrStatus.SetCurSel(m_ctrStatus.GetCount()-1);   //垂直滚动到底部
		return;
	}

    m_ctrStatus.AddString(strTime + " 监听端口成功.");
	m_ctrStatus.SetCurSel(m_ctrStatus.GetCount()-1);      //垂直滚动到底部
	iConnStatus = 1;                                      //设置联接成功标志。

	// 成功建立联接, 禁用"服务端"按钮, 启用"关闭服务端"按钮
	GetDlgItem(IDC_Connect)->EnableWindow(FALSE);
	GetDlgItem(IDC_DisConnect)->EnableWindow(TRUE);
	UpdateData(FALSE);
}

void CTestServerDlg::OnAccept()
{
	int rett = m_sListenSocket.Accept(m_sConnectSocket);
	if (rett == SOCKET_ERROR)
	{
		CString m_ErrorMsg;
		m_ErrorMsg = GetErrorMsg();
		MessageBox(m_ErrorMsg);
		return;
		return;
	}

}

void CTestServerDlg::OnReceive()
{	
	UpdateData(TRUE);
	char *pBuf = new char[1025];
	CString cBuf;	
	int iBufSize = 1024;
	int iRcvd;
    int iSent1 ;
	int iLen1;
	int count,num,kind;
	CString SendAck;
	theTime = CTime::GetCurrentTime();
    strStatusMsg = theTime.Format("%H:%M:%S");   //记录事件发生的时间

	iRcvd = m_sConnectSocket.Receive(pBuf,iBufSize);
	pBuf[iRcvd] = '\0';                          //收到的字符串,char 型
    strBuf = pBuf;                               //收到的字符串,CString型

	if(iRcvd == SOCKET_ERROR)
	{
		m_ctrStatus.AddString(strStatusMsg + " 网络传输有误!");
		m_ctrStatus.SetCurSel(m_ctrStatus.GetCount()-1);   //垂直滚动到底部
	}
	else
	{
	    KillTimer(1002); //证明收到了回应,关掉定时器
        

		if(strBuf == "client ok.") //当系统状态为: 等待客户端联接
		{
			if (iConnStatus <= 1)
		    {
			iConnStatus = 2;        //设置成功联接客户端标志
			GetDlgItem(IDC_BSEND)->EnableWindow(TRUE); //启用“发送消息”按钮
			GetDlgItem(IDC_EMSG)->EnableWindow(TRUE);  //启用“发送消息”文本框
            iSent1 = m_sConnectSocket.Send("client ok.server ok.",20);
			if (iSent1 == SOCKET_ERROR){SetTimer(1000,2000, NULL); return ;}       // 发送失败或超时
	        m_ctrStatus.AddString(strStatusMsg+" 成功连接到客户端.");         //写日志
			m_ctrStatus.SetCurSel(m_ctrStatus.GetCount()-1);                  //垂直滚动到底部
		    }
			else
			{
				iSent1 = m_sConnectSocket.Send("client ok.server ok.",20);
			    if (iSent1 == SOCKET_ERROR){SetTimer(1000,2000, NULL); return ;}       // 发送失败或超时
	            m_ctrStatus.AddString(strStatusMsg+" 客户端重连成功.");           //写日志
				m_ctrStatus.SetCurSel(m_ctrStatus.GetCount()-1);                  //垂直滚动到底部
			}
		}
		else if(iConnStatus == 2) //当系统状态为: 准备就绪, 并接收到第一帧数据
		{ 
		  //GetDlgItem(IDC_BSEND)->EnableWindow(FALSE);  //禁用“发送消息”按钮
	      //GetDlgItem(IDC_EMSG)->EnableWindow(FALSE);   //禁用“发送消息”文本框
	
		           //垂直滚动到底部
	    sscanf(strBuf, "共%d帧-第%d帧-%d类帧-%s", &count, &num,&kind,cBuf); //提取出收到的帧中的编号和字符,以*分隔开。
		switch(kind)
		{
		case 1:	if(frame_expected == num)
			{
				SendAck.Format(_T("共%d帧-第%d帧-%d类帧-%s"), count,num,2,"返回帧");
				iLen1 = SendAck.GetLength();
				iSent1 = m_sConnectSocket.Send(LPCTSTR(SendAck),iLen1);
				if (iSent1 == SOCKET_ERROR)
				{SetTimer(1000,2000, NULL);}              // 发送失败或超时
				else
				{
					if(count == num+1)
					{
					m_ctrStatus.AddString(strStatusMsg+" 收到最后一帧:"+strBuf);         //写日志
						m_ctrStatus.SetCurSel(m_ctrStatus.GetCount()-1);    
						strRecvd.Insert(num*16, cBuf);
						strRecvd.Insert((num+1)*16,'\0'); 

⌨️ 快捷键说明

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