实例介绍
【实例简介】吃棋子
(1)设计 WinForm应用程序用于获取指定主机名的 IP 地址,结果如图8-12所示
(2)编写一个可以通过因特网对弈的“吃棋子”游戏功能要求:
获取服务器可以同时服务多桌,每桌允许两个玩家通过
因特网对弈。
允许玩家自由选择坐在哪一桌的哪一方。如果两个
玩家坐在同一桌,双方应都能看到对方的状态。两个玩家均
单击“开始”按钮.游戏就开始了。
3)某桌游戏开始后,服务器以固定的时间间隔同时在
15X15的棋盘方格内向该桌随机地发送黑白两种颜色的棋
子位置,客户端程序接收到服务器发送的棋子位置和颜色后,在 15X15 棋盘的相应位置显
示棋子
@玩家坐到游戏桌座位上后,不论游戏是否开始,该玩家都可以随时调整服务器发送
棋子的时间间隔。
游戏开始后,客户端程序响应鼠标单击。每当玩家单击了某个棋子,该棋子就会从
棋盘上消失,同时具有相应颜色的玩家得 1分。注意,如果玩家单击了对方颜色的棋子,则
对方得1分。
如果两个相同颜色的棋子在水平方向或垂直方向是相邻的,那么就认为这两个棋子
是相邻的。这里不考虑对角线相邻的情况。如果相同颜色的棋子出现在相邻的位置,游戏
就结束了。该颜色对应的玩家就是失败者
同一桌的两个玩家可以聊天
【实例截图】

