資源簡介
里面包含聊天室的客戶端和服務(wù)器端的源文件和一份完整的設(shè)計報告。
一、 系統(tǒng)概要
本系統(tǒng)能實現(xiàn)基于VC++的網(wǎng)絡(luò)聊天室系統(tǒng)。有單獨的客戶端、服務(wù)器端。
服務(wù)器應(yīng)用程序能夠接受來自客戶端的廣播,然后向客戶端發(fā)送本機(jī)的IP與服務(wù)端口,讓客戶端接入到服務(wù)器進(jìn)行聊天,檢測用戶名是否合法(重復(fù)),服務(wù)器責(zé)接收來自客戶端的聊天信息,并根據(jù)用戶的需求發(fā)送給指定的人或所有人,能夠給出上線下線提示。客戶端能夠發(fā)出連接請求,能編輯發(fā)送信息,可以指定發(fā)給單人或所有人,能顯示聊天人數(shù),上線下線用戶等。
二、 通信規(guī)范的制定
服務(wù)請求規(guī)范:
服務(wù)器端:
(1) 創(chuàng)建一個UDP的套接字,接受來自客戶端的廣播請求,當(dāng)請求報文內(nèi)容為“REQUEST FOR IP ADDRESS AND SERVERPORT”時,接受請求,給客戶端發(fā)送本服務(wù)器TCP聊天室的端口號。
(2) 創(chuàng)建一個主要的TCP協(xié)議的套接字負(fù)責(zé)客戶端TCP連接 ,處理它的連接請求事件。
(3)在主要的TCP連接協(xié)議的套接字里面再創(chuàng)建TCP套接字保存到動態(tài)數(shù)組里,在主要的套接字接受請求后 ,就用這些套接字和客戶端發(fā)送和接受數(shù)據(jù)。
客戶端:
(1) 當(dāng)用戶按“連接”按鈕時,創(chuàng)建UDP協(xié)議套接字,給本地計算機(jī)發(fā)廣播,廣播內(nèi)容為“REQUEST FOR IP ADDRESS AND SERVERPORT”。
(2)當(dāng)收到服務(wù)器端的回應(yīng),收到服務(wù)器發(fā)來的端口號后,關(guān)閉UDP連接。根據(jù)服務(wù)器的IP地址和端口號重新創(chuàng)建TCP連接。
故我思考:客戶端一定要知道服務(wù)器的一個端口,我假設(shè)它知道服務(wù)器UDP服務(wù)的端口,通過發(fā)廣播給服務(wù)器的UDP服務(wù)套接字,然后等待該套接字發(fā)回服務(wù)器TCP聊天室服務(wù)的端口號,IP地址用ReceiveForom也苛刻得到。
通信規(guī)范
通信規(guī)范的制定主要跟老師給出的差不多,并做了一小點增加:
(增加驗證用戶名是否與聊天室已有用戶重復(fù),在服務(wù)器給客戶端的消息中,增加標(biāo)志0)
① TCP/IP數(shù)據(jù)通信 --- “聊天”消息傳輸格式
客戶機(jī) -? 服務(wù)器
(1)傳輸“用戶名”
STX+1+用戶名+ETX
(2) 悄悄話
STX+2+用戶名+”,”+內(nèi)容+ETX
(3) 對所有人說
STX+3+內(nèi)容+ETX
服務(wù)器-? 客戶機(jī)
(0)請求用戶名與在線用戶名重復(fù) //改進(jìn)
STX+0+用戶名+EXT
(1)首次傳輸在線用戶名
STX+1+用戶名+ETX
(2)傳輸新到用戶名
STX+2+用戶名+ETX
(3)傳輸離線用戶名
STX+3+用戶名+ETX
(4)傳輸聊天數(shù)據(jù)
STX+4+內(nèi)容+ETX
(注:STX為CHR(2),ETX 為CHR(3))
三、 主要模塊的設(shè)計分析
四、 系統(tǒng)運(yùn)行效果 (要求有屏幕截圖)
五、 心得與體會

