}
接着为“设定端口”菜单项添加响应函数,代码如下:
void CChatServerDoc::OnSetport()
{
// TODO: Add your command handler code here
CPortDlg dlg;
if(dlg.DoModal() == IDOK)
{
//创建一个新的类进行jiān tīng
m_pSocket = new CListeningSocket(this);
if (m_pSocket->Create(dlg.m_Port))
{
if (m_pSocket->Listen())
return;
}
}
}
枫叶文学网www.fywxw.com
Visual C++ 6.0 程序设计从入门到精通
·302·
为函数ProcessAccept()添加如下代码:
void CChatServerDoc::ProcessAccept()
{
//创建一个新的Socket 来处理与客户端的数据jiāo互
CClientSocket* pSocket = new CClientSocket(this);
//如果接受客户端的连接请求
if (m_pSocket->Accept(*pSocket))
{
//初始化
pSocket->Init();
//将此连接socket 加入连接链表中
m_connectionList.AddTail(pSocket);
}
//如果不接受连接
else
delete pSocket;
}
为函数ProcessReceive()添加如下代码:
void CChatServerDoc::ProcessReceive(CClientSocket* pSocket)
{
CMsg* pMsg; //接收的消息
CString sNcom; //用户名
BOOL bIsUsedNcom = FALSE; //用户名是否已使用
bIsNewChatter = FALSE; //是否为新用户
do
{
//接收消息
pMsg = ReadMsg(pSocket);
//如果此消息是发送的新的用户名(在用户登录时客户端先发此消息)
if(pMsg->code == SENDING_NICKNAME)
{
sNcom = pMsg->m_strText;
bIsNewChatter = TRUE;
//检查此用户名是否已使用
if(IsUsedNcom(sNcom))
bIsUsedNcom = TRUE;
}
//如果连接已关闭则退出
if (pMsg->m_bClose)