📄 clientdlg.cpp
字号:
// ClientDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Client.h"
#include "ClientDlg.h"
#include "ClientLogin.h"
#include "ChatDlg.h"
#include "ChatSocket.h"
#include "ListenSocket.h"
#include "PeerSocket.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CClientDlg dialog
CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CClientDlg)
// 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_pImageList = NULL;
m_pArchiveIn = NULL;
m_pArchiveOut = NULL;
m_pFile = NULL;
m_pSocket = NULL;
memset(&m_hostAddr, 0, sizeof(SOCKADDR_IN));
}
CClientDlg::~CClientDlg()
{
if (NULL != m_pImageList)
{
delete m_pImageList;
m_pImageList = NULL;
}
if (NULL != m_pArchiveIn)
{
delete m_pArchiveIn;
m_pArchiveIn = NULL;
}
if (NULL != m_pArchiveOut)
{
delete m_pArchiveOut;
m_pArchiveOut = NULL;
}
if (NULL != m_pFile)
{
delete m_pFile;
m_pFile = NULL;
}
if (m_pSocket != NULL)
{
BYTE Buffer[50];
m_pSocket->ShutDown();
while(m_pSocket->Receive(Buffer,50) > 0);//????
delete m_pSocket;
m_pSocket = NULL;
}
}
void CClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CClientDlg)
DDX_Control(pDX, IDC_CLIENT_LIST, m_ctlUserList);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
//{{AFX_MSG_MAP(CClientDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_DESTROY()
ON_NOTIFY(NM_DBLCLK, IDC_CLIENT_LIST, OnDblclkClientList)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClientDlg message handlers
BOOL CClientDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
if (!Init())
EndDialog(IDCANCEL);
return FALSE;
//#define WM_USER 0x0400
// CUserInfo userInfo;
// userInfo.m_time = CTime::GetCurrentTime();
// userInfo.m_strName = "sunhm";
// CString str("我吃饭去了。");
// ShowOfflineMsg(userInfo, str);
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CClientDlg::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 CClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/*
* 初始化
*/
BOOL CClientDlg::Init(void)
{
InitListCtrlSetting(); //初始化列表视图控件
BegingListen(); //开始监听
if(!ConnectToServer()) //连接服务器
return FALSE;
return TRUE;
}
/*
* 连接服务器
*/
BOOL CClientDlg::ConnectToServer(void)
{
//创建CChatSocket对象
m_pSocket = new CChatSocket(this);
if (!m_pSocket->Create())//创建套接字,绑定,注册网络事件
{
delete m_pSocket;
m_pSocket = NULL;
AfxMessageBox(_T("创建套接字失败!"));
return FALSE;
}
m_pFile = new CSocketFile(m_pSocket); //创建CSocketFile对象
m_pArchiveIn = new CArchive(m_pFile,CArchive::load); //创建读入文档对象
CClientLoginDlg loginDlg(this);//登录对话框
if (IDOK == loginDlg.DoModal())
{
//服务器端口
SHORT shServPort = (SHORT)atoi((LPCTSTR)loginDlg.m_strServerPort);
m_servAddr.sin_family = AF_INET; //地址家族
m_servAddr.sin_addr.S_un.S_addr = htonl((u_long)loginDlg.m_dwIP); //地址
m_servAddr.sin_port = htons(shServPort); //端口
//连接服务器
while (!m_pSocket->Connect((SOCKADDR*)&m_servAddr, sizeof(m_servAddr)))
{
if (AfxMessageBox(_T("连接服务器失败,是否再次尝试连接。"),MB_YESNO) == IDNO)
{
return FALSE;
}
}
//获取本机套接字地址
SOCKADDR_IN hostAddr;
int nSockaddLen = sizeof(SOCKADDR_IN);
m_pSocket->GetSockName((SOCKADDR*)&hostAddr, &nSockaddLen);
m_hostAddr.sin_addr.S_un.S_addr = hostAddr.sin_addr.S_un.S_addr;//主机地址
CChatPacket packet; //数据包
packet.m_type = CChatPacket::USERLIST; //类型
packet.m_UserInfo.m_strName = loginDlg.m_strName; //名称
packet.m_UserInfo.m_strPassword = loginDlg.m_strPassword; //密码
packet.m_UserInfo.m_eStatus = CUserInfo::LOGIN; //用户状态
packet.m_UserInfo.m_lIP = hostAddr.sin_addr.S_un.S_addr; //IP
packet.m_UserInfo.m_nPort = m_hostAddr.sin_port; //端口
packet.m_UserInfo.m_time = CTime::GetCurrentTime(); //登录时间
m_UserInfo = packet.m_UserInfo; //保存用户信息
//请求用户列表
SendPacket(packet);
return TRUE;
}else
{
return FALSE;
}
}
/*
* 等待接收消息
*/
void CClientDlg::ProcessPendingRead(void)
{
do
{
ReadPacket();
if (m_pSocket == NULL)
return;
}
while(!m_pArchiveIn->IsBufferEmpty());//将缓冲区数据全部读入
}
/*
* 接收服务器数据
*/
void CClientDlg::ReadPacket(void)
{
CObList obList;//临时链表
CChatPacket packet;//数据包
packet.m_pUserList = &obList;
TRY
{
packet.Serialize(*m_pArchiveIn); //接收数据
if (packet.m_type == CChatPacket::MESSAGE) //离线消息
{
ShowOfflineMsg(&packet);
}else if (packet.m_type == CChatPacket::USERLIST) //用户列表
{
CreateUserList(&obList); //创建用户链表
UpdateClientListCtl(); //更新用户界面
m_UserInfo.m_eStatus = CUserInfo::ONLINE; //修改用户状态
}else if (packet.m_type == CChatPacket::SERVERMSG) //登录失败消息
{
MessageBox(packet.m_strMsg, "Server Messages", MB_OK);
EndDialog(IDCANCEL); //退出
}
}
CATCH (CFileException, e)
{
MessageBox(_T("读入chatter链表错误"));
}
END_CATCH
}
/*
* 更新用户列表
*/
void CClientDlg::UpdateClientListCtl(void)
{
//删除原来用户链表
m_ctlUserList.DeleteAllItems();
POSITION pos;
int nIndex = 0;
for(pos = m_UserList.GetHeadPosition(); pos != NULL;)//遍历整个链表
{
//获取每个用户信息
CUserInfo userInfo;
userInfo =*(CUserInfo*)m_UserList.GetNext(pos);
LVITEM lvItem;
lvItem.mask = LVIF_TEXT |LVIF_IMAGE |LVIF_PARAM;
lvItem.iItem = nIndex;
lvItem.iSubItem = 0;
lvItem.pszText = userInfo.m_strName.GetBuffer(10);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -