由于这段时间比较忙,所以也很久没发布过新的教程,这几天刚好要为一个项目写服务端程序,所以顺便也在Unity3d里面实现了一个简单的客户端,多个客户端一同使用就是一个简单的公共聊天室了。服务端为一个控制台程序使用C#实现,当然,在Unity3d中也相应地使用了C#语言实现客户端,服务端和客户端能实现消息的互通,当服务端接收到某客户端发送过来的消息时将会对客户端列表成员进行广播,这是公共聊天室的最基本的形式。Socket通讯是网络游戏最为基础的知识,因此这个实例能向有志投身于网游行业的初学者提供指导意义。
这篇文章来自狗刨学习网
Program.cs
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.
using System;
using System.Collections.Generic; using System.Linq; using System.Text;
using System.Net.Sockets;
namespace TestServer {
class Program {
// 设置连接端口
12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37.
const int portNo = 500;
static void Main(string[] args) {
// 初始化服务器IP
System.Net.IPAddress
localAdd
=
System.Net.IPAddress.Parse(\
// 创建TCP侦听器
TcpListener listener = new TcpListener(localAdd, portNo);
listener.Start();
// 显示服务器启动信息
Console.WriteLine(\
// 循环接受客户端的连接请求 while (true) {
ChatClient
user
=
new
ChatClient(listener.AcceptTcpClient());
// 显示连接客户端的IP与端口
Console.WriteLine(user._clientIP + \ } } } }
复制代码 ChatClient.cs
1. 2. 3. 4. 5.
using System;
using System.Collections.Generic; using System.Linq; using System.Text;
using System.Collections;
6. 7. 8. 9. 10. 11. 12.
表
using System.Net.Sockets;
namespace TestServer {
class ChatClient {
public static Hashtable ALLClients = new Hashtable(); // 客户列
13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34.
private TcpClient _client; // 客户端实体 public string _clientIP; // 客户端IP private string _clientNick; // 客户端昵称
private byte[] data; // 消息数据
private bool ReceiveNick = true;
public ChatClient(TcpClient client) {
this._client = client;
this._clientIP = client.Client.RemoteEndPoint.ToString();
// 把当前客户端实例添加到客户列表当中 ALLClients.Add(this._clientIP, this);
data = new byte[this._client.ReceiveBufferSize];
// 从服务端获取消息
client.GetStream().BeginRead(data,
0,
System.Convert.ToInt32(this._client.ReceiveBufferSize), null);
ReceiveMessage,
35. 36. 37. 38.
}
// 从客戶端获取消息
public void ReceiveMessage(IAsyncResult ar)
39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72.
{
int bytesRead;
try {
lock (this._client.GetStream()) {
bytesRead = this._client.GetStream().EndRead(ar); }
if (bytesRead < 1) {
ALLClients.Remove(this._clientIP);
Broadcast(this._clientNick + \
return; } else {
string
messageReceived
=
System.Text.Encoding.ASCII.GetString(data, 0, bytesRead);
if (ReceiveNick) {
this._clientNick = messageReceived;
Broadcast(this._clientNick + \
chat.\
//this.sendMessage(\
ReceiveNick = false; } else {
73. 74. 75. 76. 77. 78. 79. 80.
Broadcast(this._clientNick + \+
messageReceived);
} }
lock (this._client.GetStream()) {
this._client.GetStream().BeginRead(data, 0,
ReceiveMessage,
System.Convert.ToInt32(this._client.ReceiveBufferSize), null);
81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99.
} }
catch (Exception ex) {
ALLClients.Remove(this._clientIP);
Broadcast(this._clientNick + \ } }
// 向客戶端发送消息
public void sendMessage(string message) { try {
System.Net.Sockets.NetworkStream ns;
lock (this._client.GetStream()) {
100. ns = this._client.GetStream(); 101. } 102.
103. // 对信息进行编码 104.
byte[]
bytesToSend
=
System.Text.Encoding.ASCII.GetBytes(message);