开放,端口重用

This commit is contained in:
sin365 2023-06-02 18:24:54 +08:00
parent 21b02018ee
commit 774983238b
5 changed files with 34 additions and 8 deletions

View File

@ -1,5 +1,6 @@
using Google.Protobuf; using Google.Protobuf;
using HunterProtobufCore; using HunterProtobufCore;
using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
namespace HaoYueNet.ClientNetwork namespace HaoYueNet.ClientNetwork
@ -342,5 +343,10 @@ namespace HaoYueNet.ClientNetwork
//Console.WriteLine(Msg); //Console.WriteLine(Msg);
OnLogOut(Msg); OnLogOut(Msg);
} }
public Socket GetClientSocket()
{
return client;
}
} }
} }

View File

@ -62,9 +62,6 @@
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ProtobufHunterNetCore.cs" /> <Compile Include="ProtobufHunterNetCore.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Content Include="protobuf-net.dll" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="app.config" /> <None Include="app.config" />
<None Include="packages.config" /> <None Include="packages.config" />

View File

@ -346,5 +346,9 @@ namespace HaoYueNet.ClientNetworkNet4x
//Console.WriteLine(Msg); //Console.WriteLine(Msg);
OnLogOut(Msg); OnLogOut(Msg);
} }
public Socket GetClientSocket()
{
return client;
}
} }
} }

View File

@ -156,16 +156,24 @@ namespace HaoYueNet.ServerNetwork
} }
/// <summary> /// <summary>
/// 启动服务 /// 启动服务
/// </summary> /// </summary>
/// <param name="localEndPoint"></param> /// <param name="localEndPoint"></param>
public bool Start(IPEndPoint localEndPoint) /// <param name="bReuseAddress">是否端口重用</param>
/// <returns></returns>
public bool Start(IPEndPoint localEndPoint,bool bReuseAddress = false)
{ {
try try
{ {
m_clients.Clear(); m_clients.Clear();
listenSocket = new Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); listenSocket = new Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
if (bReuseAddress)
{
listenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
}
listenSocket.Bind(localEndPoint); listenSocket.Bind(localEndPoint);
// start the server with a listen backlog of 100 connections // start the server with a listen backlog of 100 connections
listenSocket.Listen(m_maxConnectNum); listenSocket.Listen(m_maxConnectNum);

View File

@ -1,5 +1,7 @@
using ClientCore; using ClientCore;
using ClientCore.Event; using ClientCore.Event;
using System.Net;
using System.Net.Sockets;
App.Init("127.0.0.1", 23846); App.Init("127.0.0.1", 23846);
@ -31,6 +33,15 @@ while (true)
} }
App.chat.SendChatMsg(CmdArr[1]); App.chat.SendChatMsg(CmdArr[1]);
break; 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: default:
Console.WriteLine("未知命令" + CommandStr); Console.WriteLine("未知命令" + CommandStr);
break; break;