📄 tftpsrvdlg.cpp
字号:
// TftpSrvDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TftpSrv.h"
#include "TftpSrvDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CTftpSrvDlg dialog
CTftpSrvDlg::CTftpSrvDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTftpSrvDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTftpSrvDlg)
// 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);
RQHead = NULL;
}
void CTftpSrvDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTftpSrvDlg)
DDX_Control(pDX, IDC_LIST1, m_ListTxt);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTftpSrvDlg, CDialog)
//{{AFX_MSG_MAP(CTftpSrvDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_DATADISPLAY,OnSetDlg)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTftpSrvDlg message handlers
BOOL CTftpSrvDlg::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
OnSockInit((u_long)INADDR_ANY, 69);
OnThread();
return TRUE; // return TRUE unless you set the focus to a control
}
void CTftpSrvDlg::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 CTftpSrvDlg::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 CTftpSrvDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//OnSockInit 套接字初始化函数
BOOL CTftpSrvDlg::OnSockInit(u_long ip, u_short port)
{
m_Socket = socket(AF_INET,SOCK_DGRAM,0);
if(m_Socket == INVALID_SOCKET)
{
MessageBox("socket erro");
return FALSE;
}
SOCKADDR_IN Srv_Addr_IN;
Srv_Addr_IN.sin_addr.S_un.S_addr = htonl(ip);
Srv_Addr_IN.sin_family = AF_INET;
Srv_Addr_IN.sin_port = htons(port);
if(INVALID_SOCKET ==(bind(m_Socket,(SOCKADDR*)&Srv_Addr_IN,sizeof(Srv_Addr_IN))))
{
MessageBox("Bind Error!");
return FALSE;
}
return TRUE;
}
//====================================================
//OnThread() 创建负责接收客户端请求连接的线程 RcvMainProc
BOOL CTftpSrvDlg::OnThread()
{
RcvParam *rcvParam = new RcvParam;
rcvParam->m_Socket = m_Socket;
rcvParam->m_hwnd = this->m_hWnd;
HANDLE handl = CreateThread(NULL,0,RcvMainProc,rcvParam,0,NULL);
CloseHandle(handl);
return TRUE;
}
// RcvMainProc 负责接收客户端请求连接的线程 RcvMainProc
//每接收一个尚未存在的客户请求,则创建一个新的进程(RcvProc)负责文件的传输
DWORD WINAPI RcvMainProc(LPVOID Rcvparam)
{
int retval;
SOCKET sock = ((RcvParam*)Rcvparam)->m_Socket;
HWND hwnd = ((RcvParam*)Rcvparam)->m_hwnd;
delete Rcvparam;
SOCKADDR_IN AddrFrom;
int len = sizeof(AddrFrom);
RWRQ *RcvMsg = new RWRQ;
while(1)
{
memset(RcvMsg,0,sizeof(RcvMsg));
retval = recvfrom(sock,(char*)RcvMsg,sizeof(RWRQ),
0,(SOCKADDR *)&AddrFrom, &len);
LPARAMSTRUCT *ls = new LPARAMSTRUCT;
memset(ls,0,sizeof(LPARAMSTRUCT));
ls->m_TftpHwnd = hwnd;
ls->pcode = ntohs(RcvMsg->pcode);
strcpy(ls->fileandmode,RcvMsg->fileandmode);
ls->Sock_Addr = AddrFrom;
ls->m_TftpHwnd = hwnd;
if( CTftpSrvDlg::NotExist(RcvMsg->fileandmode,AddrFrom))
{
HANDLE handl = CreateThread(NULL,0,RcvProc,(LPVOID)ls,0,NULL);
CloseHandle(handl);
}
}
return 0;
}
//RcvProc 负责文件的传输,该函数是实现多线程的关键
//RcvProc 参数 lpParameter 包含主窗口句柄、请求操作码、
//请求文件和客户端的地址结构
//相关对象 Cexcute ,实现文件传输、差错控制等
DWORD WINAPI RcvProc(LPVOID lpParameter)
{
u_short pcode = (((LPARAMSTRUCT*)lpParameter))->pcode;
static i = 0;
char buf[30];
strcpy(buf,((LPARAMSTRUCT*)lpParameter)->fileandmode);
pcode = ((LPARAMSTRUCT*)lpParameter)->pcode;
SOCKADDR_IN RcvAddr ;
RcvAddr = ((LPARAMSTRUCT*)lpParameter)->Sock_Addr;
HWND hwnd = ((LPARAMSTRUCT*)lpParameter)->m_TftpHwnd;
delete lpParameter;
Cexcute *Exc = new Cexcute(pcode,buf,RcvAddr,hwnd);
delete Exc;
return 0;
}
//测试当前线程中是否有相同的请求存在
BOOL CTftpSrvDlg::NotExist(char * FileName, SOCKADDR_IN &RcvAddr)
{
//There had a connect in the same Host and refuse to do more transferring,then return True.
ExistRQ * tmp = new ExistRQ;
strcpy(tmp->m_File,FileName);
tmp->m_Port = RcvAddr.sin_port;
tmp->m_Addr = RcvAddr.sin_addr;
tmp->next = NULL;
// AfxMessageBox(tmp->m_File);
if(RQHead == NULL)
{
RQHead = tmp;
return TRUE;
}
else
{
ExistRQ *temp = RQHead;
while(temp != NULL && temp->m_File != NULL)
{
if(temp->m_Addr.S_un.S_addr == RcvAddr.sin_addr.S_un.S_addr
&& (temp->m_Port == RcvAddr.sin_port))// || //if(strcmp(temp->m_File,FileName) == 0)
{
delete tmp;
return FALSE;
}
temp = temp->next;
}
tmp->next = RQHead;
RQHead = tmp;
return TRUE;
}
}
void CTftpSrvDlg::OnSetDlg(WPARAM wParam,LPARAM lParam)
{
static ListCount=0;
CString str = (char*)lParam;
m_ListTxt.InsertString(ListCount,str);
ListCount++;
}
BEGIN_EVENTSINK_MAP(CTftpSrvDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CTftpSrvDlg)
ON_EVENT(CTftpSrvDlg, IDC_COMMANDBUTTON1, -600 /* Click */, OnClickCommandbutton1, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
//路径选择按钮 activX 控件函数
void CTftpSrvDlg::OnClickCommandbutton1()
{
// TODO: Add your control notification handler code here
CString strPath;
BROWSEINFO bInfo;
LPITEMIDLIST pidl;
memset(&bInfo,0,sizeof(bInfo));
bInfo.hwndOwner = m_hWnd;
bInfo.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
if((pidl = SHBrowseForFolder(&bInfo)) == NULL) return ;
char temp[MAX_PATH];
if((SHGetPathFromIDList(pidl,temp)) == NULL)
return ;
strPath.Format(temp);
SetDlgItemText(IDC_EDIT1,strPath);
SetCurrentDirectory(temp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -