if(pserversocket->slave[i]!
=NULL)
pserversocket->slave[i]->SendMsg(sbuf);
}
}
else///////////发送给选中的客户
pserversocket->slave[m_hostList.GetCurSel()-1]->SendMsg(sbuf);
charbuf[512]={0};
strcpy(buf,"自己:
");
CTimett;/////////////显示发送时间
strcat(buf,tt.GetCurrentTime().Format("%H:
%M:
%S").GetBuffer(8));
strcat(buf,">\r\n");
m_show+=buf;//////////////////
strcpy(buf,"");
strcat(buf,sbuf);strcat(buf,"\r\n");///////////显示发送内容
m_show+=buf;////////////////////
}
UpdateData(false);
m_showCtrl.LineScroll(m_showCtrl.GetLineCount());////////编辑框自动滚动
}
发送表情:
voidCServerDlg:
:
OnSendMood()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
CStringstr,data;/////////////////////插入表情
m_moodCtrl.GetLBText(m_moodCtrl.GetCurSel(),str);
m_send.GetWindowText(data);
m_send.SetWindowText(data+str);
}
清屏:
voidCServerDlg:
:
OnClearMsg()//清屏
{
//TODO:
Addyourcontrolnotificationhandlercodehere
m_show=_T("");
UpdateData(false);
}
步骤3:
客户C程序基本构成
创建TCP客户;
voidCClientDlg:
:
Onconnect()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
cc->Create(0);////////创建端口
if(cc->Connect(m_serverIP,1169))////////////////请求连接
{
m_send.EnableWindow(true);//允许发送
m_connect.EnableWindow(false);//connect
m_disconnect.EnableWindow(true);//disconnect
}
else
{
m_show+=_T("连接失败!
T.T\r\n");///////////
UpdateData(false);////////////
m_showCtrl.LineScroll(m_showCtrl.GetLineCount());////////
cc->Close();
}
return;
}
向服务器(m_serverIP,1169)发送连接请求;例如
cc->Connect(m_serverIP,1169);
断开连接:
voidCClientDlg:
:
Ondisconnect()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
cc->Close();////////////////////////////////断开连接
m_show+=_T("断开连接!
-.-\r\n");///////////
UpdateData(false);///////
m_showCtrl.LineScroll(m_showCtrl.GetLineCount());/////
/////////////////////////////////////////////按钮不可用
m_send.EnableWindow(false);//send
m_connect.EnableWindow(true);//connect
m_disconnect.EnableWindow(false);//disconnect
}
向服务器发送信息;例如
voidCClientDlg:
:
Onsend()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
charsbuf[256];
m_senddata.GetWindowText(sbuf,256);//获取发送框的字符串
m_senddata.SetWindowText("");//编辑框置空
cc->Send(sbuf,strlen(sbuf));//发送信息
m_senddata.SetFocus();/////焦点返回编辑框
if(strlen(sbuf)!
=0){//发送字符不为空
charbuf[512]={0};
strcpy(buf,"自己:
");//加上自己的标志
CTimett;//加上发送时间
strcat(buf,tt.GetCurrentTime().Format("%H:
%M:
%S").GetBuffer(8));
strcat(buf,">\r\n");
m_show+=buf;
strcpy(buf,"");//显示自己发送的内容
strcat(buf,sbuf);
strcat(buf,_T("\r\n"));
m_show+=buf;
UpdateData(false);
m_showCtrl.LineScroll(m_showCtrl.GetLineCount());//新消息滚动
}
}
发送表情:
voidCClientDlg:
:
OnMood()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
CStringstr,data;/////////////////////插入表情
m_moodCtrl.GetLBText(m_moodCtrl.GetCurSel(),str);
m_senddata.GetWindowText(data);
m_senddata.SetWindowText(data+str);
}
重载虚函数OnReceive,处理接收到的数据;例如
voidCClientSocket:
:
OnReceive(intnErrorCode)//接收消息
{
//TODO:
Addyourspecializedcodehereand/orcallthebaseclass
charrbuf[256];
intn;
n=this->Receive(rbuf,256);//获取收到的消息字符串
rbuf[n]='\0';
charbuf[512]={0};
strcpy(buf,"服务器端:
");
CTimett;//加上发送时间
strcat(buf,tt.GetCurrentTime().Format("%H:
%M:
%S").GetBuffer(8));
strcat(buf,">\r\n");
dlg->m_show+=buf;
strcpy(buf,"");//显示收到的信息
strcat(buf,rbuf);
strcat(buf,"\r\n");
dlg->m_show+=buf;
dlg->UpdateData(false);
dlg->m_showCtrl.LineScroll(dlg->m_showCtrl.GetLineCount());//新消息总显示
CSocket:
:
OnReceive(nErrorCode);
}
清屏:
voidCClientDlg:
:
OnClearMsg()///////////////////清屏
{
//TODO:
Addyourcontrolnotificationhandlercodehere
m_show=_T("");
UpdateData(false);
}
四.实验结果
客户端:
服务器端:
连接:
消息通信:
多用户连接:
断开连接:
五.实验问题
多用户连接时,到达最大连接数时,程序会出现问题,已连接的客户断开后,无法再连接上。
不同的客户聊天记录的信息应该显示不同,这个功能还没有做出来。