?? serverdlg.cpp
字號:
// ServerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ChatServer.h"
#include "ServerDlg.h"
#include "PortDlg.h"
#include "msg.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);
m_pSocket=NULL;
}
void CServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CServerDlg)
DDX_Control(pDX, IDC_STATIC_MESSAGES, m_StaMessage);
DDX_Control(pDX, IDC_STATIC_PORT, m_StaPort);
DDX_Control(pDX, IDC_STATIC_NUMBER, m_StaNumber);
DDX_Control(pDX, IDC_LIST_MSG, m_ListMsg);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
//{{AFX_MSG_MAP(CServerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_DESTROY()
ON_BN_CLICKED(IDOK, OnStopServer)
//}}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
CPortDlg dlg;
//彈出通信端口對話框
//并獲取通信端口號
if (dlg.DoModal()==IDOK)
{
//創(chuàng)建偵聽套接字對象
m_pSocket=new CListeningSocket(this);
//用獲得的通信端口偵聽
if (m_pSocket->Create(dlg.m_nPort))
{
CString strTemp;
//在窗體上顯示通信端口號
strTemp.Format("端口號:%4d",dlg.m_nPort);
m_StaPort.SetWindowText(strTemp);
//錯誤判斷
if (!m_pSocket->Listen())
{
AfxMessageBox("偵聽失敗!");
CDialog::OnOK();
return TRUE;
}
}
}
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;
}
//在CServerDlg類終止運行時進行的后續(xù)處理
void CServerDlg::OnDestroy()
{
CDialog::OnDestroy();
//刪除偵聽套接字
delete m_pSocket;
m_pSocket=NULL;
//向消息列表增加信息
CString strTemp;
strTemp="服務器終止服務!";
m_msgList.AddTail(strTemp);
//對連接列表進行處理
while (!m_connectionList.IsEmpty())
{
//向每一個連接的客戶機發(fā)送"服務器終止服務!"的消息
//并逐個刪除已建立的連接
CClientSocket* pSocket
=(CClientSocket*)m_connectionList.RemoveHead();
CMsg* pMsg=AssembleMsg(pSocket);
pMsg->m_bClose=TRUE;
SendMsg(pSocket,pMsg);
if (!pSocket->IsAborted())
{
pSocket->ShutDown();
BYTE Buffer[50];
while (pSocket->Receive(Buffer,50)>0);
delete pSocket;
}
}
//刪除消息列表
m_msgList.RemoveAll();
}
//對所有的已連接的客戶機的消息進行更新
void CServerDlg::UpdateClients()
{
for (POSITION pos=m_connectionList.GetHeadPosition();pos!=NULL;)
{
//獲得連接列表的成員
CClientSocket* pSocket
=(CClientSocket*)m_connectionList.GetNext(pos);
//對客戶機成員的消息列表進行分析
CMsg* pMsg=AssembleMsg(pSocket);
//客戶機的消息列表需要進行更新
if (pMsg!=NULL)
//發(fā)送新的消息列表
//更新客戶機消息列表的內容
SendMsg(pSocket,pMsg);
}
}
//接受連接請求
void CServerDlg::OnAccept()
{
CClientSocket* pSocket=new CClientSocket(this);
//接受連接請求
if (m_pSocket->Accept(*pSocket))
{
//對連接套接字初始化
pSocket->Initialize();
//假如連接列表
m_connectionList.AddTail(pSocket);
}
else
delete pSocket;
}
//獲取客戶機的發(fā)送消息
void CServerDlg::OnReceive(CClientSocket* pSocket)
{
do
{
//調用ReadMsg()讀取消息
CMsg* pMsg=ReadMsg(pSocket);
if (pMsg->m_bClose)
{
//如果客戶機關閉
//調用函數(shù)CloseSocket()將該與該客戶機的連接從連接列表中刪除
DeleteSocket(pSocket);
break;
}
}
while (!pSocket->m_pArchiveIn->IsBufferEmpty());
UpdateClients();
}
//分析消息列表
CMsg* CServerDlg::AssembleMsg(CClientSocket* pSocket)
{
static CMsg msg;
msg.Init();
//客戶機的消息列表無需更新
if (pSocket->m_nMsgCount>=m_msgList.GetCount())
return NULL;
//客戶機的消息列表需要更新
for (POSITION pos=m_msgList.FindIndex(pSocket->m_nMsgCount);pos!=NULL;)
{
//將缺少的消息增加到消息列表最后
CString strTemp=m_msgList.GetNext(pos);
msg.m_msgList.AddTail(strTemp);
}
pSocket->m_nMsgCount=m_msgList.GetCount();
UpdateInfo();
return& msg;
}
//讀取消息
CMsg* CServerDlg::ReadMsg(CClientSocket* pSocket)
{
static CMsg msg;
TRY
{
//讀取消息
pSocket->ReceiveMessage(&msg);
//顯示接收的消息
DisplayMsg(msg.m_strText);
//更新消息列表
m_msgList.AddTail(msg.m_strText);
}
CATCH(CFileException,e)
{
//錯誤處理
CString strTemp;
strTemp="讀取信息錯誤!";
msg.m_bClose=TRUE;
pSocket->Abort();
}
END_CATCH
return &msg;
}
//發(fā)送消息
void CServerDlg::SendMsg(CClientSocket* pSocket,CMsg* pMsg)
{
TRY
{
//發(fā)送消息
pSocket->SendMessage(pMsg);
}
CATCH(CFileException,e)
{
//錯誤處理
pSocket->Abort();
CString strTemp;
strTemp="發(fā)送信息錯誤!";
DisplayMsg(strTemp);
}
END_CATCH
}
//關閉套接字
void CServerDlg::DeleteSocket(CClientSocket* pSocket)
{
pSocket->Close();
POSITION pos,temp;
for (pos=m_connectionList.GetHeadPosition();pos!=NULL;)
{
//對于已經(jīng)關閉的客戶機
//在消息列表中將已經(jīng)建立的連接刪除
temp=pos;
CClientSocket* pSock=(CClientSocket*)m_connectionList.GetNext(pos);
//匹配成功
if (pSock==pSocket)
{
m_connectionList.RemoveAt(temp);
UpdateInfo();
break;
}
}
delete pSocket;
}
//在列表框顯示消息
void CServerDlg::DisplayMsg(LPCTSTR lpszMessage)
{
//在列表框中顯示消息
m_ListMsg.AddString(lpszMessage);
//更新系統(tǒng)信息
UpdateInfo();
}
//更新連接信息
void CServerDlg::UpdateInfo()
{
CString strTemp;
//更新在線人數(shù)
strTemp.Format("在線人數(shù):%d",m_connectionList.GetCount());
m_StaNumber.SetWindowText(strTemp);
//更新信息數(shù)目
strTemp.Format("信息數(shù):%d",m_msgList.GetCount());
m_StaMessage.SetWindowText(strTemp);
}
//按鈕的事件處理函數(shù)
void CServerDlg::OnStopServer()
{
// TODO: Add your control notification handler code here
//退出系統(tǒng)
CDialog::OnOK();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -