




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、院(系):專業(yè)班級:姓名:學號:組員:日期:成績:一、課程設計目標1. 通過C#網(wǎng)絡編程的課程設計,能夠增強我們對C#網(wǎng)絡編程的認識,更加牢固的掌握網(wǎng)絡編程的相關知識。2. 綜合運用學習到的理論知識,提高實踐能力。3. 通過小組討論形式對任務進行分解,提出實現(xiàn)方案,制定計劃,小組成員分工協(xié)作,共同完成課程設計題目,培養(yǎng)團隊合作能力。4. 課程設計期間,通過對問題的分析查找資料,培養(yǎng)資料查詢以及運用現(xiàn)代信息技術獲取相關信息并進行歸納總結的基本能力。5. 與同學討論,互相學習,提升個人學習能力。二、課程設計內(nèi)容1課程設計的內(nèi)容參考C#網(wǎng)絡應用編程實驗指導與開發(fā)實例,編寫一個網(wǎng)絡對戰(zhàn)五子棋游戲,簡單
2、的實現(xiàn)網(wǎng)絡對戰(zhàn)五子棋游戲的基本功能。1.2 由于和小組成員分工合作,本人負責客戶端方面代碼。因此,本課程設計報告主要涉及服務器端的內(nèi)容。并且把重點放在了線程管理,服務器如何管理多個玩家,服務器和客戶端如何進行通信等等。2原理介紹(服務器與客戶端通信描述)網(wǎng)絡編程的關鍵是服務器和客戶端如何通信,當服務器和客戶端建立連接后,服務器或客戶端當接收到對方發(fā)送過來的信息后,要對接收的信息做出反應。為了讓通信雙方都能理解對方發(fā)送過來的信息含義,必須事先規(guī)定每條信息的格式以及信息的含義,在本例中,規(guī)定任何一條信息,都必須以命令開頭,命令后面跟上需要的參數(shù)。命令和參數(shù)以及各參數(shù)之間均用逗號分隔。這樣接收方接收
3、到信息后,才能理解,并且根據(jù)參數(shù)可作出相應的反應。3開發(fā)環(huán)境及技術介紹3.1 開發(fā)環(huán)境:Microsoft Visual Studio 2008 Microsoft Visual Studio 2008是面向Windows Vista、Office 2007、Web 2.0的下一代開發(fā)工具,代號“Orcas”,是對Visual Studio 2005一次及時、全面的升級。VS2008引入了250多個新特性,整合了對象、關系型數(shù)據(jù)、XML的訪問方式,語言更加簡潔。使用Visual Studio 2008可以高效開發(fā)Windows應用。設計器中可以實時反映變更,XAML中智能感知功能可以提高開發(fā)效
4、率。同時Visual Studio 2008支持項目模板、調(diào)試器和部署程序。Visual Studio 2008可以高效開發(fā)Web應用,集成了AJAX 1.0,包含AJAX項目模板,它還可以高效開發(fā)Office應用和Mobile應用。3.2 引用命名空間以及相關類 命名空間: 除了一些基本的,本例中還引用了一些其他的命名空間,如下:命名空間包含允許讀寫文件和數(shù)據(jù)流的類型以及提供基本文件和目錄支持的類型。命名空間為需要嚴密控制網(wǎng)絡訪問的開發(fā)人員提供了 Windows Sockets (Winsock) 接口的托管實現(xiàn)。 、 和 類封裝有關創(chuàng)建到 Inte
5、rnet 的 TCP 和 UDP 連接的詳細信息。 用于多線程編程,對線程進行管理,如創(chuàng)建線程、啟動線程、終止線程、合并線程等等。 該命名空間為Internet網(wǎng)絡上使用的多種協(xié)議提供了方便的編程接口,利用這個命名空間提供的類,不需要考慮所使用協(xié)議的具體細節(jié),就能很快實現(xiàn)具體功能。System.Windows.Forms 命名空間包含用于創(chuàng)建基于 Windows 的應用程序的類,以充分利用 Microsoft Windows 操作系統(tǒng)中提供的豐富的用戶界面功能相關類: 類,用特定的編碼將基元數(shù)據(jù)類型讀作二進制值。類,以二進制形式將基元類型寫入流,并支持用特定的編碼寫入字符串。類,偵聽
6、來自 TCP 網(wǎng)絡客戶端的連接。Thread類,創(chuàng)建并控制線程,設置其優(yōu)先級并獲取其狀態(tài)。IPaddress 類,提供網(wǎng)際協(xié)議 (IP) 地址。IPEndPoint類,將網(wǎng)絡端點表示為 IP 地址和端口號。界面設計: 使用的控件:(1)兩個button負責啟動和終止服務。(2)listBox顯示客戶狀態(tài)信息以及服務器與客戶端通信的內(nèi)容。(3)兩個textBox控件,分別控制游戲室允許進入的最多人數(shù)和游戲室同時開出的房間數(shù)。整體界面如圖所示4服務器端主要代碼介紹 運行VS,新建一個名為GobangServer的Windows應用程序項目。4.1 添加一個User.cs類,表示所有連接到服務器的客
7、戶。代碼如下:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net.Sockets;using System.IO;namespace GobangServerclassUser publicreadonlyTcpClient client;publicreadonlyStreamReader sr;publicreadonlyStreamWriter sw;publicstring userName;public User(TcpClient cli
8、ent) this.client = client;this.userName = ""NetworkStream netStream = client.GetStream(); sr = newStreamReader(netStream, System.Text.Encoding.Default); sw = newStreamWriter(netStream, System.Text.Encoding.Default); 4.2 添加一個名為Player.cs的類,表示坐在游戲桌兩邊的玩家,代碼如下:using System;using System.Collecti
9、ons.Generic;using System.Linq;using System.Text;namespace GobangServerclassPlayer privateUser user;publicUser GameUser get return user; set user = value; privatebool start;publicbool Start get return start; set start = value; public Player() start = false; user = null; 4.3 添加一個名為GobangBoard.cs的類,表示棋
10、盤,代碼如下:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace GobangServerclassGobangBoard publicconstint None = -1; /無棋子publicconstint Black = 0; /黑色棋子publicconstint White = 1; /白色棋子privateint, grid = newint15, 15; /15*15的方格/<summary>/ 棋盤/</summary>p
11、ublicint, Grid get return grid; privateint nextIndex;publicint NextIndex / 應該黑方放棋子還是白方放棋子 get return nextIndex; set nextIndex = value; public GobangBoard()/構造函數(shù) InitializeBoard(); publicvoid InitializeBoard() / 將15*15的方格中的每個交叉點均設置為無棋子 for (int i = 0; i <= grid.GetUpperBound(0); i+) for (int j = 0
12、; j <= grid.GetUpperBound(1); j+) gridi, j = None; nextIndex = Black; publicbool IsExist(int i, int j) / 判斷放棋子的位置是否已有棋子 if (gridi, j != None) returntrue; else returnfalse; publicbool IsWin(int i, int j) / 判斷棋子落下后是否獲勝 /與方格的第ij交叉點向四個方向的連子數(shù),依次是水平,垂直,左上右下,左下右上int numbers = newint4; numbers0 = GetRowN
13、umber(i, j); numbers1 = GetColumnNumber(i, j); numbers2 = GetBacklashNumber(i, j); numbers3 = GetSlashNumber(i, j);for (int k = 0; k < numbers.Length; k+) /檢查是否獲勝 if (Math.Abs(numbersk) = 5) returntrue; returnfalse; / 判斷橫向相同顏色的棋子個數(shù)privateint GetRowNumber(int i, int j) int num = 1; /連子個數(shù)int x = i
14、+ 1; /向右檢查while (x < 15) if (gridx, j = gridi, j) /前方棋子與ij點不同時跳出循環(huán) num+; x+; else break; x = i - 1; /向左檢查while (x >= 0) if (gridx, j = gridi, j) /前方棋子與ij點不同時跳出循環(huán) num+; x-;else break; return num; privateint GetColumnNumber(int i, int j) / 判斷縱向相同顏色的棋子個數(shù) int num = 1; /連子個數(shù)int y = j + 1; /向下檢查whil
15、e (y < 15) if (gridi, y = gridi, j) /前方棋子與ij點不同時跳出循環(huán) num+; y+;else break; /向上檢查 y = j - 1;while (y >= 0) /前方棋子與ij點不同時跳出循環(huán)if (gridi, y = gridi, j) num+; y-; else break; return num; / 判斷左上到右下相同顏色的棋子個數(shù)privateint GetBacklashNumber(int i, int j) int num = 1; /連子個數(shù)int x = i + 1; /右下方向int y = j + 1;w
16、hile (x < 15 && y < 15) if (gridx, y = gridi, j) /前方棋子與ij點不同時跳出循環(huán) num+; x+; y+; else break; /左上方向(x-,y-) x = i - 1; y = j - 1;/不超出棋格while (x >= 0 && y >= 0) if (gridx, y = gridi, j) /前方棋子與ij點不同時跳出循環(huán) num+; x-; y-; else break; return num; privateint GetSlashNumber(int i, in
17、t j) / 判斷左下到右上相同顏色的棋子個數(shù) int num = 1; /連子個數(shù)int x = i - 1;int y = j + 1;while (x >= 0 && y < 15) if (gridx, y = gridi, j) /前方棋子與ij點不同時跳出循環(huán) num+; x-; y+; else break; x = i + 1; y = j - 1;while (x < 15 && y >= 0) /前方棋子與ij點不同時跳出循環(huán)if (gridx, y = gridi, j) num+; x+; y-; else bre
18、ak; return num; 4.4 添加一個名為Service.cs的類,代碼如下:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows.Forms;using System.Net.Sockets;using System.IO;namespace GobangServer/ 顯示或發(fā)送信息classService privateListBox listbox;privatedelegatevoidAddListBoxItemCallba
19、ck(string str);privateAddListBoxItemCallback addListBoxItemCallback;public Service(ListBox listbox) this.listbox = listbox; addListBoxItemCallback = newAddListBoxItemCallback(AddListBoxItem); publicvoid AddListBoxItem(string str) /比較調(diào)用AddListBoxItem方法的線程和創(chuàng)建listBox的線程是否為同一個線程if (listbox.InvokeRequire
20、d = true) listbox.Invoke(addListBoxItemCallback, str); else if (listbox.IsDisposed = false) listbox.Items.Add(str); listbox.SelectedIndex = listbox.Items.Count - 1; listbox.ClearSelected(); / 將信息發(fā)送給指定的客戶publicvoid SendToOne(User user, string str) try user.sw.WriteLine(str); user.sw.Flush(); AddListB
21、oxItem(string.Format("向0發(fā)送1", user.userName, str); catch AddListBoxItem(string.Format("向0發(fā)送信息失敗", user.userName); / 將信息發(fā)送給指定房間的所有人publicvoid SendToRoom(GameRoom gameRoom, string str) /向玩家發(fā)送for (int i = 0; i < gameRoom.gamePlayer.Length; i+) if (gameRoom.gamePlayeri.GameUser !=
22、 null) SendToOne(gameRoom.gamePlayeri.GameUser, str); /向旁觀者發(fā)送for (int i = 0; i < gameRoom.lookOnUser.Count; i+) SendToOne(gameRoom.lookOnUseri, str); publicvoid SendToAll(System.Collections.Generic.List<User> userList, string str) for (int i = 0; i < userList.Count; i+) SendToOne(userLis
23、ti, str); 4.5 添加一個名為GameRoom.cs的類,游戲室內(nèi)的每個小房間,代碼如下:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Net.Sockets;using System.Windows.Forms;namespace GobangServerclassGameRoom / 進入房間的旁觀者publicList<User> lookOnUser = newList<User>
24、;();/ 坐在玩家位置上的黑白兩個游戲者publicPlayer gamePlayer = newPlayer2;/ 黑方玩家publicPlayer BlackPlayer get return gamePlayer0; set gamePlayer0 = value; / 白方玩家publicPlayer WhitePlayer get return gamePlayer1; set gamePlayer1 = value; privateGobangBoard gameBoard = newGobangBoard();publicGobangBoard GameBoard get re
25、turn gameBoard; / 向listbox中添加顯示信息以及向客戶發(fā)送信息privateListBox listbox;privateService service;/構造函數(shù)public GameRoom(ListBox listbox) this.listbox = listbox; gamePlayer0 = newPlayer(); gamePlayer1 = newPlayer(); service = newService(listbox);/將棋盤上的棋子全部清除 gameBoard.InitializeBoard(); / 放置棋子publicvoid SetChes
26、s(int i, int j, int chessColor) /發(fā)送格式:SetChess,行,列,顏色 gameBoard.Gridi, j = chessColor; gameBoard.NextIndex = gameBoard.NextIndex = 0 ? 1 : 0; service.SendToRoom(this, string.Format("SetChess,0,1,2", i, j, chessColor);if (gameBoard.IsWin(i, j) ShowWin(chessColor); else service.SendToRoom(this, "NextChess," + gameBoard.NextIndex); privatevoid ShowWin(int chessColor) gamePlayer0.Start = false; gamePlayer1.Start = false; gameBoard.InitializeBoard(); service.SendToRoom(this, string.Format("Win,0", chessColor); 4.6 重命名Form1.cs為FormServer.cs,修改代碼如下:using S
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2023工作場所空氣中雙酚A的溶劑洗脫高效液相色譜法
- 機器人拆垛拆包機械手 自動破包投料
- 員工購房補貼借款合同協(xié)議
- 2025年叉車安全知識培訓試題(含答案)
- 譯林版八年級英語下冊期末重要知識點
- 安全風險評價與企業(yè)安全生產(chǎn)績效評估融合方法考核試卷
- 印刷機械的智能化能耗監(jiān)測與分析系統(tǒng)考核試卷
- 絲網(wǎng)印刷在教育玩具品牌建設中的作用考核試卷
- 農(nóng)業(yè)科研組織創(chuàng)新能力提升策略考核試卷
- 教育科技創(chuàng)業(yè)國際合作考核試卷
- 高速公路工作人員安全教育培訓
- 2025至2030全球及中國醫(yī)學教育行業(yè)產(chǎn)業(yè)運行態(tài)勢及投資規(guī)劃深度研究報告
- 2025至2030中國炎癥性腸?。↖BD)治療行業(yè)項目調(diào)研及市場前景預測評估報告
- 兩癌篩查培訓
- 2025年國家網(wǎng)絡安全宣傳周知識競賽考試題庫
- DB13T 5178-2020 柔性防風網(wǎng)抑塵裝置設計安裝要求
- 學校網(wǎng)評員工作管理制度
- 關于醫(yī)院“十五五”發(fā)展規(guī)劃(范本)
- 英語音節(jié)劃分試題及答案
- 夫妻債務隔離約定協(xié)議書
- 急救戰(zhàn)場搬運技術
評論
0/150
提交評論