📄 serverdlg.cpp
字号:
// ServerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Server.h"
#include "ServerDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CServerDlg dialog
CServerDlg::CServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CServerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CServerDlg)
// 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);
}
void CServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CServerDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
//{{AFX_MSG_MAP(CServerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ACCEPT, OnAccept)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CServerDlg message handlers
BOOL CServerDlg::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
CString str;
str.Format("Idle");
SetSysText(str);
CProgressCtrl *pCtrl=(CProgressCtrl*)GetDlgItem(IDC_PROGRESS);
pCtrl->SetRange(0,100);
pCtrl->SetPos(0);
m_pSocket=NULL;
return TRUE; // return TRUE unless you set the focus to a control
}
void CServerDlg::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 CServerDlg::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 CServerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
UINT CServerDlg::GetPortNumFromCtrl()
{
CEdit *pEdit=(CEdit*)GetDlgItem(IDC_PORTNUM);
CString str;
pEdit->GetWindowText(str);
m_uPortNum=(UINT)atoi((LPCTSTR)str);
return m_uPortNum;
}
CString CServerDlg::GetFileNameFromCtrl()
{
CEdit *pEdit=(CEdit *)GetDlgItem(IDC_FILENAME);
pEdit->GetWindowText(m_strFileName);
return m_strFileName;
}
void CServerDlg::SetSysText(CString &str)
{
CEdit *pEdit=(CEdit *)GetDlgItem(IDC_SYSSTATUS);
pEdit->SetWindowText(str);
}
void CServerDlg::OnAccept()
{
// TODO: Add your control notification handler code here
CString sysstr;
m_pSocket=new CSocket;
GetPortNumFromCtrl();
m_pSocket->Create(m_uPortNum);
//first listen a connection request on an specified port
sysstr.Format("Listening...");
SetSysText(sysstr);
if (!m_pSocket->Listen())
{
sysstr.Format("Listening failed");
SetSysText(sysstr);
MessageBox("can NOT listen a request");
m_pSocket->Close();
delete m_pSocket;
m_pSocket=NULL;
return ;
}
//then, accept the request
sysstr.Format("Accepting...");
SetSysText(sysstr);
CSocket ConnectedSocket;
if (!m_pSocket->Accept(ConnectedSocket))
{
sysstr.Format("Accepting Failed");
SetSysText(sysstr);
MessageBox("can NOT accepting");
m_pSocket->Close();
delete m_pSocket;
m_pSocket=NULL;
return;
}
CSocketFile SockFile(&ConnectedSocket);
CArchive arIn(&SockFile,CArchive::load); //load表示deserialize,即非连续
//Creates a CArchive object that points to SockFile
//begin reciving
sysstr.Format("Reciving...");
SetSysText(sysstr);
CProgressCtrl *pCtrl=(CProgressCtrl *)GetDlgItem(IDC_PROGRESS);
pCtrl->SetPos(0);
//first,recieve the length of file from the client
DWORD FileLength;
arIn>>FileLength;
//then,recieve the name of file from the client
CString FileName;
arIn>>FileName;
SaveFileAs(FileName);
//at last,recieve the file itself
// first,create the file
CFile *fp=new CFile;
if (!fp->Open(m_strFileName,CFile::modeCreate|CFile::modeWrite))
{
sysstr.Format("Reciving Failed");
SetSysText(sysstr);
MessageBox("Can NOT create the file");
delete fp;
arIn.Close();
SockFile.Close();
m_pSocket->Close();
delete m_pSocket;
m_pSocket=NULL;
return;
}
char buf[2048];
UINT Result;
m_dwRecievedFileLength=0;
do{
Result=arIn.Read(buf,2048);
fp->Write(buf,Result);
m_dwRecievedFileLength+=Result;
pCtrl->SetPos(m_dwRecievedFileLength*100./FileLength);//????
}
while (m_dwRecievedFileLength<FileLength);
//reception accomplished
sysstr.Format("Reception accomplished");
SetSysText(sysstr);
MessageBox("Reception accomplished");
arIn.Close();
SockFile.Close();
fp->Close();
delete fp;
m_pSocket->Close();
delete m_pSocket;
m_pSocket=NULL;
}
void CServerDlg::SaveFileAs(CString &FileName)
{
CFileDialog *pDlg=new CFileDialog(FALSE,NULL,FileName);
if (pDlg->DoModal()==IDOK)
{
m_strFileName=pDlg->GetPathName();
CEdit *pEdit=(CEdit*)GetDlgItem(IDC_FILENAME);
pEdit->SetWindowText(m_strFileName);
}
else
{
m_strFileName=FileName;
CEdit *pEdit=(CEdit*)GetDlgItem(IDC_FILENAME);
pEdit->SetWindowText(m_strFileName);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -