From 774983238b01fbb772a5e73d6b121216f08cd215 Mon Sep 17 00:00:00 2001 From: sin365 <353374337@qq.com> Date: Fri, 2 Jun 2023 18:24:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E6=94=BE=EF=BC=8C=E7=AB=AF=E5=8F=A3?= =?UTF-8?q?=E9=87=8D=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NetworkHelperCore.cs | 6 ++++++ .../HaoYueNet.ClientNetworkNet4x.csproj | 3 --- .../NetworkHelperCore.cs | 4 ++++ .../NetWork/SocketManager.cs | 18 +++++++++++++----- Simple/Client-Cli/Program.cs | 11 +++++++++++ 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore.cs b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore.cs index 7ae0a6f..849bc52 100644 --- a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore.cs +++ b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore.cs @@ -1,5 +1,6 @@ using Google.Protobuf; using HunterProtobufCore; +using System.Net; using System.Net.Sockets; namespace HaoYueNet.ClientNetwork @@ -342,5 +343,10 @@ namespace HaoYueNet.ClientNetwork //Console.WriteLine(Msg); OnLogOut(Msg); } + + public Socket GetClientSocket() + { + return client; + } } } diff --git a/NetLib/HaoYueNet.ClientNetworkNet4x/HaoYueNet.ClientNetworkNet4x.csproj b/NetLib/HaoYueNet.ClientNetworkNet4x/HaoYueNet.ClientNetworkNet4x.csproj index d0e9994..056b04a 100644 --- a/NetLib/HaoYueNet.ClientNetworkNet4x/HaoYueNet.ClientNetworkNet4x.csproj +++ b/NetLib/HaoYueNet.ClientNetworkNet4x/HaoYueNet.ClientNetworkNet4x.csproj @@ -62,9 +62,6 @@ - - - diff --git a/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperCore.cs b/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperCore.cs index b258f9f..a7caf3e 100644 --- a/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperCore.cs +++ b/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperCore.cs @@ -346,5 +346,9 @@ namespace HaoYueNet.ClientNetworkNet4x //Console.WriteLine(Msg); OnLogOut(Msg); } + public Socket GetClientSocket() + { + return client; + } } } diff --git a/NetLib/HaoYueNet.ServerNetwork/NetWork/SocketManager.cs b/NetLib/HaoYueNet.ServerNetwork/NetWork/SocketManager.cs index e0d21dd..55f7a65 100644 --- a/NetLib/HaoYueNet.ServerNetwork/NetWork/SocketManager.cs +++ b/NetLib/HaoYueNet.ServerNetwork/NetWork/SocketManager.cs @@ -156,16 +156,24 @@ namespace HaoYueNet.ServerNetwork } - /// - /// 启动服务 - /// - /// - public bool Start(IPEndPoint localEndPoint) + /// + /// 启动服务 + /// + /// + /// 是否端口重用 + /// + public bool Start(IPEndPoint localEndPoint,bool bReuseAddress = false) { try { m_clients.Clear(); listenSocket = new Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); + + if (bReuseAddress) + { + listenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); + } + listenSocket.Bind(localEndPoint); // start the server with a listen backlog of 100 connections listenSocket.Listen(m_maxConnectNum); diff --git a/Simple/Client-Cli/Program.cs b/Simple/Client-Cli/Program.cs index c0dd0de..e89f01a 100644 --- a/Simple/Client-Cli/Program.cs +++ b/Simple/Client-Cli/Program.cs @@ -1,5 +1,7 @@ using ClientCore; using ClientCore.Event; +using System.Net; +using System.Net.Sockets; App.Init("127.0.0.1", 23846); @@ -31,6 +33,15 @@ while (true) } App.chat.SendChatMsg(CmdArr[1]); break; + case "socket": + { + Socket socket = App.networkHelper.GetClientSocket(); + if (socket == null) + return; + IPEndPoint endpoint = ((IPEndPoint)socket.LocalEndPoint); + Console.WriteLine($"LocalEndPoint IP->{endpoint.Address.ToString()} Port->{endpoint.Port}"); + } + break; default: Console.WriteLine("未知命令" + CommandStr); break;