C#坦克大战网络版代码

上传者: xiaoxiao108 | 上传时间: 2021-05-26 15:43:44 | 文件大小: 92KB | 文件类型: RAR
写完单机版http://blog.csdn.net/xiaoxiao108/archive/2010/12/18/6084473.aspx游戏后 再写个网络版玩玩。 开发工具vs2008 网络版实现方法很简单 1.一个服务端,多个客户端 2.服务端开个端口监听,当一个客户端程序后连接到服务端后,服务端分配个编号给客户端作为他的坦克编号 3.当有新坦克创建,坦克移动,等操作时,客户端发送数据到服务端,服务端再把数据发送到所有的客户端来实现网络游戏的同步 具体实现代码 1.服务端开启服务代码 public void Start() { //开启udp线程 Thread t = new Thread(UDPThread); t.IsBackground = true; t.Start(); //开启tcp服务 TcpListener tl = new TcpListener(TCP_PORT); tl.Start(); while (true) { TcpClient tc = tl.AcceptTcpClient(); Stream ns = tc.GetStream(); BinaryReader br = new BinaryReader(ns); int udpPort = br.ReadInt32();//br.Close();不能关闭br BinaryWriter bw = new BinaryWriter(ns); bw.Write(ID++); IPEndPoint rep = (IPEndPoint)tc.Client.RemoteEndPoint; Client c = new Client(rep.Address.ToString(), udpPort); clients.Add(c); Console.WriteLine("A Client TCP Connect! Addr- " + rep.Address.ToString() + ":" + rep.Port); } } 2.服务端udp数据接收转发代码 private void UDPThread() { Console.WriteLine("UDP thread started at port :" + UDP_PORT); byte[] buf = new byte[1024]; UdpClient uc = new UdpClient(UDP_PORT);//// 跟java有区别 如果这句话放到while外面 就不能接受第二个坦克连入 IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 0); while (true) { buf = uc.Receive(ref ipep); Console.WriteLine("a udp packet received! from " + ipep.Address + ":" + ipep.Port); //把收到的数据转发给每一个客户端 for (int i = 0; i < clients.Count; i++) { Client c = clients[i]; UdpClient _uc = new UdpClient(); _uc.Connect(c.ip, c.udpPort); _uc.Send(buf, buf.Length);

文件下载

资源详情

