【VC开源代码栏目提醒】:网学会员,鉴于大家对VC开源代码十分关注,论文会员在此为大家搜集整理了“ChatServerDlg.cpp”一文,供大家参考学习!
// ChatServerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ChatServer.h"
#include "ChatServerDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CChatServerDlg dialog
CChatServerDlg::CChatServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CChatServerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CChatServerDlg)
m_uPort = 0;
m_ShowMsg = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CChatServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChatServerDlg)
DDX_Control(pDX, IDC_STATIC_NUM, m_staNum);
DDX_Control(pDX, IDC_IPADDRESS, m_IP);
DDX_Text(pDX, IDC_EDIT_PORT, m_uPort);
DDX_Text(pDX, IDC_SHOWMSG, m_ShowMsg);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CChatServerDlg, CDialog)
//{{AFX_MSG_MAP(CChatServerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)
ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChatServerDlg message handlers
BOOL CChatServerDlg::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
//取得服务器本地IP
char HostName[100];
gethostname(HostName, 100);
hostent *hn;
hn = gethostbyname(HostName);
LPSTR lpAddr=hn->h_addr_list[0];
struct in_addr inAddr;
memmove(&inAddr,lpAddr,4);
BYTE f0=inAddr.S_un.S_un_b.s_b1;
BYTE f1=inAddr.S_un.S_un_b.s_b2;
BYTE f2=inAddr.S_un.S_un_b.s_b3;
BYTE f3=inAddr.S_un.S_un_b.s_b4;
//将取得的IP显示在IP地址框
((CIPAddressCtrl *)(GetDlgItem(IDC_IPADDRESS)))->SetAddress(f0,f1,f2,f3);
m_uPort=8000;
UpdateData(FALSE);
GetDlgItem(IDC_BUTTON_STOP)->EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CChatServerDlg::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 CChatServerDlg::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 CChatServerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CChatServerDlg::OnButtonStart()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE); //获得用户输入
m_pListenSocket = new CListenSocket(this);
if (!m_pListenSocket->Create(m_uPort))
{
delete m_pListenSocket; //错误处理
m_pListenSocket = NULL;
AfxMessageBox(IDS_SOCKETCREATEERROR);
return;
}
if (!m_pListenSocket->Listen())
{
delete m_pListenSocket; //错误处理
m_pListenSocket = NULL;
AfxMessageBox(IDS_STARTLISTENERROR);
return;
}
//处理结束后改变对话框的控件状态
SetDlgItemText(IDC_SHOWMSG,"服务启动成功!");
GetDlgItem(IDC_EDIT_PORT)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_START)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_STOP)->EnableWindow(TRUE);
}
void CChatServerDlg::OnAccept()
{
CSessionSocket *pSocket = new CSessionSocket(this);
if (m_pListenSocket->Accept(*pSocket))
{
pSocket->Init();
m_connList.AddTail(pSocket);
//更新在线人数
CString strTemp;
strTemp.Format("在线人数:%d",m_connList.GetCount());
m_staNum.SetWindowText(strTemp);
} else delete pSocket;
}
void CChatServerDlg::OnReceive(CSessionSocket *pSocket)
{
static CMsg msg;
do {
//接收用户发送来的消息
pSocket->ReceiveMsg(&msg);
//将用户发送的的消息显示在服务器的聊天信息对话框中
m_sMsgList += msg.m_strBuf + "\r\n";
SetDlgItemText(IDC_SHOWMSG,m_sMsgList);
int m_iLineCurrentPos
=((CEdit *)(GetDlgItem(IDC_SHOWMSG)))->GetLineCount();
((CEdit *)(GetDlgItem(IDC_SHOWMSG)))->LineScroll(m_iLineCurrentPos);
//向所有客户机返回该用户发来的消息
ToClients(&msg);
//如果某一客户机关闭,在连接列表中将与该客户机的连接删除
if (msg.m_bClose)
{
pSocket->Close();
POSITION pos,temp;
//寻找并匹配已经关闭的连接
上一篇:
ChatServer.cpp
下一篇:
浮生若萍