代碼片段和文件信息
//?ChatClient.cpp?:?Defines?the?class?behaviors?for?the?application.
//
#include?“stdafx.h“
#include?“ChatClient.h“
#include?“ChatClientDlg.h“
#ifdef?_DEBUG
#define?new?DEBUG_NEW
#undef?THIS_FILE
static?char?THIS_FILE[]?=?__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//?CChatClientApp
BEGIN_MESSAGE_MAP(CChatClientApp?CWinApp)
//{{AFX_MSG_MAP(CChatClientApp)
//?NOTE?-?the?ClassWizard?will?add?and?remove?mapping?macros?here.
//????DO?NOT?EDIT?what?you?see?in?these?blocks?of?generated?code!
//}}AFX_MSG
ON_COMMAND(ID_HELP?CWinApp::onhelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//?CChatClientApp?construction
CChatClientApp::CChatClientApp()
{
//?TODO:?add?construction?code?here
//?Place?all?significant?initialization?in?InitInstance
}
/////////////////////////////////////////////////////////////////////////////
//?The?one?and?only?CChatClientApp?object
CChatClientApp?theApp;
/////////////////////////////////////////////////////////////////////////////
//?CChatClientApp?initialization
BOOL?CChatClientApp::InitInstance()
{
if?(!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return?FALSE;
}
//?Standard?initialization
//?If?you?are?not?using?these?features?and?wish?to?reduce?the?size
//??of?your?final?executable?you?should?remove?from?the?following
//??the?specific?initialization?routines?you?do?not?need.
#ifdef?_AFXDLL
Enable3dControls(); //?Call?this?when?using?MFC?in?a?shared?DLL
#else
Enable3dControlsStatic(); //?Call?this?when?linking?to?MFC?statically
#endif
CChatClientDlg?dlg;
m_pMainWnd?=?&dlg;
int?nResponse?=?dlg.DoModal();
if?(nResponse?==?IDOK)
{
//?TODO:?Place?code?here?to?handle?when?the?dialog?is
//??dismissed?with?OK
}
else?if?(nResponse?==?IDCANCEL)
{
//?TODO:?Place?code?here?to?handle?when?the?dialog?is
//??dismissed?with?Cancel
}
//?Since?the?dialog?has?been?closed?return?FALSE?so?that?we?exit?the
//??application?rather?than?start?the?application‘s?message?pump.
return?FALSE;
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????120176??2009-12-30?14:00??ChatClient\ChatClient.aps
?????文件???????1797??2009-12-30?15:01??ChatClient\ChatClient.clw
?????文件???????2180??2009-12-26?19:19??ChatClient\ChatClient.cpp
?????文件???????4570??2009-12-29?18:38??ChatClient\ChatClient.dsp
?????文件????????733??2009-12-30?04:32??ChatClient\ChatClient.dsw
?????文件???????1368??2009-12-26?19:19??ChatClient\ChatClient.h
?????文件?????123904??2009-12-30?15:01??ChatClient\ChatClient.ncb
?????文件??????56832??2009-12-30?15:01??ChatClient\ChatClient.opt
?????文件????????254??2009-12-30?11:54??ChatClient\ChatClient.plg
?????文件???????6041??2009-12-30?09:47??ChatClient\ChatClient.rc
?????文件???????4665??2009-12-30?11:02??ChatClient\ChatClientDlg.cpp
?????文件???????1771??2009-12-30?11:02??ChatClient\ChatClientDlg.h
?????文件???????6930??2009-12-30?11:45??ChatClient\ClientSocket.cpp
?????文件???????2304??2009-12-30?11:02??ChatClient\ClientSocket.h
?????文件?????323436??2009-12-30?11:50??ChatClient\Debug\ChatClient.ilk
?????文件??????13904??2009-12-30?11:22??ChatClient\Debug\ChatClient.obj
?????文件????5567824??2009-12-26?20:30??ChatClient\Debug\ChatClient.pch
?????文件?????435200??2009-12-30?11:50??ChatClient\Debug\ChatClient.pdb
?????文件?????101252??2009-12-30?09:47??ChatClient\Debug\ChatClient.res
?????文件??????22750??2009-12-30?11:22??ChatClient\Debug\ChatClientDlg.obj
?????文件??????39612??2009-12-30?11:50??ChatClient\Debug\ClientSocket.obj
?????文件?????106378??2009-12-26?20:30??ChatClient\Debug\StdAfx.obj
?????文件??????12390??2009-12-30?11:50??ChatClient\Debug\UserNameDlg.obj
?????文件?????222208??2009-12-30?14:39??ChatClient\Debug\vc60.idb
?????文件?????372736??2009-12-30?11:50??ChatClient\Debug\vc60.pdb
?????文件???????3651??2009-12-26?19:19??ChatClient\ReadMe.txt
?????文件??????98654??2009-12-26?20:41??ChatClient\res\ChatClient.ico
?????文件????????402??2009-12-26?19:19??ChatClient\res\ChatClient.rc2
?????文件????????693??2009-12-26?20:41??ChatClient\res\Master.manifest
?????文件???????1053??2009-12-29?18:06??ChatClient\resource.h
............此處省略57個文件信息
評論
共有 條評論