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

📄 serpentgbndlg.cpp

📁 本人本科的毕业设计。基于Serpent密码的回退N协议的数据传输。希望可供大家参考。谢谢诶!
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// SerpentGBNDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SerpentGBN.h"
#include "SerpentGBNDlg.h"
#include "Serpent.h"
#include "protocol.h"
#include "DlgIntroduce.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
static UINT comstat=0;
volatile  frame frame_ack;
volatile frame send_buffer[4];
volatile HANDLE hThreadClientSend,hThreadClientRecv;
volatile HANDLE hThreadServerSend,hThreadServerRecv;
volatile static HANDLE hEvent,hEvent1;
volatile  unsigned int seq_expected=0;
volatile  unsigned int seq_to_send=0;
volatile  unsigned int ack_expected=0;
volatile HANDLE hMutex;
volatile static unsigned long plaintext[4],ciphertext[4];
volatile static frame frame_to_send,frame_received;
 static	UINT file1,over;

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()

/////////////////////////////////////////////////////////////////////////////
// CSerpentGBNDlg dialog

CSerpentGBNDlg::CSerpentGBNDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSerpentGBNDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSerpentGBNDlg)
	m_Port = 6000;
	currentSize=0;
    file1=0;
	over=0;
     for(int i=0;i<50;i++)
		 cipherStore[i]=0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSerpentGBNDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSerpentGBNDlg)
	DDX_Control(pDX, IDC_IPADDRESS, m_IP);
	DDX_Control(pDX, IDC_LIST_Disp, m_Disp);
	DDX_Text(pDX, IDC_PORT, m_Port);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSerpentGBNDlg, CDialog)
	//{{AFX_MSG_MAP(CSerpentGBNDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_RADIO_SERVER, OnRadioServer)
	ON_BN_CLICKED(IDC_RADIO_CLIENT, OnRadioClient)
	ON_BN_CLICKED(IDC_MakeKey, OnMakeKey)
	ON_BN_CLICKED(IDC_jiami, Onjiami)
	ON_BN_CLICKED(IDC_jiemi, Onjiemi)
	ON_BN_CLICKED(IDC_SelectEFile, OnSelectEFile)
	ON_BN_CLICKED(IDC_Encrypt, OnEncrypt)
	ON_BN_CLICKED(IDC_SelectDFile, OnSelectDFile)
	ON_BN_CLICKED(IDC_Decrypt, OnDecrypt)
	ON_BN_CLICKED(IDC_BEGIN, OnBegin)
	ON_BN_CLICKED(IDC_DISCONNECT, OnDisconnect)
	ON_BN_CLICKED(IDC_Send, OnSend)
	ON_BN_CLICKED(IDC_Rcv, OnRcv)
	ON_BN_CLICKED(IDC_introduction, Onintroduction)
	ON_BN_CLICKED(IDC_Send_File, OnSendFile)
	ON_BN_CLICKED(IDC_Recv_File, OnRecvFile)
	ON_BN_CLICKED(IDC_fileStore, OnfileStore)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSerpentGBNDlg message handlers

BOOL CSerpentGBNDlg::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
	strDisp=_T(""); 

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

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

  BOOL   m_Server=FALSE;   
  BOOL   m_Client=FALSE; 
  CString srcPath;
CString desPath;
  Serpent SerpentProc;

int CSerpentGBNDlg::GetLocalHostInfo(CString &strHostName, CString &strIPAddress)
{
	char szHostName[256];
	
	if(gethostname(szHostName, sizeof(szHostName)))
	{
		strHostName = _T("");
		MessageBox("cuowu",_T("错误"), MB_ICONHAND|MB_OK);
		return -1;
	}
	
	PHOSTENT hostinfo;
	if((hostinfo = gethostbyname(szHostName)) == NULL)
		return GetLastError();
	
	LPCSTR ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
	
	strIPAddress = ip;
	strHostName = szHostName;
	
	return 0;
}
void CSerpentGBNDlg::OnRadioServer() 
{
	// TODO: Add your control notification handler code here
		
	CString strHostName,strIPAddress;
    GetLocalHostInfo(strHostName, strIPAddress);
//		return FALSE;	
	GetDlgItem(IDC_IPADDRESS)->SetWindowText(strIPAddress);
	GetDlgItem(IDC_IPADDRESS)->EnableWindow(FALSE);

	GetDlgItem(IDC_BEGIN)->SetWindowText(_T("启  动(&B)"));
	GetDlgItem(IDC_DISCONNECT)->SetWindowText(_T("关  闭(&D)"));
    m_Server=TRUE;

}


void CSerpentGBNDlg::OnRadioClient() 
{
	// TODO: Add your control notification handler code here
    CString strIPAddress="127.0.0.1";

	GetDlgItem(IDC_IPADDRESS)->SetWindowText(_T(""));
	GetDlgItem(IDC_IPADDRESS)->EnableWindow(TRUE);
	GetDlgItem(IDC_BEGIN)->SetWindowText(_T("连  接(&B)"));
	GetDlgItem(IDC_DISCONNECT)->SetWindowText(_T("断  开(&D)"));
    
	GetDlgItem(IDC_IPADDRESS)->SetWindowText(strIPAddress);
    m_Client=TRUE;

}

void CSerpentGBNDlg::OnBegin() 
{
	// TODO: Add your control notification handler code here
	if(m_Server==TRUE)
	{  
	 m_Disp.AddString("服务器启动!");
	 m_Disp.SetCurSel(m_Disp.GetCount()-1);   //垂直滚动到底部	   
    m_sockSrv=socket(AF_INET,SOCK_STREAM,0);
    if(m_sockSrv==SOCKET_ERROR)
	{ MessageBox("sock create fail !");
	   WSACleanup();  
	}

	SOCKADDR_IN addrSrv1;
	addrSrv1.sin_family=AF_INET;
	addrSrv1.sin_addr.s_addr=htonl(INADDR_ANY);
    addrSrv1.sin_port=htons(6000);
	m_Disp.AddString("创建套接字成功!"); 
	m_Disp.SetCurSel(m_Disp.GetCount()-1);
	if(bind(m_sockSrv,(struct sockaddr FAR *)&addrSrv1,sizeof(SOCKADDR))==SOCKET_ERROR)
	MessageBox("bind is the error");
	m_Disp.AddString("绑定成功!"); 
	m_Disp.SetCurSel(m_Disp.GetCount()-1);

	listen(m_sockSrv,5);
	SOCKADDR_IN addrClient1;
    m_Disp.AddString("开始监听!"); 
	m_Disp.SetCurSel(m_Disp.GetCount()-1);
     
	int len=sizeof(SOCKADDR);
     m_sockConn=accept(m_sockSrv,(SOCKADDR*)&addrClient1,&len);
    m_Disp.AddString("接受连接请求!"); 
	m_Disp.SetCurSel(m_Disp.GetCount()-1);
	 m_sockResponse=socket(AF_INET,SOCK_STREAM,0);
    if(m_sockResponse==SOCKET_ERROR)
	{ MessageBox("sock create fail !");
	   WSACleanup();  
	}
	SOCKADDR_IN addrSrv2;
	addrSrv2.sin_family=AF_INET;
	addrSrv2.sin_addr.s_addr=htonl(INADDR_ANY);
    addrSrv2.sin_port=htons(6001);

	if(bind(m_sockResponse,(struct sockaddr FAR *)&addrSrv2,sizeof(SOCKADDR))==SOCKET_ERROR)
	MessageBox("bind is the error");
	listen(m_sockResponse,5);
	SOCKADDR_IN addrResponse;
	 len=sizeof(SOCKADDR);
   frame_ack.frameType=data;
   m_sockConn1=accept(m_sockResponse,(SOCKADDR*)&addrResponse,&len);

  
	}
	else
	{  
	 m_Disp.AddString("客户端启动!");
	 m_Disp.SetCurSel(m_Disp.GetCount()-1);   //垂直滚动到底部
	  GetDlgItem(IDC_IPADDRESS)->EnableWindow(FALSE);	  	
	  
     DWORD dwIP,dwIP1;
     m_sockClient=socket(AF_INET,SOCK_STREAM,0);
    if(m_sockClient==SOCKET_ERROR)
	{  ::AfxMessageBox("sock create fail !\n");
	   WSACleanup();  
	}
     m_Disp.AddString("创建套接字成功!"); 
	 m_Disp.SetCurSel(m_Disp.GetCount()-1);

	((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS))->GetAddress(dwIP);
	SOCKADDR_IN addrSrv3;
	addrSrv3.sin_family=AF_INET;
	addrSrv3.sin_addr.s_addr=htonl(dwIP);
    addrSrv3.sin_port=htons(6000);
    
    m_Disp.AddString("开始连接!"); 
  	m_Disp.SetCurSel(m_Disp.GetCount()-1);
	connect(m_sockClient,(struct sockaddr FAR *)&addrSrv3,sizeof(SOCKADDR));
    m_Disp.AddString("连接成功!"); 
  	m_Disp.SetCurSel(m_Disp.GetCount()-1);

	m_sockSend=socket(AF_INET,SOCK_STREAM,0);
    if(m_sockSend==SOCKET_ERROR)
	{  ::AfxMessageBox("sock create fail !\n");
	   WSACleanup();  
	}
	((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS))->GetAddress(dwIP1);
	SOCKADDR_IN addrSrv4;
	addrSrv4.sin_family=AF_INET;
	addrSrv4.sin_addr.s_addr=htonl(dwIP1);
    addrSrv4.sin_port=htons(6001);

   connect(m_sockSend,(struct sockaddr FAR *)&addrSrv4,sizeof(SOCKADDR));

}
	GetDlgItem(IDC_BEGIN)->EnableWindow(FALSE);
	GetDlgItem(IDC_PORT)->EnableWindow(FALSE);
}
void CSerpentGBNDlg::OnDisconnect() 
{
	// TODO: Add your control notification handler code here
  if(m_Server==TRUE)
  {
    TerminateThread(hThreadServerRecv, 0);
	CloseHandle(hThreadServerRecv);
    
	TerminateThread(hThreadServerSend, 0);
	CloseHandle(hThreadServerSend);
    closesocket(m_sockConn);
	closesocket(m_sockSrv);
	closesocket(m_sockResponse);
    closesocket(m_sockConn1);
    GetDlgItem(IDC_IPADDRESS)->EnableWindow(TRUE);
    GetDlgItem(IDC_BEGIN)->EnableWindow(TRUE);
	GetDlgItem(IDC_PORT)->EnableWindow(TRUE);
	m_Server=FALSE;
  }
  else
	  if(m_Client==TRUE)
	  { 
	TerminateThread(hThreadClientRecv, 0);
	CloseHandle(hThreadClientRecv);
	TerminateThread(hThreadClientSend, 0);
	CloseHandle(hThreadClientSend);

	closesocket(m_sockClient);
	closesocket(m_sockSend);

    GetDlgItem(IDC_IPADDRESS)->EnableWindow(TRUE);
    GetDlgItem(IDC_BEGIN)->EnableWindow(TRUE);
	GetDlgItem(IDC_PORT)->EnableWindow(TRUE);
	m_Client=FALSE;
 }
}