// Type: System.Net.Sockets.TcpClient
// Assembly: System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: D0340790-10D3-458C-8C45-0A5571791C0C
// Assembly location: C:\Windows\Microsoft.NET\Framework64\v2.0.50727\System.dll
using System.Security.Permissions;
using System.Threading;
namespace System.Net.Sockets
{
public class TcpClient : IDisposable
{
private Socket m_ClientSocket;
private bool m_Active;
private NetworkStream m_DataStream;
private AddressFamily m_Family = AddressFamily.InterNetwork;
private bool m_CleanedUp;
public TcpClient(IPEndPoint localEP)
{
if (Logging.On)
Logging.Enter(Logging.Sockets, (object) this, nameof (TcpClient), (object) localEP);
this.m_Family = localEP != null ? localEP.AddressFamily : throw new ArgumentNullException(nameof (localEP));
this.initialize();
this.Client.Bind((EndPoint) localEP);
if (!Logging.On)
return;
Logging.Exit(Logging.Sockets, (object) this, nameof (TcpClient), "");
}
public TcpClient()
: this(AddressFamily.InterNetwork)
{
if (Logging.On)
Logging.Enter(Logging.Sockets, (object) this, nameof (TcpClient), (string) null);
if (!Logging.On)
return;
Logging.Exit(Logging.Sockets, (object) this, nameof (TcpClient), (string) null);
}
public TcpClient(AddressFamily family)
{
if (Logging.On)
Logging.Enter(Logging.Sockets, (object) this, nameof (TcpClient), (object) family);
this.m_Family = family == AddressFamily.InterNetwork || family == AddressFamily.InterNetworkV6 ? family : throw new ArgumentException(SR.GetString("net_protocol_invalid_family", (object) "TCP"), nameof (family));
this.initialize();
if (!Logging.On)
return;
Logging.Exit(Logging.Sockets, (object) this, nameof (TcpClient), (string) null);
}
public TcpClient(string hostname, int port)
{
if (Logging.On)
Logging.Enter(Logging.Sockets, (object) this, nameof (TcpClient), hostname);
if (hostname == null)
throw new ArgumentNullException(nameof (hostname));
if (!ValidationHelper.ValidateTcpPort(port))
throw new ArgumentOutOfRangeException(nameof (port));
try
{
this.Connect(hostname, port);
}
catch (Exception ex)
{
switch (ex)
{
case ThreadAbortException _:
case StackOverflowException _:
case OutOfMemoryException _:
throw;
default:
if (this.m_ClientSocket != null)
this.m_ClientSocket.Close();
throw ex;
}
}
if (!Logging.On)
return;
Logging.Exit(Logging.Sockets, (object) this, nameof (TcpClient), (string) null);
}
internal TcpClient(Socket acceptedSocket)
{
if (Logging.On)
Logging.Enter(Logging.Sockets, (object) this, nameof (TcpClient), (object) acceptedSocket);
this.Client = acceptedSocket;
this.m_Active = true;
if (!Logging.On)
return;
Logging.Exit(Logging.Sockets, (object) this, nameof (TcpClient), (string) null);
}
public Socket Client
{
get => this.m_ClientSocket;
set => this.m_ClientSocket = value;
}
protected bool Active
{
get => this.m_Active;
set => this.m_Active = value;
}
public int Available => this.m_ClientSocket.Available;
public bool Connected => this.m_ClientSocket.Connected;
public bool ExclusiveAddressUse
{
get => this.m_ClientSocket.ExclusiveAddressUse;
set => this.m_ClientSocket.ExclusiveAddressUse = value;
}
public void Connect(string hostname, int port)
{
if (Logging.On)
Logging.Enter(Logging.Sockets, (object) this, nameof (Connect), hostname);
if (this.m_CleanedUp)
throw new ObjectDisposedException(this.GetType().FullName);
if (hostname == null)
throw new ArgumentNullException(nameof (hostname));
if (!ValidationHelper.ValidateTcpPort(port))
throw new ArgumentOutOfRangeException(nameof (port));
if (this.m_Active)
throw new SocketException(SocketError.IsConnected);
IPAddress[] hostAddresses = Dns.GetHostAddresses(hostname);
Exception exception = (Exception) null;
Socket socket1 = (Socket) null;
Socket socket2 = (Socket) null;
try
{
if (this.m_ClientSocket == null)
{
if (Socket.SupportsIPv4)
socket2 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
if (Socket.OSSupportsIPv6)
socket1 = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
}
foreach (IPAddress address in hostAddresses)
{
try
{
if (this.m_ClientSocket == null)
{
if (address.AddressFamily == AddressFamily.InterNetwork && socket2 != null)
{
socket2.Connect(address, port);
this.m_ClientSocket = socket2;
socket1?.Close();
}
else if (socket1 != null)
{
socket1.Connect(address, port);
this.m_ClientSocket = socket1;
socket2?.Close();
}
this.m_Family = address.AddressFamily;
this.m_Active = true;
break;
}
if (address.AddressFamily == this.m_Family)
{
this.Connect(new IPEndPoint(address, port));
this.m_Active = true;
break;
}
}
catch (Exception ex)
{
switch (ex)
{
case ThreadAbortException _:
case StackOverflowException _:
case OutOfMemoryException _:
throw;
default:
exception = ex;
continue;
}
}
}
}
catch (Exception ex)
{
switch (ex)
{
case ThreadAbortException _:
case StackOverflowException _:
case OutOfMemoryException _:
throw;
default:
exception = ex;
break;
}
}
finally
{
if (!this.m_Active)
{
socket1?.Close();
socket2?.Close();
if (exception != null)
throw exception;
throw new SocketException(SocketError.NotConnected);
}
}
if (!Logging.On)
return;
Logging.Exit(Logging.Sockets, (object) this, nameof (Connect), (string) null);
}
public void Connect(IPAddress address, int port)
{
if (Logging.On)
Logging.Enter(Logging.Sockets, (object) this, nameof (Connect), (object) address);
if (this.m_CleanedUp)
throw new ObjectDisposedException(this.GetType().FullName);
if (address == null)
throw new ArgumentNullException(nameof (address));
if (!ValidationHelper.ValidateTcpPort(port))
throw new ArgumentOutOfRangeException(nameof (port));
this.Connect(new IPEndPoint(address, port));
if (!Logging.On)
return;
Logging.Exit(Logging.Sockets, (object) this, nameof (Connect), (string) null);
}
public void Connect(IPEndPoint remoteEP)
{
if (Logging.On)
Logging.Enter(Logging.Sockets, (object) this, nameof (Connect), (object) remoteEP);
if (this.m_CleanedUp)
throw new ObjectDisposedException(this.GetType().FullName);
if (remoteEP == null)
throw new ArgumentNullException(nameof (remoteEP));
this.Client.Connect((EndPoint) remoteEP);
this.m_Active = true;
if (!Logging.On)
return;
Logging.Exit(Logging.Sockets, (object) this, nameof (Connect), (string) null);
}
public void Connect(IPAddress[] ipAddresses, int port)
{
if (Logging.On)
Logging.Enter(Logging.Sockets, (object) this, nameof (Connect), (object) ipAddresses);
this.Client.Connect(ipAddresses, port);
this.m_Active = true;
if (!Logging.On)
return;
Logging.Exit(Logging.Sockets, (object) this, nameof (Connect), (string) null);
}
[HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
public IAsyncResult BeginConnect(
string host,
int port,
AsyncCallback requestCallback,
object state)
{
if (Logging.On)
Logging.Enter(Logging.Sockets, (object) this, nameof (BeginConnect), host);
IAsyncResult asyncResult = this.Client.BeginConnect(host, port, requestCallback, state);
if (Logging.On)
Logging.Exit(Logging.Sockets, (object) this, nameof (BeginConnect), (string) null);
return asyncResult;
}
[HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
public IAsyncResult BeginConnect(
IPAddress address,
int port,
AsyncCallback requestCallback,
object state)
{
if (Logging.On)
Logging.Enter(Logging.Sockets, (object) this, nameof (BeginConnect), (object) address);
IAsyncResult asyncResult = this.Client.BeginConnect(address, port, requestCallback, state);
if (Logging.On)
Logging.Exit(Logging.Sockets, (object) this, nameof (BeginConnect), (string) null);
return asyncResult;
}
[HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
public IAsyncResult BeginConnect(
IPAddress[] addresses,
int port,
AsyncCallback requestCallback,
object state)
{
if (Logging.On)
Logging.Enter(Logging.Sockets, (object) this, nameof (BeginConnect), (object) addresses);
IAsyncResult asyncResult = this.Client.BeginConnect(addresses, port, requestCallback, state);
if (Logging.On)
Logging.Exit(Logging.Sockets, (object) this, nameof (BeginConnect), (string) null);
return asyncResult;
}
public void EndConnect(IAsyncResult asyncResult)
{
if (Logging.On)
Logging.Enter(Logging.Sockets, (object) this, nameof (EndConnect), (object) asyncResult);
this.Client.EndConnect(asyncResult);
this.m_Active = true;
if (!Logging.On)
return;
Logging.Exit(Logging.Sockets, (object) this, nameof (EndConnect), (string) null);
}
public NetworkStream GetStream()
{
if (Logging.On)
Logging.Enter(Logging.Sockets, (object) this, nameof (GetStream), "");
if (this.m_CleanedUp)
throw new ObjectDisposedException(this.GetType().FullName);
if (!this.Client.Connected)
throw new InvalidOperationException(SR.GetString("net_notconnected"));
if (this.m_DataStream == null)
this.m_DataStream = new NetworkStream(this.Client, true);
if (Logging.On)
Logging.Exit(Logging.Sockets, (object) this, nameof (GetStream), (object) this.m_DataStream);
return this.m_DataStream;
}
public void Close()
{
if (Logging.On)
Logging.Enter(Logging.Sockets, (object) this, nameof (Close), "");
((IDisposable) this).Dispose();
if (!Logging.On)
return;
Logging.Exit(Logging.Sockets, (object) this, nameof (Close), "");
}
protected virtual void Dispose(bool disposing)
{
if (Logging.On)
Logging.Enter(Logging.Sockets, (object) this, nameof (Dispose), "");
if (this.m_CleanedUp)
{
if (!Logging.On)
return;
Logging.Exit(Logging.Sockets, (object) this, nameof (Dispose), "");
}
else
{
if (disposing)
{
IDisposable dataStream = (IDisposable) this.m_DataStream;
if (dataStream != null)
{
dataStream.Dispose();
}
else
{
Socket client = this.Client;
if (client != null)
{
try
{
client.InternalShutdown(SocketShutdown.Both);
}
finally
{
client.Close();
this.Client = (Socket) null;
}
}
}
GC.SuppressFinalize((object) this);
}
this.m_CleanedUp = true;
if (!Logging.On)
return;
Logging.Exit(Logging.Sockets, (object) this, nameof (Dispose), "");
}
}
void IDisposable.Dispose() => this.Dispose(true);
~TcpClient() => this.Dispose(false);
public int ReceiveBufferSize
{
get => this.numericOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer);
set => this.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, value);
}
public int SendBufferSize
{
get => this.numericOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer);
set => this.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, value);
}
public int ReceiveTimeout
{
get => this.numericOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout);
set => this.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, value);
}
public int SendTimeout
{
get => this.numericOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout);
set => this.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, value);
}
public LingerOption LingerState
{
get => (LingerOption) this.Client.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger);
set => this.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, (object) value);
}
public bool NoDelay
{
get => this.numericOption(SocketOptionLevel.Tcp, SocketOptionName.Debug) != 0;
set => this.Client.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.Debug, value ? 1 : 0);
}
private void initialize()
{
this.Client = new Socket(this.m_Family, SocketType.Stream, ProtocolType.Tcp);
this.m_Active = false;
}
private int numericOption(SocketOptionLevel optionLevel, SocketOptionName optionName) => (int) this.Client.GetSocketOption(optionLevel, optionName);
}
}
【核心代码】
.
├── C#程序设计 大作业封面模板 2023.doc
├── GameClient
│ ├── GameClient
│ │ ├── DotColor.cs
│ │ ├── FormPlaying.cs
│ │ ├── FormPlaying.designer.cs
│ │ ├── FormPlaying.resx
│ │ ├── FormRoom.cs
│ │ ├── FormRoom.designer.cs
│ │ ├── FormRoom.resx
│ │ ├── GameClient.csproj
│ │ ├── Program.cs
│ │ ├── Properties
│ │ │ ├── AssemblyInfo.cs
│ │ │ ├── Resources.Designer.cs
│ │ │ ├── Resources.resx
│ │ │ ├── Settings.Designer.cs
│ │ │ └── Settings.settings
│ │ ├── Service.cs
│ │ ├── bin
│ │ │ └── Debug
│ │ │ ├── GameClient.exe
│ │ │ ├── GameClient.pdb
│ │ │ ├── GameClient.vshost.exe
│ │ │ ├── GameClient.vshost.exe.manifest
│ │ │ ├── black.gif
│ │ │ ├── chessboard.gif
│ │ │ └── white.gif
│ │ ├── black.gif
│ │ ├── obj
│ │ │ └── Debug
│ │ │ ├── GameClient.FormPlaying.resources
│ │ │ ├── GameClient.FormRoom.resources
│ │ │ ├── GameClient.Properties.Resources.resources
│ │ │ ├── GameClient.csproj.AssemblyReference.cache
│ │ │ ├── GameClient.csproj.CoreCompileInputs.cache
│ │ │ ├── GameClient.csproj.FileListAbsolute.txt
│ │ │ ├── GameClient.csproj.GenerateResource.cache
│ │ │ ├── GameClient.exe
│ │ │ ├── GameClient.pdb
│ │ │ ├── Refactor
│ │ │ └── TempPE
│ │ │ └── Properties.Resources.Designer.cs.dll
│ │ └── white.gif
│ ├── GameClient.sln
│ ├── GameClient.sln.DotSettings.user
│ └── GameClient.suo
├── GameServer
│ ├── GameServer
│ │ ├── FormServer.cs
│ │ ├── FormServer.designer.cs
│ │ ├── FormServer.resx
│ │ ├── GameServer.csproj
│ │ ├── GameTable.cs
│ │ ├── Player.cs
│ │ ├── Program.cs
│ │ ├── Properties
│ │ │ ├── AssemblyInfo.cs
│ │ │ ├── Resources.Designer.cs
│ │ │ ├── Resources.resx
│ │ │ ├── Settings.Designer.cs
│ │ │ └── Settings.settings
│ │ ├── Service.cs
│ │ ├── User.cs
│ │ ├── bin
│ │ │ └── Debug
│ │ │ ├── GameServer.exe
│ │ │ ├── GameServer.pdb
│ │ │ ├── GameServer.vshost.exe
│ │ │ └── GameServer.vshost.exe.manifest
│ │ └── obj
│ │ └── Debug
│ │ ├── GameServer.FormServer.resources
│ │ ├── GameServer.Properties.Resources.resources
│ │ ├── GameServer.csproj.AssemblyReference.cache
│ │ ├── GameServer.csproj.CoreCompileInputs.cache
│ │ ├── GameServer.csproj.FileListAbsolute.txt
│ │ ├── GameServer.csproj.GenerateResource.cache
│ │ ├── GameServer.exe
│ │ ├── GameServer.pdb
│ │ ├── Refactor
│ │ └── TempPE
│ ├── GameServer.sln
│ └── GameServer.suo
└── 好例子网C#_吃棋子 (2).zip
18 directories, 67 files
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论