From 847d176840a12c99b148574fb81cc0f07a74a3db Mon Sep 17 00:00:00 2001 From: Mili Date: Mon, 22 Aug 2022 20:41:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=A1=B9=E7=9B=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FunGameServer.csproj | 15 +++++ FunGameServer.sln | 25 +++++++++ Main.cs | 120 ++++++++++++++++++++++++++++++++++++++++ Sockets/ClientSocket.cs | 81 +++++++++++++++++++++++++++ 4 files changed, 241 insertions(+) create mode 100644 FunGameServer.csproj create mode 100644 FunGameServer.sln create mode 100644 Main.cs create mode 100644 Sockets/ClientSocket.cs diff --git a/FunGameServer.csproj b/FunGameServer.csproj new file mode 100644 index 0000000..997e3ee --- /dev/null +++ b/FunGameServer.csproj @@ -0,0 +1,15 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + diff --git a/FunGameServer.sln b/FunGameServer.sln new file mode 100644 index 0000000..e4cce4d --- /dev/null +++ b/FunGameServer.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32804.467 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FunGameServer", "FunGameServer.csproj", "{CFC4F490-967B-4F12-883E-86C2A6E61461}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {CFC4F490-967B-4F12-883E-86C2A6E61461}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CFC4F490-967B-4F12-883E-86C2A6E61461}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CFC4F490-967B-4F12-883E-86C2A6E61461}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CFC4F490-967B-4F12-883E-86C2A6E61461}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B4235C3B-6B99-4B97-B7F1-B586B219B51E} + EndGlobalSection +EndGlobal diff --git a/Main.cs b/Main.cs new file mode 100644 index 0000000..1a3c226 --- /dev/null +++ b/Main.cs @@ -0,0 +1,120 @@ +using System.Net.Sockets; +using System.Net; +using System.Text; +using System.Text.RegularExpressions; +using System; +using FunGameServer.Sockets; +using System.Net.WebSockets; + +bool Running = true; +Socket? ServerSocket = null; + +string host = "127.0.0.1"; +int port = 22222; + +try +{ + Task t = Task.Factory.StartNew(() => + { + // 创建IP地址终结点对象 + IPAddress ip = IPAddress.Parse(host); + IPEndPoint ipe = new IPEndPoint(ip, port); + + // 创建TCP Socket对象并绑定终结点 + ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + ServerSocket.Bind(ipe); + + // 开始监听连接 + ServerSocket.Listen(0); + Console.WriteLine("服务器启动成功,正在监听 . . ."); + + while (Running) + { + Socket socket; + try + { + socket = ServerSocket.Accept(); + IPEndPoint? clientIP = (IPEndPoint?)socket.RemoteEndPoint; + if (clientIP != null) + Console.WriteLine("客户端" + clientIP.ToString() + "连接 . . ."); + else + Console.WriteLine("未知地点客户端连接 . . ."); + Task.Factory.StartNew(() => + { + new ClientSocket(socket, Running).Start(); + }); + // 接收客户端消息 + Receive(socket); + // 发送给客户端消息 + Send(socket); + } + catch (Exception e) + { + Console.WriteLine("ERROR: 客户端断开连接!\n" + e.StackTrace); + } + } + + }); +} +catch (Exception e) +{ + Console.WriteLine(e.StackTrace); + if (ServerSocket != null) + { + ServerSocket.Close(); + ServerSocket = null; + } +} +finally +{ + while (Running) + { + string? order = ""; + order = Console.ReadLine(); + if (order != null && !order.Equals("")) + { + switch (order) + { + case "quit": + Running = false; + break; + } + } + } +} + +Console.WriteLine("服务器已关闭,按任意键退出程序。"); +Console.ReadKey(); + + +static void Receive(Socket socket) +{ + byte[] bytes = new byte[1024]; + //从客户端接收消息 + int len = socket.Receive(bytes, bytes.Length, 0); + //将消息转为字符串 + string recvStr = Encoding.ASCII.GetString(bytes, 0, len); + Console.WriteLine("接收的客户端消息 : {0}", recvStr); +} + +void Send(Socket socket) +{ + string sendStr = ">> 服务器" + host + ":" + port + "连接成功"; + Console.WriteLine("发送给客户端消息 : {0}", sendStr); + // 将字符串消息转为数组 + byte[] bytes = Encoding.ASCII.GetBytes(sendStr); + //发送消息给客户端 + socket.Send(bytes, bytes.Length, 0); +} + +bool IsIP(string ip) +{ + //判断是否为IP + return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"); +} + +bool IsEmail(string ip) +{ + //判断是否为Email + return Regex.IsMatch(ip, @"^(\w)+(\.\w)*@(\w)+((\.\w+)+)$"); +} \ No newline at end of file diff --git a/Sockets/ClientSocket.cs b/Sockets/ClientSocket.cs new file mode 100644 index 0000000..5594b52 --- /dev/null +++ b/Sockets/ClientSocket.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace FunGameServer.Sockets +{ + public class ClientSocket + { + public bool Running { get; set; } = false; + public Socket? Socket { get; set; } = null; + + public ClientSocket(Socket socket, bool running) + { + Socket = socket; + Running = running; + } + + public void Start() + { + Task StringStream = Task.Factory.StartNew(() => + { + CreateStringStream(); + }); + Task IntStream = Task.Factory.StartNew(() => + { + CreateStringStream(); + }); + Task DecimalStream = Task.Factory.StartNew(() => + { + CreateDecimalStream(); + }); + Task ObjectStream = Task.Factory.StartNew(() => + { + CreateObjectStream(); + }); + } + + private void CreateStringStream() + { + Thread.Sleep(1000); + Console.WriteLine("Creating: StringStream...OK"); + while (Running) + { + + } + } + + private void CreateIntStream() + { + Thread.Sleep(1000); + Console.WriteLine("Creating: IntStream...OK"); + while (Running) + { + + } + } + + private void CreateDecimalStream() + { + Thread.Sleep(1000); + Console.WriteLine("Creating: DecimalStream...OK"); + while (Running) + { + + } + } + + private void CreateObjectStream() + { + Thread.Sleep(1000); + Console.WriteLine("Creating: ObjectStream...OK"); + while (Running) + { + + } + } + } +}