void CSerpentGBNDlg::OnSend() 
{
	// TODO: Add your control notification handler code here

hMutex=CreateMutex(NULL,FALSE,NULL);
hEvent=CreateEvent(NULL,true,false,NULL);
hThreadServerSend=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ServerFunc,this,0,NULL);
hThreadServerRecv=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)responseFunc,this,0,NULL);

}

void CSerpentGBNDlg::OnRcv() 
{
	// TODO: Add your control notification handler code here
hMutex=CreateMutex(NULL,FALSE,NULL);
hEvent=CreateEvent(NULL,true,true,NULL);
hEvent1=CreateEvent(NULL,true,false,NULL);

     
hThreadClientRecv=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ClientFunc,this,CREATE_SUSPENDED,NULL);
hThreadClientSend=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)sendFunc,this,CREATE_SUSPENDED,NULL);

ResumeThread(hThreadClientRecv);
ResumeThread(hThreadClientSend);	

}

void CALLBACK CSerpentGBNDlg::ServerFunc(LPVOID lParam)
{
	CString   strShowDisp1   =_T("");   
    CString   strTempDisp1   =_T(""); 
	DWORD dwRet;

    CSerpentGBNDlg *pDlg=(CSerpentGBNDlg*)lParam;
 //  
    while(1)
	{  
        Sleep(500);
		//	Sleep(1000); 	
   //	WaitForSingleObject(hEvent,INFINITE); 	
     dwRet=WaitForSingleObject(hMutex,INFINITE);
			if(dwRet == WAIT_FAILED)
			{ ReleaseMutex(hMutex); 
				continue;
			}  
			if((pDlg->currentSize<4)&&(seq_to_send<=pDlg->frameCount-1))
			{
	        frame_to_send.totalFrame=pDlg->frameCount;
			frame_to_send.info=pDlg->cipherTo[seq_to_send];
	        frame_to_send.seq=seq_to_send;	  
	        dwRet=send(pDlg->m_sockConn,(char*)&frame_to_send,sizeof(frame_to_send),0);
         	if(dwRet == SOCKET_ERROR)  
			continue;
			pDlg->SetTimer(seq_to_send,50, NULL); 
	        pDlg->currentSize++;
		   strTempDisp1.Format("发送%d帧",(seq_to_send)%8); 
           pDlg->m_Disp.AddString(strTempDisp1); 
	       pDlg->m_Disp.SetCurSel(pDlg->m_Disp.GetCount()-1);   //垂直滚动到底部
           strTempDisp1.Empty();
		   seq_to_send++;
			}
			else
			if(pDlg->currentSize==4)
			{
			pDlg->m_Disp.AddString("发送窗口满!"); 
	        pDlg->m_Disp.SetCurSel(pDlg->m_Disp.GetCount()-1); 
			}
               
			//	{//ReleaseMutex(hMutex);
			//continue;}
//			}
//	 inc(seq_to_send);
     ReleaseMutex(hMutex); 
  //   ResetEvent(hEvent);
//	 SetEvent(hEvent1);
	}
 closesocket(pDlg->m_sockConn);

}


