📄 fileserverdlg.cpp
字号:
f// FileServerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "FileServer.h"
#include "FileServerDlg.h"
#include "DlgProxy.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern DWORD WINAPI ConnectionServer(void *);
CFileServerDlg *pDlg;
void ReportError(int id, bool fatal, CString &msg);
extern int ServerSocket;
int Killing=0;
/////////////////////////////////////////////////////////////////////////////
// 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()
/******************************************************************************/
/* GetHostNameFromRegistry */
/******************************************************************************/
/* Get the TCP/IP host name from the Registry */
static BOOL GetHostNameFromRegistry(LPSTR HostName,DWORD dwSizeHostName)
{
HKEY hkTcpip;
DWORD dwError;
DWORD dwSize;
char *p;
dwError = RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters",
0,
KEY_READ,
&hkTcpip);
if (dwError) {
SetLastError(dwError);
return FALSE;
}
/* Get the host name */
dwSize = dwSizeHostName;
dwError = RegQueryValueEx(
hkTcpip,
"Hostname",
NULL,
NULL,
(unsigned char *)HostName,
&dwSize);
if (dwError) {
RegCloseKey(hkTcpip);
SetLastError(dwError);
return FALSE;
}
/* Append a dot */
strncat(HostName,".",dwSizeHostName);
/* Append the domain name */
dwSize = dwSizeHostName - strlen(HostName);
p = HostName + strlen(HostName);
dwError = RegQueryValueEx(
hkTcpip,
"Domain",
NULL,
NULL,
(unsigned char *)p,
&dwSize);
if (dwError || strlen(p)==0) {
//RegCloseKey(hkTcpip);
//SetLastError(dwError);
//return FALSE;
// remove trailing .
HostName[strlen(HostName)-1] = 0;
}
/* Terminate successfully */
RegCloseKey(hkTcpip);
return TRUE;
}
/******************************************************************************/
/* GetLocalHostIPInfo */
/******************************************************************************/
/* Get the local host IP addresses and names into the LocalHostIPInfo array.
Return the number of addresses in the nAddresses variable. */
#define MAX_INET_ADDRESSES 5
typedef struct _IPInfo {
IN_ADDR IPAddr;
char Name[256];
char Address[64];
} tIPInfo;
static tIPInfo LocalHostIPInfo[MAX_INET_ADDRESSES];
static void GetLocalHostIPInfo(char *localip)
{
char szBuff[256];
DWORD dwSize;
PHOSTENT phe;
int i;
int nAddresses;
/* Error condition is indicated if this is 0 on exit */
nAddresses = 0;
/* Get host name */
dwSize = sizeof(szBuff);
if (gethostname(szBuff,dwSize)) {
return;
}
/* Get host addresses */
phe = gethostbyname(szBuff);
if (phe == NULL) {
return;
}
if (strchr(szBuff,'.')==NULL) {
/* gethostname() did not return a FQDN. Go get the FQDN from the Registry */
if (!GetHostNameFromRegistry(szBuff,dwSize)) {
return;
}
}
/* Copy the IP addresses */
strncpy(LocalHostIPInfo[0].Name,"",sizeof(LocalHostIPInfo[i].Name));
i = 0;
while (i<MAX_INET_ADDRESSES) {
if (phe->h_addr_list[i]==0) break;
memcpy(&LocalHostIPInfo[i].IPAddr,phe->h_addr_list[i],sizeof(struct in_addr));
strncpy(LocalHostIPInfo[i].Address,
inet_ntoa(LocalHostIPInfo[i].IPAddr),
sizeof(LocalHostIPInfo[i].Address));
strncpy(LocalHostIPInfo[i].Name,szBuff,sizeof(LocalHostIPInfo[i].Name));
i++;
}
nAddresses = i;
if (nAddresses>0) {
strcpy(localip,LocalHostIPInfo[0].Address);
} else {
strcpy(localip,"?");
}
#if 0
/* Now look up the names corresponding to the addresses */
for (i=0;i<nAddresses;i++) {
time_t tStartedTime, tFinishedTime;
tStartedTime = time(&tStartedTime);
phe = gethostbyaddr((const char *)&LocalHostIPInfo[i].IPAddr,sizeof(IN_ADDR),PF_INET);
tFinishedTime = time(&tFinishedTime);
//Did it take more than 20 seconds? If so, DNS does not active.
if (tFinishedTime - tStartedTime > 20) {
MessageBox(NULL,
"DNS(Domain Name Service) not operational.\nVerify DNS and your network.",
"TCPIPTrc",
MB_OK);
return;
}
if (phe!=NULL) {
/* We got something which claims to be a host name */
if (strchr(phe->h_name,'.')!=0) {
/* It has a dot, so it's probably a FQDN - replace the host name we got earlier. */
strncpy(LocalHostIPInfo[i].Name,phe->h_name,sizeof(LocalHostIPInfo[i].Name));
}
}
}
#endif
}
/////////////////////////////////////////////////////////////////////////////
// CFileServerDlg dialog
IMPLEMENT_DYNAMIC(CFileServerDlg, CDialog);
CFileServerDlg::CFileServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFileServerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFileServerDlg)
m_PortNumber = 0;
m_Path = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_pAutoProxy = NULL;
m_PortNumber =20000;
m_NumFiles = 0;
}
CFileServerDlg::~CFileServerDlg()
{
// If there is an automation proxy for this dialog, set
// its back pointer to this dialog to NULL, so it knows
// the dialog has been deleted.
if (m_pAutoProxy != NULL)
m_pAutoProxy->m_pDialog = NULL;
}
void CFileServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFileServerDlg)
DDX_Control(pDX, IDC_NUMFILES1, m_NumFilesCtrl);
DDX_Control(pDX, m_LocalIP, m_LocalIPCtrl);
DDX_Control(pDX, IDC_CONSOLE, m_ConsoleCtrl);
DDX_Control(pDX, IDC_CLEAR, m_ClearCtrl);
DDX_Control(pDX, IDC_PATH, m_PathCtrl);
DDX_Control(pDX, IDC_ACTION, m_ActionCtrl);
DDX_Control(pDX, IDC_PORTNUMBER, m_PortNumberCtrl);
DDX_Control(pDX, IDC_FILELIST, m_FileListCtrl);
DDX_Text(pDX, IDC_PORTNUMBER, m_PortNumber);
DDX_Text(pDX, IDC_PATH, m_Path);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFileServerDlg, CDialog)
//{{AFX_MSG_MAP(CFileServerDlg)
ON_WM_SYSCOMMAND()
ON_WM_DESTROY()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_ACTION, OnAction)
ON_EN_CHANGE(IDC_PORTNUMBER, OnChangePortnumber)
ON_BN_CLICKED(IDC_CLEAR, OnClear)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFileServerDlg message handlers
BOOL CFileServerDlg::OnInitDialog()
{
static LV_COLUMN cols[] = {
{LVCF_TEXT|LVCF_SUBITEM|LVCF_WIDTH,LVCFMT_LEFT,400,"Sts",64,0,0,0},
{LVCF_TEXT|LVCF_SUBITEM|LVCF_WIDTH,LVCFMT_LEFT,400,"Filename",64,1,0,0},
{LVCF_TEXT|LVCF_SUBITEM|LVCF_WIDTH,LVCFMT_LEFT,400,"Access",64,2,0,0},
{LVCF_TEXT|LVCF_SUBITEM|LVCF_WIDTH,LVCFMT_LEFT,400,"Kbytes",64,3,0,0}
};
RECT rect;
char filename[512];
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
GetLocalHostIPInfo(filename);
m_LocalIPCtrl.SetWindowText(filename);
m_FileListCtrl.GetClientRect(&rect);
cols[0].cx = (rect.right*01)/20;
cols[1].cx = (rect.right*12)/20;
cols[2].cx = (rect.right*03)/20;
cols[3].cx = (rect.right*04)/20;
m_FileListCtrl.InsertColumn(0,cols);
m_FileListCtrl.InsertColumn(1,cols+1);
m_FileListCtrl.InsertColumn(2,cols+2);
m_FileListCtrl.InsertColumn(3,cols+3);
m_FileListCtrl.SetExtendedStyle(LVS_EX_GRIDLINES);
m_ClearCtrl.EnableWindow(false);
m_LeftOver = "";
m_PortNumberCtrl.GetWindowText(m_CurPort);
OnChangePortnumber();
// Create a thread to handle incoming socket requests
pDlg = this;
m_Thread = CreateThread(NULL,4000,ConnectionServer,(void *)m_PortNumber,0,&m_threadid);
return TRUE; // return TRUE unless you set the focus to a control
}
void CFileServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
void CFileServerDlg::OnDestroy()
{
WinHelp(0L, HELP_QUIT);
CDialog::OnDestroy();
}
// 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 CFileServerDlg::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 CFileServerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
// Automation servers should not exit when a user closes the UI
// if a controller still holds on to one of its objects. These
// message handlers make sure that if the proxy is still in use,
// then the UI is hidden but the dialog remains around if it
// is dismissed.
void CFileServerDlg::OnClose()
{
if (CanExit())
CDialog::OnClose();
}
void CFileServerDlg::OnOK()
{
if (CanExit())
CDialog::OnOK();
}
void CFileServerDlg::OnCancel()
{
if (CanExit())
CDialog::OnCancel();
}
BOOL CFileServerDlg::CanExit()
{
// If the proxy object is still around, then the automation
// controller is still holding on to this application. Leave
// the dialog around, but hide its UI.
if (m_pAutoProxy != NULL)
{
ShowWindow(SW_HIDE);
return FALSE;
}
return TRUE;
}
void CFileServerDlg::OnAction()
{
char pn[256];
int l;
SOCKET ss;
m_PortNumberCtrl.GetWindowText(pn,sizeof(pn)-1);
l = atoi(pn);
if (l!=0) {
ss = ServerSocket;
ServerSocket = 0;
Killing = 1;
if (ss!=0) {
closesocket(ss);
Sleep(100);
itoa(l,pn,10);
m_CurPort = pn;
m_PortNumber = l;
m_Thread = CreateThread(NULL,4000,ConnectionServer,(void *)m_PortNumber,0,&m_threadid);
}
} else {
CString spn = pn;
ReportError(IDS_INVPORTNO,false,spn);
m_PortNumberCtrl.SetWindowText(m_CurPort);
}
m_PortNumberCtrl.SetWindowText(m_CurPort);
OnChangePortnumber();
}
void CFileServerDlg::OnChangePortnumber()
{
CString np;
int j,l,ok;
m_PortNumberCtrl.GetWindowText(np);
l = np.GetLength();
ok = (l>0);
for (j=0;j<l; j++) {
if ((np[j]<'0') || ('9'< np[j])) {
ok = 0;
break;
}
}
m_ActionCtrl.EnableWindow((np!=m_CurPort) && ok);
}
void CFileServerDlg::OnClear()
{
m_ConsoleCtrl.ResetContent();
m_ClearCtrl.EnableWindow(false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -