开放,端口重用

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 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;
}
}
}

View File

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

View File

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

View File

@ -156,16 +156,24 @@ namespace HaoYueNet.ServerNetwork
}
/// <summary>
/// 启动服务
/// </summary>
/// <param name="localEndPoint"></param>
public bool Start(IPEndPoint localEndPoint)
/// <summary>
/// 启动服务
/// </summary>
/// <param name="localEndPoint"></param>
/// <param name="bReuseAddress">是否端口重用</param>
/// <returns></returns>
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);

View File

@ -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;