《C#网络应用编程》课程设计(共53页).doc
精选优质文档-倾情为你奉上C#网络应用编程院(系): 专业班级: 姓名: 学号: 组员: 日期: 成绩: 专心-专注-专业一、课程设计目标1. 通过C#网络编程的课程设计,能够增强我们对C#网络编程的认识,更加牢固的掌握网络编程的相关知识。2. 综合运用学习到的理论知识,提高实践能力。3. 通过小组讨论形式对任务进行分解,提出实现方案,制定计划,小组成员分工协作,共同完成课程设计题目,培养团队合作能力。4. 课程设计期间,通过对问题的分析查找资料,培养资料查询以及运用现代信息技术获取相关信息并进行归纳总结的基本能力。5. 与同学讨论,互相学习,提升个人学习能力。二、课程设计内容1课程设计的内容1.1参考C#网络应用编程实验指导与开发实例,编写一个网络对战五子棋游戏,简单的实现网络对战五子棋游戏的基本功能。1.2 由于和小组成员分工合作,本人负责客户端方面代码。因此,本课程设计报告主要涉及服务器端的内容。并且把重点放在了线程管理,服务器如何管理多个玩家,服务器和客户端如何进行通信等等。2原理介绍(服务器与客户端通信描述) 网络编程的关键是服务器和客户端如何通信,当服务器和客户端建立连接后,服务器或客户端当接收到对方发送过来的信息后,要对接收的信息做出反应。为了让通信双方都能理解对方发送过来的信息含义,必须事先规定每条信息的格式以及信息的含义,在本例中,规定任何一条信息,都必须以命令开头,命令后面跟上需要的参数。命令和参数以及各参数之间均用逗号分隔。这样接收方接收到信息后,才能理解,并且根据参数可作出相应的反应。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中智能感知功能可以提高开发效率。同时Visual Studio 2008支持项目模板、调试器和部署程序。Visual Studio 2008可以高效开发Web应用,集成了AJAX 1.0,包含AJAX项目模板,它还可以高效开发Office应用和Mobile应用。3.2 引用命名空间以及相关类 命名空间: 除了一些基本的,本例中还引用了一些其他的命名空间,如下:System.IO 命名空间包含允许读写文件和数据流的类型以及提供基本文件和目录支持的类型。System.Net.Sockets 命名空间为需要严密控制网络访问的开发人员提供了 Windows Sockets (Winsock) 接口的托管实现。 、 和 类封装有关创建到 Internet 的 TCP 和 UDP 连接的详细信息。System.Threading 用于多线程编程,对线程进行管理,如创建线程、启动线程、终止线程、合并线程等等。System.Net 该命名空间为Internet网络上使用的多种协议提供了方便的编程接口,利用这个命名空间提供的类,不需要考虑所使用协议的具体细节,就能很快实现具体功能。 System.Windows.Forms 命名空间包含用于创建基于 Windows 的应用程序的类,以充分利用 Microsoft Windows 操作系统中提供的丰富的用户界面功能相关类: 类,用特定的编码将基元数据类型读作二进制值。类,以二进制形式将基元类型写入流,并支持用特定的编码写入字符串。类,侦听来自 TCP 网络客户端的连接。Thread类,创建并控制线程,设置其优先级并获取其状态。IPaddress 类,提供网际协议 (IP) 地址。IPEndPoint类,将网络端点表示为 IP 地址和端口号。界面设计: 使用的控件:(1)两个button负责启动和终止服务。(2)listBox显示客户状态信息以及服务器与客户端通信的内容。(3)两个textBox控件,分别控制游戏室允许进入的最多人数和游戏室同时开出的房间数。整体界面如图所示4服务器端主要代码介绍 运行VS,新建一个名为GobangServer的Windows应用程序项目。4.1 添加一个User.cs类,表示所有连接到服务器的客户。代码如下:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net.Sockets;using System.IO;namespace GobangServer class User public readonly TcpClient client; public readonly StreamReader sr; public readonly StreamWriter sw; public string userName; public User(TcpClient client) this.client = client; this.userName = "" NetworkStream netStream = client.GetStream(); sr = new StreamReader(netStream, System.Text.Encoding.Default); sw = new StreamWriter(netStream, System.Text.Encoding.Default); 4.2 添加一个名为Player.cs的类,表示坐在游戏桌两边的玩家,代码如下:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace GobangServer class Player private User user; public User GameUser get return user; set user = value; private bool start; public bool Start get return start; set start = value; public Player() start = false; user = null; 4.3 添加一个名为GobangBoard.cs的类,表示棋盘,代码如下:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace GobangServer class GobangBoard public const int None = -1; /无棋子 public const int Black = 0; /黑色棋子 public const int White = 1; /白色棋子 private int, grid = new int15, 15; /15*15的方格 / <summary> / 棋盘 / </summary> public int, Grid get return grid; private int nextIndex; public int NextIndex / 应该黑方放棋子还是白方放棋子 get return nextIndex; set nextIndex = value; public GobangBoard()/构造函数 InitializeBoard(); public void InitializeBoard() / 将15*15的方格中的每个交叉点均设置为无棋子 for (int i = 0; i <= grid.GetUpperBound(0); i+) for (int j = 0; j <= grid.GetUpperBound(1); j+) gridi, j = None; nextIndex = Black; public bool IsExist(int i, int j) / 判断放棋子的位置是否已有棋子 if (gridi, j != None) return true; else return false; public bool IsWin(int i, int j) / 判断棋子落下后是否获胜 /与方格的第ij交叉点向四个方向的连子数,依次是水平,垂直,左上右下,左下右上 int numbers = new int4; numbers0 = GetRowNumber(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) return true; return false; / 判断横向相同颜色的棋子个数 private int GetRowNumber(int i, int j) int num = 1; /连子个数 int x = i + 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; private int GetColumnNumber(int i, int j) / 判断纵向相同颜色的棋子个数 int num = 1; /连子个数 int y = j + 1; /向下检查 while (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; / 判断左上到右下相同颜色的棋子个数 private int GetBacklashNumber(int i, int j) int num = 1; /连子个数 int x = i + 1; /右下方向 int y = j + 1; while (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; private int GetSlashNumber(int i, int 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 break; 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 / 显示或发送信息 class Service private ListBox listbox; private delegate void AddListBoxItemCallback(string str); private AddListBoxItemCallback addListBoxItemCallback; public Service(ListBox listbox) this.listbox = listbox; addListBoxItemCallback = new AddListBoxItemCallback(AddListBoxItem); public void AddListBoxItem(string str) /比较调用AddListBoxItem方法的线程和创建listBox的线程是否为同一个线程 if (listbox.InvokeRequired = true) listbox.Invoke(addListBoxItemCallback, str); else if (listbox.IsDisposed = false) listbox.Items.Add(str); listbox.SelectedIndex = listbox.Items.Count - 1; listbox.ClearSelected(); / 将信息发送给指定的客户 public void SendToOne(User user, string str) try user.sw.WriteLine(str); user.sw.Flush(); AddListBoxItem(string.Format("向0发送1", user.userName, str); catch AddListBoxItem(string.Format("向0发送信息失败", user.userName); / 将信息发送给指定房间的所有人 public void SendToRoom(GameRoom gameRoom, string str) /向玩家发送 for (int i = 0; i < gameRoom.gamePlayer.Length; i+) if (gameRoom.gamePlayeri.GameUser != null) SendToOne(gameRoom.gamePlayeri.GameUser, str); /向旁观者发送 for (int i = 0; i < gameRoom.lookOnUser.Count; i+) SendToOne(gameRoom.lookOnUseri, str); public void SendToAll(System.Collections.Generic.List<User> userList, string str) for (int i = 0; i < userList.Count; i+) SendToOne(userListi, 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 GobangServer class GameRoom / 进入房间的旁观者 public List<User> lookOnUser = new List<User>(); / 坐在玩家位置上的黑白两个游戏者 public Player gamePlayer = new Player2; / 黑方玩家 public Player BlackPlayer get return gamePlayer0; set gamePlayer0 = value; / 白方玩家 public Player WhitePlayer get return gamePlayer1; set gamePlayer1 = value; private GobangBoard gameBoard = new GobangBoard(); public GobangBoard GameBoard get return gameBoard; / 向listbox中添加显示信息以及向客户发送信息 private ListBox listbox; private Service service; /构造函数 public GameRoom(ListBox listbox) this.listbox = listbox; gamePlayer0 = new Player(); gamePlayer1 = new Player(); service = new Service(listbox); /将棋盘上的棋子全部清除 gameBoard.InitializeBoard(); / 放置棋子 public void SetChess(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); private void 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 System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Net.Sockets;using System.IO;using System.Threading;using System.Net;namespace GobangServer public partial class FormServer : Form private int maxUsers; List<User> userList = new List<User>(); private int maxRoomNumbers; private GameRoom gameRoom; IPAddress localAddress; private int port = 51888; private TcpListener myListener; private Service service; public FormServer() InitializeComponent(); listBoxStatus.HorizontalScrollbar = true; service = new Service(listBoxStatus); /加载窗体触发的事件 private void FormServer_Load(object sender, EventArgs e) listBoxStatus.HorizontalScrollbar = true; IPAddress addrIp = Dns.GetHostAddresses(Dns.GetHostName(); foreach (IPAddress p in addrIp) if (p.AddressFamily = AddressFamily.InterNetwork)