c sharp 網(wǎng)絡(luò)聊天程序.doc_第1頁
c sharp 網(wǎng)絡(luò)聊天程序.doc_第2頁
c sharp 網(wǎng)絡(luò)聊天程序.doc_第3頁
c sharp 網(wǎng)絡(luò)聊天程序.doc_第4頁
c sharp 網(wǎng)絡(luò)聊天程序.doc_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

同步TCP網(wǎng)絡(luò)聊天程序一、 課程設(shè)計目的與任務(wù)(一) 目的加深對計算機(jī)網(wǎng)絡(luò)的基本概念和原理,以及網(wǎng)絡(luò)編程接口Socket概念及編程原理的理解;提高學(xué)生網(wǎng)絡(luò)應(yīng)用與編程、分析與解決實(shí)際問題的能力,為大型網(wǎng)絡(luò)編程打下堅實(shí)基礎(chǔ);通過撰寫課程設(shè)計報告,鍛煉學(xué)生的邏輯組織和語言表達(dá)能力;培養(yǎng)學(xué)生理論運(yùn)用于實(shí)踐的綜合應(yīng)用和設(shè)計創(chuàng)新能力。通過本次課程設(shè)計,使學(xué)生進(jìn)一步理解、領(lǐng)會C#語言和網(wǎng)絡(luò)編程技術(shù),把所學(xué)的知識運(yùn)用到具體的程序設(shè)計當(dāng)中去,編寫一個接近實(shí)際的應(yīng)用程序。本課程設(shè)計是一門綜合性實(shí)驗(yàn)。通過本次課程設(shè)計,掌握.net應(yīng)用程序設(shè)計;加深對TCP/IP協(xié)議的理解;掌握C/S編程模式;掌握Socket機(jī)制、傳輸控制協(xié)議;用戶數(shù)據(jù)報協(xié)議;掌握網(wǎng)絡(luò)抓包的原理;掌握網(wǎng)絡(luò)編程應(yīng)用程序分析、設(shè)計、編程和調(diào)試的整個過程。(二) 任務(wù)設(shè)計完成與網(wǎng)絡(luò)應(yīng)用相關(guān)題目的網(wǎng)絡(luò)應(yīng)用軟件;調(diào)試運(yùn)行之后,要求邊演示邊解釋設(shè)計的思想、過程及采用的方法;完成課程設(shè)計報告。二、 課程設(shè)計的基本要求熟練掌握網(wǎng)絡(luò)的基本概念和原理;熟練掌握網(wǎng)絡(luò)編程接口Socket概念及編程原理;掌握基于TCP/IP的Internet編程技術(shù);掌握各種軟件開發(fā)工具的使用過程及方法。三、 設(shè)備及工具硬件:微機(jī)120臺以上,I3以上處理器,1024M以上內(nèi)存、Ethernet網(wǎng)卡,交換機(jī)軟件:Windows2000/XP操作系統(tǒng),VS2010編程環(huán)境。四、 課程設(shè)計的內(nèi)容及步驟(一). 課程設(shè)計的內(nèi)容用同步TCP編寫網(wǎng)絡(luò)聊天程序,設(shè)計界面如下圖所示:客戶端:服務(wù)器端:(二). 課程設(shè)計的步驟1. 服務(wù)器端的編程創(chuàng)建TcpListener對象,調(diào)用該對象的Start方法在指定端口進(jìn)行監(jiān)聽。具體代碼及注釋如下。/保存連接的所有用戶 private List userList = new List(); IPAddress localAddress; /監(jiān)聽端口 private const int port = 51888; private TcpListener myListener; bool isNormalExit = false; public MainForm() InitializeComponent(); listBoxStatus.HorizontalScrollbar = true; IPAddressaddrIP=Dns.GetHostAddresses(Dns.GetHostName(); localAddress = addrIP0; buttonStop.Enabled = false; 循環(huán)調(diào)用AcceptTcpClient方法接受客戶端的連接請求。每得到一個新的TcpClient對象,即創(chuàng)建一個與該客戶對應(yīng)的線程,并與之通信。具體代碼及注釋如下。/ 【開始監(jiān)聽】按鈕的Click事件 private void buttonStart_Click(object sender, EventArgs e) myListener = new TcpListener(localAddress, port); myListener.Start(); AddItemToListBox(string.Format(開始在0:1監(jiān)聽客戶連接, localAddress, port); /創(chuàng)建一個線程監(jiān)聽客戶端連接請求 Thread myThread = new Thread(ListenClientConnect); myThread.Start(); buttonStart.Enabled = false; buttonStop.Enabled = true; private void ListenClientConnect() TcpClient newClient = null; while (true) try newClient = myListener.AcceptTcpClient(); catch break; /每接受一個客戶端連接就創(chuàng)建一個對應(yīng)的線程循環(huán)接收該客戶端發(fā)來的信息 User user = new User(newClient); Thread threadReceive = new Thread(ReceiveData); threadReceive.Start(user); userList.Add(user); AddItemToListBox(string.Format(0進(jìn)入, newClient.Client.RemoteEndPoint); AddItemToListBox(string.Format(當(dāng)前連接用戶數(shù):0, userList.Count); 處理接收、發(fā)送的客戶端數(shù)據(jù) private void ReceiveData(object userState) User user = (User)userState; TcpClient client = user.client; while (isNormalExit = false) string receiveString = null; try receiveString = user.br.ReadString(); catch if (isNormalExit = false) AddItemToListBox(string.Format(與0失去聯(lián)系,已終止接收該用戶信息, client.Client.RemoteEndPoint); RemoveUser(user); break; AddItemToListBox(string.Format(來自0:1, user.client.Client.RemoteEndPoint, receiveString); string splitString = receiveString.Split(,); switch (splitString0) case Login: user.userName = splitString1; SendToAllClient(user, receiveString); break; case Logout: SendToAllClient(user, receiveString); RemoveUser(user); return; case Talk: string talkString =receiveString.Substring(splitString0.Length + splitString1.Length + 2); AddItemToListBox(string.Format(0對1說:2, user.userName, splitString1, talkString); SendToClient(user, talk, + user.userName + , + talkString); foreach (User target in userList) if (target.userName = splitString1 & user.userName != splitString1) SendToClient(target, talk, + user.userName + , + talkString); break; break; default: AddItemToListBox(什么意思?。?+ receiveString); break; /服務(wù)器將一個用戶的信息發(fā)送給另一個用戶 private void SendToClient(User user, string message) try user.bw.Write(message); user.bw.Flush(); AddItemToListBox(string.Format(向0發(fā)送:1, user.userName, message); catch AddItemToListBox(string.Format(向0發(fā)送信息失敗, user.userName); /發(fā)送信息給所有用戶 private void SendToAllClient(User user, string message) string command = message.Split(,)0.ToLower(); if (command = login) for (int i = 0; i userList.Count; i+) SendToClient(userListi, message); if (userListi.userName != user.userName) SendToClient(user, login, + userListi.userName); else if(command=logout) for (int i = 0; i userList.Count; i+) if (userListi.userName != user.userName) SendToClient(userListi, message); /刪除退出的用戶,告知所有用戶,并將連接用戶數(shù)減1 private void RemoveUser(User user) userList.Remove(user); user.Close(); AddItemToListBox(string.Format(當(dāng)前連接用戶數(shù):0, userList.Count); private delegate void AddItemToListBoxDelegate(string str); private void AddItemToListBox(string str) if (listBoxStatus.InvokeRequired) AddItemToListBoxDelegate d = AddItemToListBox; listBoxStatus.Invoke(d, str); else listBoxStatus.Items.Add(str); listBoxStatus.SelectedIndex = listBoxStatus.Items.Count - 1; listBoxStatus.ClearSelected(); 關(guān)閉與客戶端的連接private void buttonStop_Click(object sender, EventArgs e) AddItemToListBox(開始停止服務(wù),并依次使用戶退出!); isNormalExit = true; for (int i = userList.Count - 1; i = 0; i-) RemoveUser(userListi); myListener.Stop(); buttonStart.Enabled = true; buttonStop.Enabled = false; /關(guān)閉窗口時觸發(fā)的事件 Private void MainForm_FormClosing(objectsender, FormClosingEventArgs e) if (myListener != null) /引發(fā)buttonStop的Click事件 buttonStop.PerformClick(); private void MainForm_Load(object sender, EventArgs e) 程序運(yùn)行的結(jié)果2. 客戶端的編程使用Connect方法與服務(wù)器建立連接,并利用TcpClient對象的GetStream方法得到網(wǎng)絡(luò)流,利用該網(wǎng)絡(luò)流與服務(wù)器進(jìn)行數(shù)據(jù)傳輸。具體代碼及注釋如下。 / 【連接服務(wù)器】按鈕的Click事件 private void buttonConnect_Click(object sender, EventArgs e) buttonConnect.Enabled = false; try client = new TcpClient(1, 51888); AddTalkMessage(連接成功); catch AddTalkMessage(連接失敗); buttonConnect.Enabled = true; return; /獲取網(wǎng)絡(luò)流 NetworkStream networkStream = client.GetStream(); /將網(wǎng)絡(luò)流作為二進(jìn)制讀寫對象 br = new BinaryReader(networkStream); bw = new BinaryWriter(networkStream); SendMessage(Login, + textBoxUserName.Text); Thread threadReceive = new Thread(new ThreadStart(ReceiveData); threadReceive.IsBackground = true; threadReceive.Start(); 創(chuàng)建線程監(jiān)聽指定端口,循環(huán)接受并處理服務(wù)器發(fā)過來的信息。具體代碼及注釋如下。/ 處|理接收的服務(wù)器端數(shù)據(jù) private void ReceiveData() string receiveString = null; while (isExit = false) try /從網(wǎng)絡(luò)流中讀出字符串 /此方法會自動判斷字符串長度前綴,并根據(jù)長度前綴讀出字符串 receiveString = br.ReadString(); catch if (isExit = false) MessageBox.Show(與服務(wù)器失去聯(lián)系); break; string splitString= receiveString.Split(,); string command = splitString0.ToLower(); switch (command) case login: /格式:login,用戶名 AddOnline(splitString1); break; case logout: /格式:logout,用戶名 RemoveUserName(splitString1); break; case talk: /格式:talk,用戶名,對話信息 AddTalkMessage(string.Format(0說:1, splitString1,receiveString.Substring( splitString0.Length + splitString1.Length + 2); break; default: AddTalkMessage(什么意思啊?: + receiveString); break; Application.Exit(); /向服務(wù)器端發(fā)送信息 private void SendMessage(string message) try /將字符串寫入網(wǎng)絡(luò)流,此方法會自動附加字符串長度前綴 bw.Write(message); bw.Flush(); catch AddTalkMessage(發(fā)送失敗!); /【發(fā)送】按鈕的Click事件 private void buttonSend_Click(object sender, EventArgs e) if (listBoxOnlineStatus.SelectedIndex != -1) SendMessage(Talk, + listBoxOnlineStatus.SelectedItem + , + textBoxSend.Text); textBoxSend.Clear(); else MessageBox.Show(請先在當(dāng)前在線中選擇對話者); 向服務(wù)器發(fā)送關(guān)閉信息,并關(guān)閉與服務(wù)器的連接。 / 關(guān)閉窗口時觸發(fā)的事件 private void MainForm_FormClosing(object sender, FormClosingEventArgs e) /未與服務(wù)器連接前client為anull if (client != null) SendM

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論