ImageVerifierCode 换一换
格式:DOCX , 页数:40 ,大小:801.84KB ,
资源ID:16677682      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/16677682.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(《C#网络应用编程》课程设计Word文档格式.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

《C#网络应用编程》课程设计Word文档格式.docx

1、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支持项目模板、调试器和部署

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

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

4、ryWriter类,以二进制形式将基元类型写入流,并支持用特定的编码写入字符串。TcpListener类,侦听来自 TCP 网络客户端的连接。Thread类,创建并控制线程,设置其优先级并获取其状态。IPaddress 类,提供网际协议 (IP) 地址。IPEndPoint类,将网络端点表示为 IP 地址和端口号。界面设计:使用的控件:(1)两个button负责启动和终止服务。(2)listBox显示客户状态信息以及服务器与客户端通信的内容。(3)两个textBox控件,分别控制游戏室允许进入的最多人数和游戏室同时开出的房间数。整体界面如图所示4服务器端主要代码介绍 运行VS,新建一个名为Go

5、bangServer的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

6、 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 添加一个名为Play

7、er.cs的类,表示坐在游戏桌两边的玩家,代码如下: class Player private User user; public User GameUser get return user; set user = value; private bool start; public bool Start return start; start = value; public Player() start = false; user = null;4.3 添加一个名为GobangBoard.cs的类,表示棋盘,代码如下: class GobangBoard public const int No

8、ne = -1; /无棋子 public const int Black = 0; /黑色棋子 public const int White = 1; /白色棋子 private int, grid = new int15, 15; /15*15的方格 / / 棋盘/summary public int, Grid return grid; private int nextIndex; public int NextIndex / 应该黑方放棋子还是白方放棋子 return nextIndex; nextIndex = value; public GobangBoard()/构造函数 Init

9、ializeBoard(); 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 f

10、alse; 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 (

11、Math.Abs(numbersk) = 5) return false; / 判断横向相同颜色的棋子个数 private int GetRowNumber(int i, int j) int num = 1; /连子个数 int x = i + 1; /向右检查 while (x = 0) num+; x-; break; return num; private int GetColumnNumber(int i, int j) / 判断纵向相同颜色的棋子个数 int y = j + 1; /向下检查 while (y /前方棋子与ij点不同时跳出循环 if (gridi, y = grid

12、i, j) num+; y-;/ 判断左上到右下相同颜色的棋子个数 private int GetBacklashNumber(int i, int j) /右下方向 15 & y if (gridx, y = gridi, j) /前方棋子与ij点不同时跳出循环 x-; private int GetSlashNumber(int i, int j) / 判断左下到右上相同颜色的棋子个数 int x = i - 1; x = i + 1; if (gridx, y = gridi, j)4.4 添加一个名为Service.cs的类,代码如下:using System.Windows.Form

13、s; / 显示或发送信息 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 voi

14、d AddListBoxItem(string str) /比较调用AddListBoxItem方法的线程和创建listBox的线程是否为同一个线程 if (listbox.InvokeRequired = true) listbox.Invoke(addListBoxItemCallback, str); if (listbox.IsDisposed = false) listbox.Items.Add(str); listbox.SelectedIndex = listbox.Items.Count - 1; listbox.ClearSelected(); / 将信息发送给指定的客户 p

15、ublic void SendToOne(User user, string str) try user.sw.WriteLine(str); user.sw.Flush(); AddListBoxItem(string.Format(向0发送1, user.userName, str); catch向0发送信息失败, user.userName); / 将信息发送给指定房间的所有人 public void SendToRoom(GameRoom gameRoom, string str) /向玩家发送 gameRoom.gamePlayer.Length; if (gameRoom.game

16、Playeri.GameUser != null) SendToOne(gameRoom.gamePlayeri.GameUser, str); /向旁观者发送 gameRoom.lookOnUser.Count; SendToOne(gameRoom.lookOnUseri, str); public void SendToAll(System.Collections.Generic.List userList, string str) userList.Count; SendToOne(userListi, str);4.5 添加一个名为GameRoom.cs的类,游戏室内的每个小房间,代

17、码如下: class GameRoom / 进入房间的旁观者 public List lookOnUser = new List(); / 坐在玩家位置上的黑白两个游戏者 public Player gamePlayer = new Player2; / 黑方玩家 public Player BlackPlayer return gamePlayer0; gamePlayer0 = value; / 白方玩家 public Player WhitePlayer return gamePlayer1; gamePlayer1 = value; private GobangBoard gameBo

18、ard = new GobangBoard(); public GobangBoard GameBoard return gameBoard; / 向listbox中添加显示信息以及向客户发送信息 private Service service; /构造函数 public GameRoom(ListBox listbox) gamePlayer0 = new Player(); gamePlayer1 = new Player(); service = new Service(listbox); /将棋盘上的棋子全部清除 gameBoard.InitializeBoard(); / 放置棋子

19、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); service.SendToRoom

20、(this, NextChess, + gameBoard.NextIndex); private void ShowWin(int chessColor) gamePlayer0.Start = false; gamePlayer1.Start = false;Win,0, chessColor);4.6 重命名Form1.cs为FormServer.cs,修改代码如下:using System.ComponentModel;using System.Data;using System.Drawing;using System.Threading;using System.Net; public partia

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1