void CALLBACK CSerpentGBNDlg::responseFunc(LPVOID lParam)
{
	CString   strShowDisp1   =_T("");   
    CString   strTempDisp1   =_T(""); 

    CSerpentGBNDlg *pDlg=(CSerpentGBNDlg*)lParam;
	DWORD dwRet;

  // Sleep(1000);
  while(1)
  {  	 
	 Sleep(1000);
   //  WaitForSingleObject(hEvent1,INFINITE); 
     WaitForSingleObject(hMutex,INFINITE);		
     
     dwRet=recv(pDlg->m_sockConn1,(char*)&frame_ack,sizeof(frame_ack),0); 					
     if(frame_ack.frameType==nak)
	 {		 
	 seq_to_send=frame_ack.seq;	
	 strTempDisp1.Format("重传%d帧",(frame_ack.seq)%8); 
	 pDlg->m_Disp.AddString(strTempDisp1); 
	 pDlg->m_Disp.SetCurSel(pDlg->m_Disp.GetCount()-1);
     ack_expected=frame_ack.seq;
      pDlg->currentSize--;
	 }
   else if((frame_ack.frameType==ack)&&(frame_ack.seq==ack_expected))
   {  
	  strTempDisp1.Format("收到%d帧确认",(frame_ack.seq)%8); 
	  pDlg->m_Disp.AddString(strTempDisp1); 
   	  pDlg->m_Disp.SetCurSel(pDlg->m_Disp.GetCount()-1);
              ack_expected++;
			  pDlg->currentSize--;
	 if((frame_ack.seq==pDlg->frameCount-1)&&pDlg->currentSize==0)
	 { 
     pDlg->m_Disp.AddString("发送完毕!"); 
	 pDlg->m_Disp.SetCurSel(pDlg->m_Disp.GetCount()-1);
     ack_expected=0;
      seq_to_send=0;
	 pDlg->frameCount=0;
     pDlg->currentSize=0;
 	 TerminateThread(hThreadServerSend, 0);
	 CloseHandle(hThreadServerSend);
	 TerminateThread(hThreadServerRecv, 0);
	 CloseHandle(hThreadServerRecv);

     CloseHandle(hMutex);
		}
	   }
     
//ResetEvent(hEvent);
//SetEvent(hEvent);

⌨️ 快捷键说明

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