From b71bd4e9fa965c9586b81a551fe336bba3d99cc0 Mon Sep 17 00:00:00 2001 From: Mili Date: Thu, 25 Aug 2022 00:16:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=81=E8=A3=85Socket=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=BF=83=E8=B7=B3=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FunGameServer.csproj | 1 - Main.cs | 48 ++++++++++++++--------- Models/Config/Config.cs | 34 ++++++++++++++++ Models/Config/SocketEnums.cs | 27 +++++++++++++ Sockets/ClientSocket.cs | 75 +++++++++++++++++++++++++++++++++--- 5 files changed, 160 insertions(+), 25 deletions(-) create mode 100644 Models/Config/Config.cs create mode 100644 Models/Config/SocketEnums.cs diff --git a/FunGameServer.csproj b/FunGameServer.csproj index 997e3ee..2b0c232 100644 --- a/FunGameServer.csproj +++ b/FunGameServer.csproj @@ -9,7 +9,6 @@ - diff --git a/Main.cs b/Main.cs index 6f54f41..b459508 100644 --- a/Main.cs +++ b/Main.cs @@ -5,27 +5,27 @@ using System.Text.RegularExpressions; using System; using FunGameServer.Sockets; using System.Net.WebSockets; +using FunGameServer.Models.Config; bool Running = true; Socket? ServerSocket = null; -string host = "127.0.0.1"; -int port = 22222; +string host = Config.SERVER_IPADRESS; +int port = Config.SERVER_PORT; try { Task t = Task.Factory.StartNew(() => { // 创建IP地址终结点对象 - IPAddress ip = IPAddress.Parse(host); - IPEndPoint ipe = new IPEndPoint(ip, port); + IPEndPoint ip = new(IPAddress.Parse(host), port); // 创建TCP Socket对象并绑定终结点 ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - ServerSocket.Bind(ipe); + ServerSocket.Bind(ip); // 开始监听连接 - ServerSocket.Listen(0); + ServerSocket.Listen(Config.MAX_PLAYERS); Console.WriteLine("服务器启动成功,正在监听 . . ."); while (Running) @@ -39,14 +39,16 @@ try Console.WriteLine("客户端" + clientIP.ToString() + "连接 . . ."); else Console.WriteLine("未知地点客户端连接 . . ."); - Task.Factory.StartNew(() => - { - new ClientSocket(socket, Running).Start(); - }); - // 接收客户端消息 - Read(socket); - // 发送给客户端消息 - Send(socket); + if (Read(socket) && Send(socket)) + Task.Factory.StartNew(() => + { + new ClientSocket(socket, Running).Start(); + }); + else + if (clientIP != null) + Console.WriteLine("客户端" + clientIP.ToString() + "连接失败。"); + else + Console.WriteLine("客户端连接失败。"); } catch (Exception e) { @@ -87,7 +89,7 @@ Console.WriteLine("服务器已关闭,按任意键退出程序。"); Console.ReadKey(); -void Read(Socket socket) +bool Read(Socket socket) { // 接收客户端消息 byte[] buffer = new byte[2048]; @@ -95,20 +97,28 @@ void Read(Socket socket) if (length > 0) { string msg = Encoding.GetEncoding("unicode").GetString(buffer, 0, length); - Console.WriteLine("收到来自:客户端 -> " + msg); + Console.WriteLine("收到来自:客户端(玩家ID) -> " + msg); + return true; } else Console.WriteLine("客户端没有回应。"); + return false; } -void Send(Socket socket) +bool Send(Socket socket) { // 发送消息给客户端 string msg = ">> 已连接至服务器 -> [ " + host + " ] 连接成功"; - Console.WriteLine("发送给:客户端 <- " + msg); byte[] buffer = new byte[2048]; buffer = Encoding.GetEncoding("unicode").GetBytes(msg); - socket.Send(buffer); + if (socket.Send(buffer) > 0) + { + Console.WriteLine("发送给:客户端 <- " + msg); + return true; + } + else + Console.WriteLine("无法传输数据,与客户端的连接可能丢失。"); + return false; } bool IsIP(string ip) diff --git a/Models/Config/Config.cs b/Models/Config/Config.cs new file mode 100644 index 0000000..0d0d229 --- /dev/null +++ b/Models/Config/Config.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Net.Sockets; +using System.Reflection.Metadata; +using System.Text; +using System.Threading.Tasks; + +namespace FunGameServer.Models.Config +{ + public static class Config + { + public static int MAX_PLAYERS = 16; // 最多接受连接的玩家数量 + public static int ONLINE_PLAYERS = 0; // 已连接的玩家数量 + public static int CONNECTING_PLAYERS = 0; // 正在连接的玩家数量 + public static string SERVER_IPADRESS = "127.0.0.1"; // 默认IP地址 + public static int SERVER_PORT = 22222; // 默认端口 + + /// + /// string: 玩家标识ID + /// Task:玩家线程 + /// + public static ConcurrentDictionary OnlinePlayers = new ConcurrentDictionary(); + + /** + * string:房间号 + * string:玩家标识ID + * Task:玩家线程 + */ + public static ConcurrentDictionary> PlayingPlayers= new ConcurrentDictionary>(); + + } +} diff --git a/Models/Config/SocketEnums.cs b/Models/Config/SocketEnums.cs new file mode 100644 index 0000000..acaf86b --- /dev/null +++ b/Models/Config/SocketEnums.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FunGameServer.Models.Config +{ + public static class SocketEnums + { + public enum SendType + { + Login = 1, + CheckLogin = 2, + Logout = 3, + HeartBeat = 4, + } + + public enum ReadType + { + Login = 1, + CheckLogin = 2, + Logout = 3, + HeartBeat = 4, + } + } +} diff --git a/Sockets/ClientSocket.cs b/Sockets/ClientSocket.cs index 5594b52..8290c23 100644 --- a/Sockets/ClientSocket.cs +++ b/Sockets/ClientSocket.cs @@ -1,6 +1,8 @@ -using System; +using FunGameServer.Models.Config; +using System; using System.Collections.Generic; using System.Linq; +using System.IO; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; @@ -18,6 +20,59 @@ namespace FunGameServer.Sockets Running = running; } + bool Read(Socket socket) + { + // 接收客户端消息 + byte[] buffer = new byte[2048]; + int length = socket.Receive(buffer); + if (length > 0) + { + string type = Encoding.GetEncoding("unicode").GetString(buffer, 0, length); + Console.Write("收到来自:客户端(" + type + ") -> "); + buffer = new byte[2048]; + length = socket.Receive(buffer); + if (length > 0) + { + string msg = Encoding.GetEncoding("unicode").GetString(buffer, 0, length); + Console.WriteLine(msg); + int getType = Convert.ToInt32(type); + if (getType == (int)SocketEnums.ReadType.HeartBeat) // 检测心跳包 + Send(socket, getType, msg); + return true; + } + else + Console.WriteLine("客户端没有回应。"); + return false; + } + else + Console.WriteLine("客户端没有回应。"); + return false; + } + + bool Send(Socket socket, int type, string msg) + { + // 发送消息给客户端 + byte[] buffer = new byte[2048]; + buffer = Encoding.GetEncoding("unicode").GetBytes(Convert.ToString(type)); + if (socket.Send(buffer) > 0) + { + Console.Write("发送给:客户端(" + type + ") <- "); + buffer = new byte[2048]; + buffer = Encoding.GetEncoding("unicode").GetBytes(msg); + if (socket.Send(buffer) > 0) + { + Console.WriteLine("发送给:客户端(" + msg + ") <- "); + return true; + } + else + Console.WriteLine("无法传输数据,与客户端的连接可能丢失。"); + return false; + } + else + Console.WriteLine("无法传输数据,与客户端的连接可能丢失。"); + return false; + } + public void Start() { Task StringStream = Task.Factory.StartNew(() => @@ -44,7 +99,14 @@ namespace FunGameServer.Sockets Console.WriteLine("Creating: StringStream...OK"); while (Running) { - + if (Socket != null) + Read(Socket); + else + { + Console.WriteLine("ERROR: Socket is Closed."); + Console.WriteLine("DONE: StringStream is Closed."); + break; + } } } @@ -54,7 +116,8 @@ namespace FunGameServer.Sockets Console.WriteLine("Creating: IntStream...OK"); while (Running) { - + Console.WriteLine("DONE: IntStream is Closed."); + break; } } @@ -64,7 +127,8 @@ namespace FunGameServer.Sockets Console.WriteLine("Creating: DecimalStream...OK"); while (Running) { - + Console.WriteLine("DONE: DecimalStream is Closed."); + break; } } @@ -74,7 +138,8 @@ namespace FunGameServer.Sockets Console.WriteLine("Creating: ObjectStream...OK"); while (Running) { - + Console.WriteLine("DONE: ObjectStream is Closed."); + break; } } }