按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
CString tempStr; tempList; sName;
tempStr = 〃〃;
tempList = m_ChattersList;
do
{
//利用“:”得到名字并查找(客户端发送的消息前面都有用户名加“:”)
sName = tempList。Left(tempList。Find(〃:〃; 0));
tempList = tempList。Mid(tempList。Find(〃:〃; 0) + 1);
//找到则返回真
if(sName == sNickName)
return TRUE;
}while(tempList。Find(〃:〃 ; 0) != …1);
//未被使用则返回假
return FALSE;
}
为函数 AssembleMsg()编写如下代码:
CMsg* CChatServerDoc::AssembleMsg(CClientSocket* pSocket ; int nCode)
{
static CMsg msg;
msg。Init();
for (POSITION pos1 = m_msgList。FindIndex(pSocket…》m_nMsgCount); pos1 != NULL;)
{
//信息内容
·305 ·
…………………………………………………………Page 317……………………………………………………………
Visual C++ 6。0 程序设计从入门到精通
msg。m_strText = m_msgList。GetNext(pos1);
//信息类型
msg。code = nCode;
}
pSocket…》m_nMsgCount = m_msgList。GetCount();
return &msg;
}
为函数 SendForNewer()编写如下代码:
void CChatServerDoc::SendForNewer(CMsg* pMsg)
{
for(POSITION pos = m_connectionList。GetHeadPosition(); pos != NULL;)
{
//对每个客户端都发送信息
CClientSocket* pSocket = (CClientSocket*)m_connectionList。GetNext(pos);
if (pSocket != NULL)
SendMsg(pSocket; pMsg);
}
}
为函数 SendToAllClients()编写如下代码:
void CChatServerDoc::SendToAllClients(int nCode)
{
for(POSITION pos = m_connectionList。GetHeadPosition(); pos != NULL;)
{
//得到每个客户端
CClientSocket* pSocket = (CClientSocket*)m_connectionList。GetNext(pos);
//封装信息
CMsg* pMsg = AssembleMsg(pSocket; nCode);
//发送信息
if (pMsg != NULL)
SendMsg(pSocket; pMsg);
}
}
为函数 UpdateMessageView()编写如下代码:
void CChatServerDoc::UpdateMessageView(LPCTSTR lpszMessage)
{
for(POSITION pos=GetFirstViewPosition();pos!=NULL;)
{
CView* pView = GetNextView(pos);
CChatView* pChatView = DYNAMIC_DOWNCAST(CChatView; pView);
if (pChatView != NULL)
·306 ·
…………………………………………………………Page 318……………………………………………………………
第 11 章 网络编程
pChatView…》ShowMessage(lpszMessage);
}
}
为函数 UpdateChattersListView()编写如下代码:
void CChatServerDoc::UpdateChattersListView(CString sName ; CClientSocket* pSocket)
{
CString sIPAddress;
UINT iPort;
//得到 IP 地址和端口号
pSocket…》GetPeerName(sIPAddress ; iPort);
for(POSITION pos=GetFirstViewPosition();pos!=NULL;)
{
CView* pView = GetNextView(pos);
CChattersListView* pChattersListView = DYNAMIC_DOWNCAST(CChattersListView; pView);
//更新用户列表
if (pChattersListView != NULL)
pChattersListView…》AddChatter(sName ; sIPAddress ; iPort);
}
}
为函数 CloseSocket()添加如下代码:
void CChatServerDoc::CloseSocket(CClientSocket* pSocket)
{
//关闭连接
pSocket…》Close();
POSITION pos;temp;
for(pos = m_connectionList。GetHeadPosition(); pos != NULL;)
{
temp = pos;
CClientSocket* pSock = (CClientSocket*)m_connectionList。GetNext(pos);
//将此 socket 从连接链表中删除
if (pSock == pSocket)
{
m_connectionList。RemoveAt(temp);
break;
}
}
delete pSocket;
//如果没有连接则清空连接链表
if(m_connectionList。GetCount() == 0)
m_msgList。RemoveAll();
·307 ·
…………………………………………………………Page 319……………………………………………………………
Visual C++ 6。0 程序设计从入门到精通
}
为函数 DeleteChatter()编写如下代码:
void CChatServerDoc::DeleteChatter(CMsg* pMsg)
{
//得到要删除的用户名
CString sNickName = pMsg…》m_strText。Left(pMsg…》m_strText。Find(〃:〃; 0));
POSITION pos;
for(pos=GetFirstViewPosition();pos!=NULL;)
{
CView* pView = GetNextView(pos);
CChattersListView* pChattersListView = DYNAMIC_DOWNCAST(CChattersListView; pView);
//在用户列表视图中将其删除
if (pChattersListView != NULL)
pChattersListView…》DeleteChatter(sNickName);
}
CString tempStr; sName; tempList;
tempStr = 〃〃;
tempList = m_ChattersList;
//得到除去此用户名之外的所有用户名
do
{
sName = tempList。Left(tempList。Find(〃:〃; 0));
tempList = tempList。Mid(tempList。Find(〃:〃; 0) + 1);
if(sName != sNickName)
tempStr += sName + 〃:〃;
}while(tempList。Find(〃:〃 ; 0) != …1);
m_ChattersList = tempStr;
//加入到信息链表和信息内容中
//接下来要对各客户端发送这些用户名,以使各客户端更新用户列表
pMsg…》m_strText = m_ChattersList;
m_msgList。AddTail(m_ChattersList);
}
为 DeleteContents() 函数编写如下代码:
void CChatServerDoc::DeleteContents()
{
if(m_pSocket == (CListeningSocket*)NULL)
return;
//删除监听 socket
delete m_pSocket;
m_pSocket = NULL;
·308 ·
…………………………………………………………Page 320……………………………………………………………
第 11 章 网络编程
CString temp;
temp。Format(〃服务器已关闭〃);
m_msgList。AddTail(temp);
//对当前连接的每个客户端发送信息后关闭与该客户端的连接
while(!m_connectionList。IsEmpty())
{
CClientSocket* pSocket = (CClientSocket*)m_connectionList。RemoveHead();
CMsg* pMsg = AssembleMsg(pSocket ; NORMAL_MESSAGE);
pMsg…》m_bClose = TRUE;
SendMsg(pSocket; pMsg);
pSocket…》ShutDown(2);
delete pSocket;
}
//清空所