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

📄 appfiletransfersdlg.cpp

📁 使用TCP协议实现网络传输文件功能
💻 CPP
字号:
// AppFileTransfersDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AppFileTransfers.h"
#include "AppFileTransfersDlg.h"

#include <comdef.h>

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About


DWORD WINAPI SendThread(LPVOID pParam)
{
	try
	{
		if (CreateTransfersServer())
		{
			if (!SendTransfersFile())
			{
		//		::MessageBox (NULL,"发送失败","提示信息",MB_OK);
			}else
			{
		//		::MessageBox(NULL,"发送完毕","提示信息",MB_OK);
			}
		}
	//	if (CloseTransfersServer())
	//	{
	//		;
	//	}
	}catch (_com_error e)
	{
		;	
	}
	return 0;
}

DWORD WINAPI ReceiveThread(LPVOID pParam)
{
	try
	{
		if (CreateTransfersClient())
		{
			if (!ReceiveTransfersFile())
			{
		//		::MessageBox(NULL,"接收失败","提示信息",MB_OK);
			
			}else
			{
		//		::MessageBox(NULL,"接收完毕","提示信息",MB_OK);
			}
		}
	//	if (CloseTransfersClient())
	//	{
	//		;
	//	}
	}catch (_com_error e)
	{
		;	
	}
	return 0;
}
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()

/////////////////////////////////////////////////////////////////////////////
// CAppFileTransfersDlg dialog

CAppFileTransfersDlg::CAppFileTransfersDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAppFileTransfersDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAppFileTransfersDlg)
	m_editip = _T("");
	m_editport = _T("");
	m_file1 = _T("");
	m_file2 = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAppFileTransfersDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAppFileTransfersDlg)
	DDX_Control(pDX, IDC_Receive, m_receive);
	DDX_Control(pDX, IDC_Send, m_send);
	DDX_Text(pDX, IDC_EDITIP, m_editip);
	DDX_Text(pDX, IDC_EDITPORT, m_editport);
	DDX_Text(pDX, IDC_EDITFILE1, m_file1);
	DDX_Text(pDX, IDC_EDITFILE2, m_file2);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAppFileTransfersDlg, CDialog)
	//{{AFX_MSG_MAP(CAppFileTransfersDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_Send, OnSend)
	ON_BN_CLICKED(IDC_Receive, OnReceive)
	ON_BN_CLICKED(IDC_Server, OnServer)
	ON_BN_CLICKED(IDC_Cilent, OnCilent)
	ON_BN_CLICKED(IDC_CLOSESERVER, OnCloseserver)
	ON_BN_CLICKED(IDC_BADD, OnBadd)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAppFileTransfersDlg message handlers

BOOL CAppFileTransfersDlg::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

	if (GetIP () != NULL)
	{
		m_editip=GetIP ();
	}else
	{
		m_editip="255.255.255.255";
	}
	m_editport="7878";
	m_file1="f:\\temp\\1.rar";
	m_file2="f:\\temp";

	UpdateData(FALSE);

	m_state="";
	m_cilent=false;

	SetReName(true);


	SetTimer(1,200,NULL);

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

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

void CAppFileTransfersDlg::OnSend() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
		if (!SetSendFilePath(m_file1))
		{
			MessageBox("设置文件路径失败","提示信息",MB_OK);
			return;
		}
		if (!SetReceivePath(m_file2))
		{
			MessageBox("设置文件路径失败","提示信息",MB_OK);
			return;
		}
			if (SendTransfersFile())
			{
				m_send.EnableWindow (FALSE);
				m_receive.EnableWindow (FALSE);
				m_send_rece=true;
			}

}

void CAppFileTransfersDlg::OnReceive() 
{
	// TODO: Add your control notification handler code here
		UpdateData(TRUE);
		if (!SetSendFilePath(m_file1))
		{
			MessageBox("设置文件路径失败","提示信息",MB_OK);
			return;
		}
		if (!SetReceivePath(m_file2))
		{
			MessageBox("设置文件路径失败","提示信息",MB_OK);
			return;
		}
		if (ReceiveTransfersFile())
		{
			m_send.EnableWindow (FALSE);
			m_receive.EnableWindow (FALSE);
			m_send_rece=false;
		}
}

void CAppFileTransfersDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	CloseTransfersServer();
	Sleep(1000);
	CDialog::OnCancel();
}

void CAppFileTransfersDlg::OnServer() 
{
	// TODO: Add your control notification handler code here

	try
	{	
			
		UpdateData(TRUE);
		char *p = m_editport.GetBuffer(m_editport.GetLength());
		int newport=atoi(p);
		if (!SetTransfersPort(newport)) 
		{
			MessageBox("设置端口失败","提示信息",MB_OK);
			return ;
		}
		if (!SetTransfersIP(m_editip)) 
		{
			MessageBox("设置IP地址失败","提示信息",MB_OK);
			return;
		}
		if (!SetSendFilePath(m_file1))
		{
			MessageBox("设置文件路径失败","提示信息",MB_OK);
			return ;
		}
		if (!SetReceivePath(m_file2))
		{
			MessageBox("设置文件路径失败","提示信息",MB_OK);
			return ;
		}
		if (CreateTransfersServer())
		{
			char myEdition[20];
			strcpy(myEdition,"服务器  ");
			strcat(myEdition,GetTransfersEdition());
			SetWindowText(myEdition);
			CWnd * pwnd1 = GetDlgItem(IDC_Server);
			pwnd1->EnableWindow(false);
			CWnd * pwnd2 = GetDlgItem(IDC_Cilent);
			pwnd2->EnableWindow(false);
			CWnd * pwnd3 = GetDlgItem(IDC_Send);
			pwnd3->EnableWindow(false);
			CWnd * pwnd4 = GetDlgItem(IDC_Receive);
			pwnd4->EnableWindow(false);


		}
		return ;
	}catch (_com_error e)
	{
		MessageBox("发送设置错误","提示信息",MB_OK);	
	}
}

void CAppFileTransfersDlg::OnCilent() 
{
	// TODO: Add your control notification handler code here
	try
	{	
		UpdateData(TRUE);
		char *p = m_editport.GetBuffer(m_editport.GetLength());
		int newport=atoi(p);
		if (!SetTransfersPort(newport)) 
		{
			MessageBox("设置端口失败","提示信息",MB_OK);
			return;
		}
		if (!SetTransfersIP((CString)m_editip)) 
		{
			MessageBox("设置IP地址失败","提示信息",MB_OK);
			return;
		}
		if (!SetSendFilePath(m_file1))
		{
			MessageBox("设置文件路径失败","提示信息",MB_OK);
			return;
		}
		if (!SetReceivePath(m_file2))
		{
			MessageBox("设置文件路径失败","提示信息",MB_OK);
			return;
		}
		if (CreateTransfersClient())
		{
			char myEdition[20];
			strcpy(myEdition,"客户端  ");
			strcat(myEdition,GetTransfersEdition());
			SetWindowText(myEdition);
			CWnd * pwnd1 = GetDlgItem(IDC_Server);
			pwnd1->EnableWindow(false);
			CWnd * pwnd2 = GetDlgItem(IDC_Cilent);
			pwnd2->EnableWindow(false);
			m_cilent=true;
		}	
		
return ;
	}catch (_com_error e)
	{
		MessageBox("接收设置错误","提示信息",MB_OK);	
	}
}

void CAppFileTransfersDlg::OnCloseserver() 
{
	// TODO: Add your control notification handler code here
	CloseTransfersServer();
}

void CAppFileTransfersDlg::OnBadd() 
{
	// TODO: Add your control notification handler code here
		UpdateData(TRUE);
		if (!SetSendFilePath(m_file1))
		{
			MessageBox("设置文件路径失败","提示信息",MB_OK);
			return;
		}
		if (!SetReceivePath(m_file2))
		{
			MessageBox("设置文件路径失败","提示信息",MB_OK);
			return;
		}
		MessageBox("设置成功!\t","提示信息",MB_OK);
}

void CAppFileTransfersDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if (m_cilent==true)
	{
		
		if ((CString)GetTransfersState()=="C")
		{
			if(m_send_rece==true)
			{
				char myEdition[200];
				strcpy(myEdition,"客户端  ");
				strcat(myEdition,GetTransfersEdition());
				strcat(myEdition,"       发送文件中...");
				m_state=myEdition;
			}else
			{
				char myEdition[200];
				strcpy(myEdition,"客户端  ");
				strcat(myEdition,GetTransfersEdition());
				strcat(myEdition,"       接收文件中...");
				m_state=myEdition;
			}
		}else
		{
			if(m_send_rece==true)
			{
				char myEdition[200];
				strcpy(myEdition,"客户端  ");
				strcat(myEdition,GetTransfersEdition());
				if ((CString)GetTransfersState()=="A")
				{
					strcat(myEdition,"       发送文件完毕");
				}else
				{
					strcat(myEdition,"       发送文件错误");
				}
				m_state=myEdition;
			}else
			{
				char myEdition[200];
				strcpy(myEdition,"客户端  ");
				strcat(myEdition,GetTransfersEdition());
				if ((CString)GetTransfersState()=="A")
				{
					strcat(myEdition,"       接收文件完毕");
				}else
				{
					strcat(myEdition,"       接收文件错误");
				}
				m_state=myEdition;
			}
			m_send.EnableWindow (TRUE);
			m_receive.EnableWindow (TRUE);
		}
		SetWindowText(m_state);
	}	
	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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