C网络应用编程课程设计_第1页
C网络应用编程课程设计_第2页
C网络应用编程课程设计_第3页
C网络应用编程课程设计_第4页
C网络应用编程课程设计_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、院(系):专业班级:姓名:学号:组员:日期:成绩:一、课程设计目标1. 通过C#网络编程的课程设计,能够增强我们对C#网络编程的认识,更加牢固的掌握网络编程的相关知识。2. 综合运用学习到的理论知识,提高实践能力。3. 通过小组讨论形式对任务进行分解,提出实现方案,制定计划,小组成员分工协作,共同完成课程设计题目,培养团队合作能力。4. 课程设计期间,通过对问题的分析查找资料,培养资料查询以及运用现代信息技术获取相关信息并进行归纳总结的基本能力。5. 与同学讨论,互相学习,提升个人学习能力。二、课程设计内容1课程设计的内容参考C#网络应用编程实验指导与开发实例,编写一个网络对战五子棋游戏,简单

2、的实现网络对战五子棋游戏的基本功能。1.2 由于和小组成员分工合作,本人负责客户端方面代码。因此,本课程设计报告主要涉及服务器端的内容。并且把重点放在了线程管理,服务器如何管理多个玩家,服务器和客户端如何进行通信等等。2原理介绍(服务器与客户端通信描述)网络编程的关键是服务器和客户端如何通信,当服务器和客户端建立连接后,服务器或客户端当接收到对方发送过来的信息后,要对接收的信息做出反应。为了让通信双方都能理解对方发送过来的信息含义,必须事先规定每条信息的格式以及信息的含义,在本例中,规定任何一条信息,都必须以命令开头,命令后面跟上需要的参数。命令和参数以及各参数之间均用逗号分隔。这样接收方接收

3、到信息后,才能理解,并且根据参数可作出相应的反应。3开发环境及技术介绍3.1 开发环境:Microsoft Visual Studio 2008 Microsoft Visual Studio 2008是面向Windows Vista、Office 2007、Web 2.0的下一代开发工具,代号“Orcas”,是对Visual Studio 2005一次及时、全面的升级。VS2008引入了250多个新特性,整合了对象、关系型数据、XML的访问方式,语言更加简洁。使用Visual Studio 2008可以高效开发Windows应用。设计器中可以实时反映变更,XAML中智能感知功能可以提高开发效

4、率。同时Visual Studio 2008支持项目模板、调试器和部署程序。Visual Studio 2008可以高效开发Web应用,集成了AJAX 1.0,包含AJAX项目模板,它还可以高效开发Office应用和Mobile应用。3.2 引用命名空间以及相关类 命名空间: 除了一些基本的,本例中还引用了一些其他的命名空间,如下:命名空间包含允许读写文件和数据流的类型以及提供基本文件和目录支持的类型。命名空间为需要严密控制网络访问的开发人员提供了 Windows Sockets (Winsock) 接口的托管实现。 、 和  类封装有关创建到 Inte

5、rnet 的 TCP 和 UDP 连接的详细信息。 用于多线程编程,对线程进行管理,如创建线程、启动线程、终止线程、合并线程等等。 该命名空间为Internet网络上使用的多种协议提供了方便的编程接口,利用这个命名空间提供的类,不需要考虑所使用协议的具体细节,就能很快实现具体功能。System.Windows.Forms 命名空间包含用于创建基于 Windows 的应用程序的类,以充分利用 Microsoft Windows 操作系统中提供的丰富的用户界面功能相关类: 类,用特定的编码将基元数据类型读作二进制值。类,以二进制形式将基元类型写入流,并支持用特定的编码写入字符串。类,侦听

6、来自 TCP 网络客户端的连接。Thread类,创建并控制线程,设置其优先级并获取其状态。IPaddress 类,提供网际协议 (IP) 地址。IPEndPoint类,将网络端点表示为 IP 地址和端口号。界面设计: 使用的控件:(1)两个button负责启动和终止服务。(2)listBox显示客户状态信息以及服务器与客户端通信的内容。(3)两个textBox控件,分别控制游戏室允许进入的最多人数和游戏室同时开出的房间数。整体界面如图所示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()/构造函数 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交叉点向四个方向的连子数,依次是水平,垂直,左上右下,左下右上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; / 判断横向相同颜色的棋子个数privateint GetRowNumber(int i, int j) int num = 1; /连子个数int x = i

14、+ 1; /向右检查while (x < 15) if (gridx, j = gridi, j) /前方棋子与ij点不同时跳出循环 num+; x+; else break; x = i - 1; /向左检查while (x >= 0) if (gridx, j = gridi, j) /前方棋子与ij点不同时跳出循环 num+; x-;else break; return num; privateint GetColumnNumber(int i, int j) / 判断纵向相同颜色的棋子个数 int num = 1; /连子个数int y = j + 1; /向下检查whil

15、e (y < 15) if (gridi, y = gridi, j) /前方棋子与ij点不同时跳出循环 num+; y+;else break; /向上检查 y = j - 1;while (y >= 0) /前方棋子与ij点不同时跳出循环if (gridi, y = gridi, j) num+; y-; else break; return num; / 判断左上到右下相同颜色的棋子个数privateint GetBacklashNumber(int i, int j) int num = 1; /连子个数int x = i + 1; /右下方向int y = j + 1;w

16、hile (x < 15 && y < 15) if (gridx, y = gridi, j) /前方棋子与ij点不同时跳出循环 num+; x+; y+; else break; /左上方向(x-,y-) x = i - 1; y = j - 1;/不超出棋格while (x >= 0 && y >= 0) if (gridx, y = gridi, j) /前方棋子与ij点不同时跳出循环 num+; x-; y-; else break; return num; privateint GetSlashNumber(int i, in

17、t j) / 判断左下到右上相同颜色的棋子个数 int num = 1; /连子个数int x = i - 1;int y = j + 1;while (x >= 0 && y < 15) if (gridx, y = gridi, j) /前方棋子与ij点不同时跳出循环 num+; x-; y+; else break; x = i + 1; y = j - 1;while (x < 15 && y >= 0) /前方棋子与ij点不同时跳出循环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/ 显示或发送信息classService privateListBox listbox;privatedelegatevoidAddListBoxItemCallba

19、ck(string str);privateAddListBoxItemCallback addListBoxItemCallback;public Service(ListBox listbox) this.listbox = listbox; addListBoxItemCallback = newAddListBoxItemCallback(AddListBoxItem); publicvoid AddListBoxItem(string str) /比较调用AddListBoxItem方法的线程和创建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(); / 将信息发送给指定的客户publicvoid SendToOne(User user, string str) try user.sw.WriteLine(str); user.sw.Flush(); AddListB

21、oxItem(string.Format("向0发送1", user.userName, str); catch AddListBoxItem(string.Format("向0发送信息失败", user.userName); / 将信息发送给指定房间的所有人publicvoid SendToRoom(GameRoom gameRoom, string str) /向玩家发送for (int i = 0; i < gameRoom.gamePlayer.Length; i+) if (gameRoom.gamePlayeri.GameUser !=

22、 null) SendToOne(gameRoom.gamePlayeri.GameUser, str); /向旁观者发送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的类,游戏室内的每个小房间,代码如下: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中添加显示信息以及向客户发送信息privateListBox listbox;privateService service;/构造函数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) /发送格式: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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论