多人聊天系统C++Builder 6.docx
《多人聊天系统C++Builder 6.docx》由会员分享,可在线阅读,更多相关《多人聊天系统C++Builder 6.docx(19页珍藏版)》请在冰豆网上搜索。
![多人聊天系统C++Builder 6.docx](https://file1.bdocx.com/fileroot1/2023-1/14/1d336fba-8788-42ac-ba53-72d68d67c058/1d336fba-8788-42ac-ba53-72d68d67c0581.gif)
多人聊天系统C++Builder6
用户说明书
服务器程序图标
服务器窗口界面
监听:
点击监听即开启服务器,监听各个用户登陆情况;
断开:
即断开服务器,在有用户在线时,会提示确认信息;
禁止用户:
断开某些用户的连接;
内容框:
显示连接用户的IP和用户名;
发送信息窗口:
用来对所有在线用户发送信息或者对个别用户发送信息;
状态栏:
分别显示用户连接数量、连接状态和目前时间。
用户客户端程序图标
用户窗口界面
主机:
设置服务器IP的地址;
登陆:
可以自设用户名登陆或者匿名登陆服务器;
离开:
断开与服务器的连接;
接受消息窗口:
显示服务器端和客户端发送的信息;
发送窗口:
发送信息内容;
在线用户列表:
显示在线的用户昵称,选中可进行私聊对话;
状态栏:
分别显示本机IP地址,本机登陆状态和目前时间。
附录:
1、服务器代码
//---------------------------------------------------------------------------
#include
#pragmahdrstop
#include"ChatServer.h"
//---------------------------------------------------------------------------
#pragmapackage(smart_init)
#pragmaresource"*.dfm"
TForm1*Form1;
intNum=0;
//---------------------------------------------------------------------------
__fastcallTForm1:
:
TForm1(TComponent*Owner)
:
TForm(Owner)
{
ServerSocket1->Port=1500;
ServerSocket1->Active=true;
m_iClientNum=0;
m_sIsName="NA";
m_sIsChat="CT";
m_sPart="#";
m_sIsCommand="CO";
m_sTo="ALL";
MenuListen->Enabled=false;
MenuClose->Enabled=true;
Application->OnException=AppException;
TDateTimeNowTime;
NowTime=Now();
StatusBar1->Panels->Items[2]->Text=NowTime.DateTimeString();
}
//---------------------------------------------------------------------------
void__fastcallTForm1:
:
ServerSocket1Accept(TObject*Sender,
TCustomWinSocket*Socket)
{
AnsiStringsClientNum;
m_iClientNum++;
sClientNum.sprintf("有%d台客户连接",m_iClientNum);
//Num=m_iClientNum;
StatusBar1->Panels->Items[1]->Text="连接成功!
";
StatusBar1->Panels->Items[0]->Text=sClientNum;
ListUserAddr->Items->Add(Socket->RemoteAddress);
for(inti=0;iSocket->ActiveConnections;i++)
ServerSocket1->Socket->Connections[i]->SendText("CLEAR");
Sleep(100);
}
//---------------------------------------------------------------------------
void__fastcallTForm1:
:
ServerSocket1ClientRead(TObject*Sender,
TCustomWinSocket*Socket)
{
AnsiStringsTemp;
AnsiStringsPre;
AnsiStringsWelcome;
sTemp=Socket->ReceiveText();
sPre=sTemp.SubString(1,2);
if(sPre==m_sIsName)
{
AnsiStringsName;
sName=sTemp.SubString(3,sTemp.Length()-2);
ListUserName->Items->Add(sName);
for(inti=0;iSocket->ActiveConnections;i++)
{
for(intj=0;jItems->Count;j++)
{
AnsiStringsName;
sName=ListUserName->Items->Strings[j];
ServerSocket1->Socket->Connections[i]->SendText(sName);
Sleep(10);
}
sWelcome=m_sIsChat+m_sPart;
sWelcome+=AnsiString("★服务器说:
大家欢迎(")+sName+AnsiString(")的光临!
");
ServerSocket1->Socket->Connections[i]->SendText(sWelcome);
}
}
if(sPre==m_sIsChat)
{
AnsiStringsTo,sChat;
intiPos;
iPos=sTemp.Pos(m_sPart);
sTo=sTemp.SubString(3,iPos-3);
sChat=m_sIsChat+sTemp.SubString(iPos,sTemp.Length()-iPos+1);
if(sTo==AnsiString("ALL"))
for(inti=0;iSocket->ActiveConnections;i++)
ServerSocket1->Socket->Connections[i]->SendText(sChat);
if(sTo!
=AnsiString("ALL"))
{
intiIndex=ListUserName->Items->IndexOf(sTo);
ServerSocket1->Socket->Connections[iIndex]->SendText(sChat);
}
}
}
//---------------------------------------------------------------------------
void__fastcallTForm1:
:
AppException(TObject*Sender,Exception*E)
{
Application->ShowException(E);
Application->Terminate();
}
//---------------------------------------------------------------------------
void__fastcallTForm1:
:
ServerSocket1ClientDisconnect(TObject*Sender,
TCustomWinSocket*Socket)
{
AnsiStringsTemp,sClientNum;
m_iClientNum--;
sClientNum.sprintf("有%d台客户连接",m_iClientNum);
//Num=m_iClientNum;
StatusBar1->Panels->Items[0]->Text=sClientNum;
sTemp.sprintf("%s断开连接!
",Socket->RemoteAddress);
StatusBar1->Panels->Items[1]->Text=sTemp;
intiIndex;
iIndex=ListUserAddr->Items->IndexOf(Socket->RemoteAddress);
ListUserAddr->Items->Delete(iIndex);
ListUserName->Items->Delete(iIndex);
}
//---------------------------------------------------------------------------
void__fastcallTForm1:
:
ServerSocket1ClientError(TObject*Sender,
TCustomWinSocket*Socket,TErrorEventErrorEvent,int&ErrorCode)
{
ErrorCode=0;
}
//---------------------------------------------------------------------------
void__fastcallTForm1:
:
EdtSendKeyDown(TObject*Sender,WORD&Key,
TShiftStateShift)
{
if(Key==VK_RETURN)
{
if(EdtSend->Text=="")
{
ShowMessage("对不起,不能发空信息!
");
}
else
{
AnsiStringsTemp;
sTemp=m_sIsChat+m_sPart+AnsiString("★服务器说:
")+EdtSend->Text;
if(m_sTo==AnsiString("ALL"))
for(inti=0;iSocket->ActiveConnections;i++)
ServerSocket1->Socket->Connections[i]->SendText(sTemp);
if(m_sTo!
=AnsiString("ALL"))
{
intiIndex=ListUserName->Items->IndexOf(m_sTo);
ServerSocket1->Socket->Connections[iIndex]->SendText(sTemp);
}
EdtSend->Clear();
}
}
}
//---------------------------------------------------------------------------
void__fastcallTForm1:
:
ListUserNameClick(TObject*Sender)
{
m_sTo=ListUserName->Items->Strings[ListUserName->ItemIndex];
LabelNotice->Caption=AnsiString("向")+m_sTo+AnsiString("发信息!
");
}
//---------------------------------------------------------------------------
void__fastcallTForm1:
:
BtnToAllClick(TObject*Sender)
{
m_sTo="ALL";
LabelNotice->Caption="向所有人发信息";
}
//---------------------------------------------------------------------------
void__fastcallTForm1:
:
MenuCloseClick(TObject*Sender)
{
if(ServerSocket1->Active)
if(m_iClientNum!
=0)
{
if(MessageBox(Handle,"确实要断开连接吗?
","系统提示",MB_OKCANCEL|MB_ICONQUESTION)==IDOK)
{
ServerSocket1->Close();
MenuListen->Enabled=true;
MenuClose->Enabled=false;
ListUserAddr->Items->Clear();
ListUserName->Items->Clear();
m_iClientNum=0;
StatusBar1->Panels->Items[0]->Text="有0台客户连接";
StatusBar1->Panels->Items[1]->Text="服务器断开连接";
}
}
else
{
ServerSocket1->Close();
MenuListen->Enabled=true;
MenuClose->Enabled=false;
ListUserAddr->Items->Clear();
ListUserName->Items->Clear();
m_iClientNum=0;
StatusBar1->Panels->Items[0]->Text="有0台客户连接";
StatusBar1->Panels->Items[1]->Text="服务器断开连接";
}
}
//---------------------------------------------------------------------------
void__fastcallTForm1:
:
MenuDisconnectClick(TObject*Sender)
{
if(m_sTo=="ALL")
{
AnsiStringsTemp;
sTemp.sprintf("真的要断开与%s的连接吗?
","所有人");
if(MessageDlg(sTemp,mtConfirmation,
TMsgDlgButtons()<for(inti=0;iSocket->ActiveConnections;i++)
ServerSocket1->Socket->Connections[i]->Close();
}
if(m_sTo!
="ALL")
{
AnsiStringsTemp;
sTemp.sprintf("真的要断开与%s的连接吗?
",m_sTo);
if(MessageDlg(sTemp,mtConfirmation,
TMsgDlgButtons()<{
intiIndex=ListUserName->Items->IndexOf(m_sTo);
ServerSocket1->Socket->Connections[iIndex]->Close();
}
}
}
//---------------------------------------------------------------------------
void__fastcallTForm1:
:
MenuListenClick(TObject*Sender)
{
if(!
ServerSocket1->Active)
ServerSocket1->Open();
MenuListen->Enabled=false;
MenuClose->Enabled=true;
}
//---------------------------------------------------------------------------
void__fastcallTForm1:
:
btnsendClick(TObject*Sender)
{
if(EdtSend->Text=="")
{
ShowMessage("对不起,不能发空信息!
");
}
else
{
AnsiStringsTemp;
sTemp=m_sIsChat+m_sPart+AnsiString("★服务器说:
")+EdtSend->Text;
if(m_sTo==AnsiString("ALL"))
for(inti=0;iSocket->ActiveConnections;i++)
ServerSocket1->Socket->Connections[i]->SendText(sTemp);
if(m_sTo!
=AnsiString("ALL"))
{
intiIndex=ListUserName->Items->IndexOf(m_sTo);
ServerSocket1->Socket->Connections[iIndex]->SendText(sTemp);
}
EdtSend->Clear();
}
}
//---------------------------------------------------------------------------
void__fastcallTForm1:
:
FormCloseQuery(TObject*Sender,bool&CanClose)
{
if(m_iClientNum!
=0)
{
CanClose=(MessageBox(Handle,"还有用户在线,确实要退出吗?
","系统提示",MB_OKCANCEL|MB_ICONQUESTION)==IDOK);
}
else
{
CanClose=true;
}
}
//---------------------------------------------------------------------------
2、用户客户端代码
//---------------------------------------------------------------------------
#include
#pragmahdrstop
#include"ChatClient.h"
//---------------------------------------------------------------------------
#pragmapackage(smart_init)
#pragmaresource"*.dfm"
TForm1*Form1;
//---------------------------------------------------------------------------
__fastcallTForm1:
:
TForm1(TComponent*Owner)
:
TForm(Owner)
{
m_sHostAddress=Powersock1->LocalIP;
StatusBar1->Panels->Items[0]->Text=Powersock1->LocalIP;
ClientSocket1->Active=false;
ClientSocket1->Address=m_sHostAddress;
ClientSocket1->Port=1500;
m_sSpeakTo="ALL";
m_sIsName="NA";
m_sIsChat="CT";
m_sPart="#";
m_HasConnected=false;
TDateTimeNowTime;
NowTime=Now();
StatusBar1->Panels->Items[2]->Text=NowTime.DateTimeString();;
}
//---------------------------------------------------------------------------
void__fastcallTForm1:
:
ClientSocket1Connecting(TObject*Sender,
TCustomWinSocket*Socket)
{
StatusBar1->Panels->Items[1]->Text="正在连接,请稍后....";
}
//---------------------------------------------------------------------------
void__fastcallTForm1:
:
ClientSocket1Connect(TObject*Sender,
TCustomWinSocket*Socket)
{
Socket->SendText(m_sIsName+m_sName);
AnsiStringsTemp;
sTemp.sprintf("%s登录成功!
",m_sName);
MenuConnect->Enabled=false;
StatusBar1->Panels->Items[1]->Text=sTemp;
LabelNotice->Caption=m_sName+AnsiString("对所有人说....");
m_HasConnected=true;
}
//---------------------------------------------------------------------------
void__fastcallTForm1:
:
ClientSocket1Read(TObject*Sender,
TCustomWinSocket*Socket)
{
AnsiStringsInfo;
AnsiStringsType;
AnsiStringsChat;
sInfo=Socket->ReceiveText();
sType=sInfo.SubString(1,2);
if(sInfo==AnsiString("CLEAR"))
ListUser->Items->