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

📄 httpserverdlg.cpp

📁 Visual_C++.NET精彩案例237.rar
💻 CPP
字号:
// HttpServerDlg.cpp :实现文件
//

#include "stdafx.h"
#include "HttpServer.h"
#include "HttpServerDlg.h"

#include "Serverthread.h"
#include "BlockSock.h"

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

// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
extern CMyBlockSocket g_sListen;

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// 对话框数据
	//{{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 支持
	//}}AFX_VIRTUAL

// 实现
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()


// CHttpServerDlg 对话框

CHttpServerDlg::CHttpServerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CHttpServerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CHttpServerDlg)
		// 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 CHttpServerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHttpServerDlg)
	DDX_Control(pDX, IDC_STATIC_PATH, m_StaPath);
	DDX_Control(pDX, IDC_STATIC_STATUS, m_StaStatus);
	DDX_Control(pDX, IDC_BTN_STOP, m_BtnStop);
	DDX_Control(pDX, IDOK, m_BtnStart);
	DDX_Control(pDX, IDC_LIST_INFO, m_ListInfo);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CHttpServerDlg, CDialog)
	//{{AFX_MSG_MAP(CHttpServerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_STOP, OnBtnStop)
	ON_BN_CLICKED(IDOK, OnStartServer)
	ON_BN_CLICKED(IDC_BTN_SETPATH, OnBtnSetpath)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


// CHttpServerDlg 消息处理程序

BOOL CHttpServerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 将\“关于...\”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	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);
		}
	}

	// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	
	// TODO: 在此添加额外的初始化代码
	m_BtnStart.EnableWindow(TRUE);
	m_BtnStop.EnableWindow(FALSE);
	m_StaStatus.SetWindowText("服务停止");
	m_StaPath.SetWindowText("服务器路径:"+g_strDirect);
	
	return TRUE;   // 除非设置了控件的焦点,否则返回 TRUE
}

void CHttpServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CHttpServerDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// 使图标在工作矩形中居中
		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;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}


//当用户拖动最小化窗口时系统调用此函数取得光标显示。
HCURSOR CHttpServerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CHttpServerDlg::OnBtnStop() 
{
	try 
	{
		if(g_bListening) 
		{
			g_sListen.Close();
			Sleep(300);
		}
	}
	catch(CMyBlockSocketException* e) {
		LogBlockingSocketException(&m_ListInfo, "主程序:", e);
		e->Delete();
	}

	g_bListening=FALSE;
	g_nConnection=0;
	m_StaStatus.SetWindowText("服务停止");
	m_ListInfo.AddString("HTTP服务器关闭!");
	m_BtnStart.EnableWindow(TRUE);
	m_BtnStop.EnableWindow(FALSE);
}

void CHttpServerDlg::OnStartServer() 
{
	try 
	{
		CSocketAddress saServer;
		if(g_strIPServer.IsEmpty()) 
		{
			saServer = CSocketAddress(INADDR_ANY, (USHORT) g_nPortServer);
		}
		else 
		{
			saServer = CSocketAddress(g_strIPServer, (USHORT) g_nPortServer);
		}
		g_sListen.Create();
		//绑定地址
		g_sListen.Bind(saServer);
		//开始侦听
		g_sListen.Listen();
		g_bListening = TRUE;
		g_nConnection = 0;
		//创建服务器工作线程
		AfxBeginThread(ServerThreadProc, &m_ListInfo, THREAD_PRIORITY_NORMAL);
		m_ListInfo.AddString("HTTP服务器开始服务啦!");
		m_StaStatus.SetWindowText("侦听连接");
	}
	catch(CMyBlockSocketException* e) 
	{
		g_sListen.Cleanup();
		LogBlockingSocketException(&m_ListInfo, "主程序:", e);
		e->Delete();
	}
	m_BtnStart.EnableWindow(FALSE);
	m_BtnStop.EnableWindow(TRUE);
}

void CHttpServerDlg::OnBtnSetpath() 
{
	CFileDialog dlg(TRUE,"htm","Default.htm");

	if (dlg.DoModal()==IDOK)
	{
		g_strDirect=dlg.GetPathName();
		g_strDirect.Replace(dlg.GetFileName(),"");
		m_StaPath.SetWindowText("服务器路径:"+g_strDirect);
	}
}

⌨️ 快捷键说明

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