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

📄 serverdlg.cpp

📁 类似于QQ的聊天工具,分为客户端和服务器端,有共享空间,能发布公告,可传输文件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ServerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Server.h"
#include "ServerDlg.h"
#include "CallBoard.h"
#include "FileManage.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)
{
	m_CallBoardHwnd  = NULL;
	m_FileManageHwnd = NULL;
	m_bConnentDB     = false;
	m_nNewCallCount  = 0;
	//{{AFX_DATA_INIT(CServerDlg)
	m_strPort = _T("");
	//}}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)
	DDX_Control(pDX, IDC_LST_SHOW, m_lstShow);
	DDX_Text(pDX, IDC_EDIT_PORT, m_strPort);
	DDV_MaxChars(pDX, m_strPort, 5);
	//}}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_BTN_STARTSERVER, OnBtnStartserver)
	ON_BN_CLICKED(IDC_BTN_CLOSESERVER, OnBtnCloseserver)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BTN_CLOSEFILESERVER, OnBtnClosefileserver)
	ON_BN_CLICKED(IDC_BTN_CONNECTDB, OnBtnConnectdb)
	ON_BN_CLICKED(IDC_BTN_UNCONNECTDB, OnBtnUnconnectdb)
	ON_BN_CLICKED(IDC_BTN_CALLBOARD, OnBtnCallboard)
	ON_BN_CLICKED(IDC_BTN_SHAREFILEMNG, OnBtnSharefilemng)
	ON_BN_CLICKED(IDC_BTN_STARTFILESERVER, OnBtnStartfileserver)
	ON_BN_CLICKED(IDC_BTN_SETFILEDIR, OnBtnSetfiledir)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_NETEVENT, OnNetEvent)
	ON_MESSAGE(WM_FILEEVENT, OnFileEvent)
	ON_MESSAGE(WM_DEALSYSMSG, OnDealSysMsg)
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
	SetTimer(TIMERID_TIME, 200, NULL);

	/////////////////////////////////////////////////////////////////////////
	//初始化列表控件
	DWORD dwStyle = LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES; 
	m_lstShow.SetExtendedStyle(dwStyle);
	m_lstShow.SetBkColor(RGB(223, 223, 223));
	m_lstShow.SetTextBkColor(RGB(223, 223, 223));
	m_lstShow.SetTextColor(RGB(255, 15, 14));

	m_lstShow.InsertColumn(0, "SOCK", LVCFMT_LEFT, 40, 0);
	m_lstShow.InsertColumn(1, "用户名", LVCFMT_LEFT, 70, 1);
	m_lstShow.InsertColumn(2, "用户ID", LVCFMT_LEFT, 70, 2);
	m_lstShow.InsertColumn(3, "文件PORT", LVCFMT_LEFT, 60, 3);
	m_lstShow.InsertColumn(4, "IP 地 址", LVCFMT_LEFT, 105, 4);
	m_lstShow.InsertColumn(5, "备    注", LVCFMT_LEFT, 75, 4);	
	m_lstShow.InsertColumn(6, "时    间", LVCFMT_LEFT, 127, 5);

	(GetDlgItem(IDC_EDIT_PORT))->SetFocus();
	(GetDlgItem(IDC_BTN_UNCONNECTDB))->EnableWindow(false);
	(GetDlgItem(IDC_BTN_CLOSESERVER))->EnableWindow(false);
	(GetDlgItem(IDC_BTN_CLOSEFILESERVER))->EnableWindow(false);
	return FALSE;  // 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;
}

void CServerDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	//CDialog::OnOK();
}

void CServerDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}

void CServerDlg::OnBtnStartserver() 
{
	// TODO: Add your control notification handler code here
	
	CString strPort;
	GetDlgItem(IDC_EDIT_PORT)->GetWindowText(strPort);

	//设置监听端口获取焦点
	if(strPort.IsEmpty())
	{
		(GetDlgItem(IDC_EDIT_PORT))->SetFocus();
		AfxMessageBox("请设置服务器监听端口!");
		return;
	}

	//启动服务器
	if(!m_SvrSock.CreatServer(m_hWnd, atoi(strPort)))
	{
		return;
	}

	//注册网络事件
	int ret = WSAAsyncSelect(m_SvrSock.GetServerSocket(), m_hWnd, 
                              WM_NETEVENT, FD_ACCEPT | FD_CLOSE | FD_READ); 
	if(SOCKET_ERROR == ret)
	{
		AfxMessageBox("网络事件注册失败!");
		return;
	}

	/*m_strSysMsg.Format("服务器成功启动  %s", m_strTime);
	int Index = m_lstShow.GetItemCount();
	m_lstShow.InsertItem(Index, m_strSysMsg);*/

	(GetDlgItem(IDC_BTN_CLOSESERVER))->EnableWindow(true);
	(GetDlgItem(IDC_BTN_STARTSERVER))->EnableWindow(false);
	(GetDlgItem(IDC_EDIT_PORT))->EnableWindow(false);
	(GetDlgItem(IDC_STC_PORT))->EnableWindow(false);
}

void CServerDlg::OnNetEvent(WPARAM wParam, LPARAM lParam)
{
	//调用API函数,得到网络事件类型
    int iEvent = WSAGETSELECTEVENT(lParam); 
          
    //得到发出此事件的客户端套接字
    SOCKET sClientSock = (SOCKET)wParam;
	//tagClientSockeInfo csi;

	switch(iEvent)
	{
		case FD_ACCEPT: //客户端连接请求
			{
				//AfxMessageBox("OnAccept!");

				if(m_SvrSock.OnAccept())
				{
					/*//tagClientSockeInfo* csi = m_SvrSock.GetClientInfo();
					m_strSysMsg.Format("连接 %d %s %s %s",csi.m_sClientSocket,
						               csi.m_strUsrId, csi.m_strIp, csi.m_strTime);
					int Index = m_lstShow.GetItemCount();
					m_lstShow.InsertItem(Index, m_strSysMsg);*/
				}
				break;
			}
		case FD_CLOSE:  //客户端断开事件:
			{
				//AfxMessageBox("OnClose");
				if(m_SvrSock.OnClose(sClientSock))
				{
					/*m_strSysMsg.Format("断开 %d %s %s %s",csi.m_sClientSocket,
						               csi.m_strUsrId, csi.m_strIp, csi.m_strTime);
					int Index = m_lstShow.GetItemCount();
					m_lstShow.InsertItem(Index, m_strSysMsg);*/
				}
				break;
			}
		case FD_READ:   //网络数据包到达事件
			{
				//AfxMessageBox("OnReceive");
				m_SvrSock.OnRecieve(sClientSock);
				break;
			}
		default: break;

⌨️ 快捷键说明

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