// ClientDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Client.h"
#include "ClientDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//下载文件线程函数参数
typedef struct _FileDownloadParam
{
unsigned short ID; //标识
hdr header; //包头
SOCKET s; //套接字
CClientDlg* ptr; //主线程
string strFilePath;//下载文件路径
}FileDownloadParam, *pFileDownloadParam;
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CClientDlg dialog
CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CClientDlg)
m_strSaveFile = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_hThreadSR = NULL;
//m_hThreadRecv = NULL;
//memset(m_hThreadRecvF, 0, MAX_RECVFILE_THREAD);
m_bConning = FALSE;
m_nReqCur = REQNON;
//memset(m_sFData, INVALID_SOCKET, MAX_RECVFILE_THREAD);
//初始化套接字等
m_ulFileSize = 0;
m_ulCurTotal = 0;
}
CClientDlg::~CClientDlg()
{
}
void CClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CClientDlg)
DDX_Control(pDX, IDC_PROGRESS, m_ctlProgress);
DDX_Control(pDX, IDC_TREE, m_ctlTree);
DDX_Control(pDX, IDC_IPADDRESS, m_ctlIP);
DDX_Text(pDX, IDC_EDIT_FILEPATH, m_strSaveFile);
DDX_Control(pDX, IDC_STATIC_TIP, m_ctlTip);
DDX_Control(pDX, IDC_STATIC_PROGRESS, m_ctlFileDLTip);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
//{{AFX_MSG_MAP(CClientDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
ON_BN_CLICKED(IDC_BUTTON_SETPATH, OnButtonSetFilePath)
ON_NOTIFY(TVN_ITEMEXPANDING, IDC_TREE, OnItemexpandingTree)
ON_NOTIFY(NM_DBLCLK, IDC_TREE, OnDblclkTree)
ON_WM_TIMER()
ON_WM_CLOSE()
ON_WM_DESTROY()
ON_COMMAND(ID_MENUITEM_DOWNLOAD, OnFileDownload)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClientDlg message handlers
BOOL CClientDlg::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
InitSocket(); //初始化Windows Sockets
m_ctlIP.SetAddress(127, 0, 0, 1); //设置服务器地址
m_ctlTree.InsertItem(_T("我的电脑"),TVI_ROOT, TVI_LAST ); //插入根节点
m_hTreeCur = m_ctlTree.GetRootItem();
SetTimer(TIMER, 10, NULL); //设置定时器
return TRUE;
}
void CClientDlg::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 CClientDlg::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 CClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/*
* 初始化Windows Sockets
*/
void CClientDlg::InitSocket(void)
{
WORD wVersionRequested; //请求socket版本
WSADATA wsaData; //wsaData结构
int nErrCode; //返回值
wVersionRequested = MAKEWORD( 2, 2 );//请求windows Sockets 2.2版本
nErrCode = WSAStartup( wVersionRequested, &wsaData );
if ( 0 != nErrCode )
{
return;
}
//创建套接字
m_sHost = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (INVALID_SOCKET == m_sHost)
{
return;
}
//设置系统接收数据为默认的BUF_TIMES倍
unsigned int uiRcvBuf;
int uiRcvBufLen = sizeof(uiRcvBuf);
nErrCode= getsockopt(m_sHost, SOL_SOCKET, SO_RCVBUF,(char*)&uiRcvBuf, &uiRcvBufLen);
if (SOCKET_ERROR == nErrCode)
{
return;
}
uiRcvBuf *= BUF_TIMES;
nErrCode = setsockopt(m_sHost, SOL_SOCKET, SO_RCVBUF,(char*)&uiRcvBuf, uiRcvBufLen);
if (SOCKET_ERROR == nErrCode)
{
AfxMessageBox(_T("修改系统发送数据缓冲区失败!"));
}
//检查设置系统接收数据缓冲区是否成功
unsigned int uiNewRcvBuf;
getsockopt(m_sHost, SOL_SOCKET, SO_RCVBUF,(char*)&uiNewRcvBuf, &uiRcvBufLen);
if (SOCKET_ERROR == nErrCode || uiNewRcvBuf != uiRcvBuf)
{
AfxMessageBox(_T("修改系统发送数据缓冲区失败!"));;
}
}
/*
* 释放套接字占用的资源
*/
void CClientDlg::UnInitSocket(void)
{
closesocket(m_sHost);
WSACleanup();
}
/*
* 连接服务器
*/
void CClientDlg::OnButtonConnect()
{
//获得服务器的IP地址
UpdateData(TRUE);
DWORD dwServIP;
m_ctlIP.GetAddress(dwServIP);
//服务器套结字地址
SOCKADDR_IN servAddr;
servAddr.sin_family = AF_INET;
servAddr.sin_addr.S_un.S_addr = htonl(dwServIP);
servAddr.sin_port = htons(SERVERPORT);
//连接服务器
int nErrCode;
nErrCode = connect(m_sHost,(sockaddr*)&servAddr, sizeof(SOCKADDR_IN));
if (SOCKET_ERROR == nErrCode)
{
AfxMessageBox("连接服务器失败!",MB_OK, 0);
return;
}
//显示连接服务器成功信息
m_ctlTip.SetWindowText(_T("连接服务器成功!"));
m_bConning = TRUE;
//请求服务器目录
m_nReqCur = REQROOT;
//创建发送和接收目录线程
DWORD dwThread;
m_hThreadSR = CreateThread(NULL, 0, SendAndRecvDirInforThread, this, 0, &dwThread);
CloseHandle(m_hThreadSR);
//设置连接服务器按钮为无效状态
CButton *pBt = (CButton*)this->GetDlgItem(IDC_BUTTON_CONNECT);
pBt->EnableWindow(FALSE);
}
/*
* 设置文件保存位置
*/
void CClientDlg::OnButtonSetFilePath()
{
TCHAR tcSaveFilePath[MAX_PATH]; //临时字符数组
BROWSEINFO browInfo = {0}; //清零
browInfo.hwndOwner = GetSafeHwnd();
browInfo.ulFlags = BIF_RETURNFSANCESTORS ;
browInfo.lpszTitle = "S