[{"title":"( 44 个子文件 92KB ) C#坦克大战网络版代码","children":[{"title":"TankServer","children":[{"title":"TankServer.csproj <span style='color:#111;'> 2.43KB </span>","children":null,"spread":false},{"title":"bin","children":[{"title":"Debug","children":[{"title":"TankServer.pdb <span style='color:#111;'> 15.50KB </span>","children":null,"spread":false},{"title":"TankServer.vshost.exe.manifest <span style='color:#111;'> 490B </span>","children":null,"spread":false},{"title":"TankServer.vshost.exe <span style='color:#111;'> 13.99KB </span>","children":null,"spread":false},{"title":"TankServer.exe <span style='color:#111;'> 6.50KB </span>","children":null,"spread":false}],"spread":true}],"spread":true},{"title":"obj","children":[{"title":"Debug","children":[{"title":"TankServer.pdb <span style='color:#111;'> 15.50KB </span>","children":null,"spread":false},{"title":"Refactor","children":null,"spread":false},{"title":"TempPE","children":null,"spread":false},{"title":"TankServer.csproj.FileListAbsolute.txt <span style='color:#111;'> 281B </span>","children":null,"spread":false},{"title":"TankServer.exe <span style='color:#111;'> 6.50KB </span>","children":null,"spread":false}],"spread":true}],"spread":true},{"title":"Properties","children":[{"title":"AssemblyInfo.cs <span style='color:#111;'> 1.37KB </span>","children":null,"spread":false}],"spread":true},{"title":"Program.cs <span style='color:#111;'> 3.17KB </span>","children":null,"spread":false}],"spread":true},{"title":"WindowsFormsApplication1","children":[{"title":"TankClient.resx <span style='color:#111;'> 5.87KB </span>","children":null,"spread":false},{"title":"TankClient.cs <span style='color:#111;'> 2.73KB </span>","children":null,"spread":false},{"title":"bin","children":[{"title":"Debug","children":[{"title":"WindowsFormsApplication1.vshost.exe <span style='color:#111;'> 13.99KB </span>","children":null,"spread":false},{"title":"WindowsFormsApplication1.exe <span style='color:#111;'> 18.00KB </span>","children":null,"spread":false},{"title":"新建 文本文档.txt <span style='color:#111;'> 0B </span>","children":null,"spread":false},{"title":"WindowsFormsApplication1.vshost.exe.manifest <span style='color:#111;'> 490B </span>","children":null,"spread":false},{"title":"WindowsFormsApplication1.pdb <span style='color:#111;'> 57.50KB </span>","children":null,"spread":false}],"spread":true}],"spread":true},{"title":"Missile.cs <span style='color:#111;'> 4.51KB </span>","children":null,"spread":false},{"title":"obj","children":[{"title":"Debug","children":[{"title":"WindowsFormsApplication1.exe <span style='color:#111;'> 18.00KB </span>","children":null,"spread":false},{"title":"TankClient.csproj.GenerateResource.Cache <span style='color:#111;'> 910B </span>","children":null,"spread":false},{"title":"WindowsFormsApplication1.csproj.FileListAbsolute.txt <span style='color:#111;'> 4.29KB </span>","children":null,"spread":false},{"title":"WindowsFormsApplication1.Properties.Resources.resources <span style='color:#111;'> 180B </span>","children":null,"spread":false},{"title":"Refactor","children":null,"spread":false},{"title":"TankClient.csproj.FileListAbsolute.txt <span style='color:#111;'> 1.81KB </span>","children":null,"spread":false},{"title":"TempPE","children":null,"spread":false},{"title":"WindowsFormsApplication1.pdb <span style='color:#111;'> 57.50KB </span>","children":null,"spread":false},{"title":"WindowsFormsApplication1.csproj.GenerateResource.Cache <span style='color:#111;'> 847B </span>","children":null,"spread":false},{"title":"WindowsFormsApplication1.TankClient.resources <span style='color:#111;'> 180B </span>","children":null,"spread":false}],"spread":true}],"spread":true},{"title":"Properties","children":[{"title":"Resources.Designer.cs <span style='color:#111;'> 2.83KB </span>","children":null,"spread":false},{"title":"Settings.settings <span style='color:#111;'> 249B </span>","children":null,"spread":false},{"title":"Resources.resx <span style='color:#111;'> 5.48KB </span>","children":null,"spread":false},{"title":"Settings.Designer.cs <span style='color:#111;'> 1.08KB </span>","children":null,"spread":false},{"title":"AssemblyInfo.cs <span style='color:#111;'> 1.39KB </span>","children":null,"spread":false}],"spread":true},{"title":"Program.cs <span style='color:#111;'> 510B </span>","children":null,"spread":false},{"title":"TankMoveMsg.cs <span style='color:#111;'> 2.21KB </span>","children":null,"spread":false},{"title":"TankClient.csproj <span style='color:#111;'> 4.12KB </span>","children":null,"spread":false},{"title":"NetClient.cs <span style='color:#111;'> 4.20KB </span>","children":null,"spread":false},{"title":"MissileNewMsg.cs <span style='color:#111;'> 1.77KB </span>","children":null,"spread":false},{"title":"TankClient.Designer.cs <span style='color:#111;'> 2.88KB </span>","children":null,"spread":false},{"title":"Tank.cs <span style='color:#111;'> 8.50KB </span>","children":null,"spread":false},{"title":"Msg.cs <span style='color:#111;'> 774B </span>","children":null,"spread":false},{"title":"TankNewMsg.cs <span style='color:#111;'> 2.52KB </span>","children":null,"spread":false},{"title":"GamePanel.cs <span style='color:#111;'> 524B </span>","children":null,"spread":false},{"title":"Direction.cs <span style='color:#111;'> 264B </span>","children":null,"spread":false}],"spread":false},{"title":"WindowsFormsApplication1.sln <span style='color:#111;'> 1.38KB </span>","children":null,"spread":false},{"title":"WindowsFormsApplication1.suo <span style='color:#111;'> 52.50KB </span>","children":null,"spread":false}],"spread":true}]

评论信息

  • quchuangh :
    赞一个,学到很多东西
    2015-08-07
  • abc334668500 :
    确切的说不能叫c#坦克大战网络版。
    2015-05-23
  • Garrsion :
    很不错的代码
    2015-03-04
  • 秦明峰 :
    可以用,但是比较简单。
    2014-05-28
  • liu563813130 :
    我自己也写了一个,感觉没你写得好
    2014-05-27

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明