计算机网络课程设计之简单的聊天室设计代码.docx
- 文档编号:5904712
- 上传时间:2023-01-02
- 格式:DOCX
- 页数:19
- 大小:19.72KB
计算机网络课程设计之简单的聊天室设计代码.docx
《计算机网络课程设计之简单的聊天室设计代码.docx》由会员分享,可在线阅读,更多相关《计算机网络课程设计之简单的聊天室设计代码.docx(19页珍藏版)》请在冰豆网上搜索。
计算机网络课程设计之简单的聊天室设计代码
源代码
I服务器:
//ServerDlg.cpp:
implementationfile
//
#include"stdafx.h"
#include"Server.h"
#include"ServerDlg.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CAboutDlgdialogusedforAppAbout
classCAboutDlg:
publicCDialog
{
public:
CAboutDlg();
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg:
:
CAboutDlg():
CDialog(CAboutDlg:
:
IDD)
{
}
voidCAboutDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CServerDlgdialog
CServerDlg:
:
CServerDlg(CWnd*pParent/*=NULL*/)
:
CDialog(CServerDlg:
:
IDD,pParent)
{
m_hIcon=AfxGetApp()->LoadIcon(IDI_ICONAPP);
}
voidCServerDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
DDX_Control(pDX,IDC_LIST_USER,m_lbUser);
DDX_Text(pDX,IDC_EDIT_DISPLAY,m_strDisplay);
}
BEGIN_MESSAGE_MAP(CServerDlg,CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BN_START,OnBnStart)
ON_BN_CLICKED(IDC_BN_CLOSE,OnBnClose)
ON_MESSAGE(WM_RECEIVE,OnReceive)
ON_MESSAGE(WM_CLIENTCLOSE,OnClientClose)
ON_MESSAGE(WM_ACCEPT,OnAccept)
ON_WM_DESTROY()
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CServerDlgmessagehandlers
BOOLCServerDlg:
:
OnInitDialog()
{
CDialog:
:
OnInitDialog();
ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX<0xF000);
CMenu*pSysMenu=GetSystemMenu(FALSE);
if(pSysMenu!
=NULL)
{
CStringstrAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if(!
strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);
}
}
SetIcon(m_hIcon,TRUE);//Setbigicon
SetIcon(m_hIcon,FALSE);//Setsmallicon
m_iNetPort=3127;
m_Server.Initialize(this);
m_bServer=FALSE;
GetDlgItem(IDC_BN_CLOSE)->EnableWindow(FALSE);
returnTRUE;//returnTRUEunlessyousetthefocustoacontrol
}
voidCServerDlg:
:
OnSysCommand(UINTnID,LPARAMlParam)
{
if((nID&0xFFF0)==IDM_ABOUTBOX)
{
CAboutDlgdlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog:
:
OnSysCommand(nID,lParam);
}
}
voidCServerDlg:
:
OnPaint()
{
if(IsIconic())
{
CPaintDCdc(this);//devicecontextforpainting
SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);
intcxIcon=GetSystemMetrics(SM_CXICON);
intcyIcon=GetSystemMetrics(SM_CYICON);
CRectrect;
GetClientRect(&rect);
intx=(rect.Width()-cxIcon+1)/2;
inty=(rect.Height()-cyIcon+1)/2;
dc.DrawIcon(x,y,m_hIcon);
}
else
{
CDialog:
:
OnPaint();
}
}
HCURSORCServerDlg:
:
OnQueryDragIcon()
{
return(HCURSOR)m_hIcon;
}
voidCServerDlg:
:
OnBnStart()
{
//创建套接字
m_Server.Create(m_iNetPort);
//开始监听
m_Server.Listen();
//显示
CStringstrInfo;
strInfo.LoadString(IDS_CREATE_SERVER);
ShowInDisplay(strInfo);
m_bServer=TRUE;
GetDlgItem(IDC_BN_START)->EnableWindow(FALSE);
GetDlgItem(IDC_BN_CLOSE)->EnableWindow(TRUE);
}
voidCServerDlg:
:
OnBnClose()
{
CloseServer();
GetDlgItem(IDC_BN_START)->EnableWindow(TRUE);
GetDlgItem(IDC_BN_CLOSE)->EnableWindow(FALSE);
}
voidCServerDlg:
:
OnAccept()
{
//创建新客户
CMySocket*pNewClient=newCMySocket;
pNewClient->Initialize(this);
m_Server.Accept(*pNewClient);
//放入链表中
m_lsClient.AddTail(pNewClient);
}
voidCServerDlg:
:
OnClientClose(WPARAMwParam,LPARAMlParam)
{
CMySocket*p_delClient=(CMySocket*)wParam;
//关闭该用户
p_delClient->ShutDown();
charbuffer[BUFFERSIZE];
while(p_delClient->Receive(buffer,BUFFERSIZE)>0);
p_delClient->Close();
//在用户链表中删除该用户
POSITIONpsCur,psList=m_lsClient.GetHeadPosition();
CMySocket*p_curClient;
while(psList!
=NULL)
{
psCur=psList;
p_curClient=(CMySocket*)m_lsClient.GetNext(psList);
if(p_delClient==p_curClient)
{
m_lsClient.RemoveAt(psCur);
break;
}
}
//发送信息告诉其他用户该客户退出
CStringstrMsg;
strMsg.LoadString(IDS_CLIENT_CLOSE);
NETMESSAGEnetMessage(PTC_CLIENT_QUIT,p_delClient->GetName(),_T(""),p_delClient->GetName()+strMsg);
SendToAllClient(netMessage);
//界面上清理该用户信息
m_lbUser.DeleteString(m_lbUser.FindString(-1,p_delClient->GetName()));
ShowInDisplay(p_delClient->GetName()+strMsg);
//删除该用户
deletep_delClient;
}
voidCServerDlg:
:
OnReceive(WPARAMwParam,LPARAMlParam)
{
CMySocket*p_curClient=(CMySocket*)wParam;
NETMESSAGEnetMessage;
p_curClient->Receive((char*)&netMessage,sizeof(netMessage));
switch(netMessage.type)
{
casePTC_NEW:
//新客户加入
NewClientAdd(p_curClient,&netMessage);
break;
casePTC_SAY:
//普通聊天
ShowMessage(netMessage);
break;
}
//把加工过的信息发送给其他用户
SendToAllClient(netMessage);
}
voidCServerDlg:
:
CloseServer()
{
if(!
m_bServer)return;
//向所有客户发送服务端关闭的信息
CStringstrInfo;
strInfo.LoadString(IDS_CLOSESERVER);
NETMESSAGEnetMessage(PTC_SYSTEM,_T(""),_T(""),strInfo);
SendToAllClient(netMessage);
//服务端界面显示关闭信息
ShowInDisplay(strInfo);
//关闭服务端
m_Server.Close();
//关闭客户端
charbuffer[BUFFERSIZE];
CMySocket*m_pClient;
POSITIONpsList=m_lsClient.GetHeadPosition();
while(psList!
=NULL)
{
m_pClient=(CMySocket*)m_lsClient.GetNext(psList);
m_pClient->ShutDown();
while(m_pClient->Receive(buffer,BUFFERSIZE)>0);
m_pClient->Close();
deletem_pClient;
}
m_lsClient.RemoveAll();
m_bServer=FALSE;
}
//向所有户发出信息
voidCServerDlg:
:
SendToAllClient(constNETMESSAGE&netMessage)
{
CMySocket*m_pClient;
POSITIONpsList=m_lsClient.GetHeadPosition();
while(psList!
=NULL)
{
m_pClient=(CMySocket*)m_lsClient.GetNext(psList);
m_pClient->SendMsg(netMessage);
}
}
voidCServerDlg:
:
ShowInDisplay(CStringstr)
{
m_strDisplay+=str+"\r\n";
UpdateData(false);
}
voidCServerDlg:
:
OnDestroy()
{
CDialog:
:
OnDestroy();
//TODO:
Addyourmessagehandlercodehere
}
//新用户加入
voidCServerDlg:
:
NewClientAdd(CMySocket*p_client,NETMESSAGE*netMessage)
{
//存入用户名字
p_client->SetName(netMessage->form);
//在列表框中显示该用户
m_lbUser.AddString(p_client->GetName());
//发送用户列表给该客户
NETMESSAGEnetMsg(PTC_USER_LIST,_T(""),_T(""),_T(""));
intiListLen=m_lbUser.GetCount();
CStringstrMsg;
for(intindex=m_lbUser.GetTopIndex();index { m_lbUser.GetText(index,strMsg); strcpy(netMsg.data,(LPCTSTR)strMsg); p_client->SendMsg(netMsg); } //加工信息 strMsg.LoadString(IDS_NEW_CLIENT); strMsg=p_client->GetName()+strMsg; strcpy(netMessage->data,(LPCTSTR)strMsg); //显示该用户进入 ShowInDisplay(strMsg); } //显示普通消息 voidCServerDlg: : ShowMessage(NETMESSAGEnetMessage) { CStringstrTo=netMessage.to; if(strTo==_T("")) { strTo="所有人"; } CStringstrForm=netMessage.form; CStringstrMsg=netMessage.data; CStringstrOut=strForm+"对"+strTo+"说: "+strMsg; ShowInDisplay(strOut); } II客户端: //ClientDlg.cpp: implementationfile // #include"stdafx.h" #include"Client.h" #include"ClientDlg.h" #ifdef_DEBUG #definenewDEBUG_NEW #undefTHIS_FILE staticcharTHIS_FILE[]=__FILE__; #endif ///////////////////////////////////////////////////////////////////////////// //CAboutDlgdialogusedforAppAbout classCAboutDlg: publicCDialog { public: CAboutDlg(); enum{IDD=IDD_ABOUTBOX}; protected: virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport protected: DECLARE_MESSAGE_MAP() }; CAboutDlg: : CAboutDlg(): CDialog(CAboutDlg: : IDD) { } voidCAboutDlg: : DoDataExchange(CDataExchange*pDX) { CDialog: : DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg,CDialog) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// //CClientDlgdialog CClientDlg: : CClientDlg(CWnd*pParent/*=NULL*/) : CDialog(CClientDlg: : IDD,pParent) { m_strDisplay=_T(""); m_strSend=_T(""); m_bWhispering=FALSE; m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME); } voidCClientDlg: : DoDataExchange(CDataExchange*pDX) { CDialog: : DoDataExchange(pDX); DDX_Control(pDX,IDC_LIST_USER,m_lbUser); DDX_Text(pDX,IDC_EDIT_DIAPLAY,m_strDisplay); DDX_Text(pDX,IDC_EDIT_SEND,m_strSend); DDX_Check(pDX,IDC_CHECK_WHISPERING,m_bWhispering); } BEGIN_MESSAGE_MAP(CClientDlg,CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BN_CONNECT,OnBnConnect) ON_BN_CLICKED(IDC_BN_CLOSE,OnBnClose) ON_BN_CLICKED(IDC_BN_SEND,OnBnSend) ON_MESSAGE(WM_RECEIVE,OnReceive) ON_MESSAGE(WM_SOCKETCLOSE,OnServerClose) ON_MESSAGE(WM_SEND,OnSend) ON_MESSAGE(WM_CONNCET,OnConnect) ON_LBN_DBLCLK(IDC_LIST_USER,OnDblclkListUser) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// //CClientDlgmessagehandlers BOOLCClientDlg: : OnInitDialog() { CDialog: : OnInitDialog(); ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX<0xF000); CMenu*pSysMenu=GetSystemMenu(FALSE); if(pSysMenu! =NULL) { CStringstrAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if(! strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu); } } SetIcon(m_hIcon,TRUE);//Setbigicon SetIcon(m_hIcon,FALSE);//Setsmallicon //参数初始化 m_Socket.Initialize(this); m_strNetIP=_T("127.0.0.1"); m_iNetPort=3127; m_bConnect=FALSE; GetDlgItem(IDC_BN_CLOSE)->EnableWindow(FALSE); GetDlgItem(IDC_BN_SEND)->EnableWindow(FALSE); returnTRUE;//returnTRUEunlessyousetthefocustoacontrol } voidCClientDlg: : OnSysCommand(UINTnID,LPARAMlParam) { if((nID&
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机网络 课程设计 简单 聊天室 设计 代码
![提示](https://static.bdocx.com/images/bang_tan.gif)