BindlLocal

main
Kevin Li 2023-09-01 20:20:57 +08:00
parent add66ce981
commit 065c76af4e
1 changed files with 39 additions and 24 deletions

View File

@ -44,10 +44,24 @@ namespace dotNetty_kcp
public System.Threading.Tasks.Task<IChannel> BindLocal() public System.Threading.Tasks.Task<IChannel> BindLocal()
{ {
var localAddress = new IPEndPoint(IPAddress.Any, 0); //var localAddress = new IPEndPoint(IPAddress.Any, 0);
var localAddress = new IPEndPoint(GetLocalIPAddress(), 0);
return bootstrap.BindAsync(localAddress); return bootstrap.BindAsync(localAddress);
} }
public static string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new Exception("No network adapters with an IPv4 address in the system!");
}
public void init(ChannelConfig channelConfig, ExecutorPool executorPool, IEventLoopGroup eventLoopGroup) public void init(ChannelConfig channelConfig, ExecutorPool executorPool, IEventLoopGroup eventLoopGroup)
{ {
if (channelConfig.UseConvChannel) if (channelConfig.UseConvChannel)
@ -101,7 +115,8 @@ namespace dotNetty_kcp
* 4Gwifi使 * 4Gwifi使
* @param ukcp * @param ukcp
*/ */
public void reconnect(Ukcp ukcp){ public void reconnect(Ukcp ukcp)
{
if (!(_channelManager is ServerConvChannelManager)) if (!(_channelManager is ServerConvChannelManager))
{ {
throw new Exception("reconnect can only be used in convChannel"); throw new Exception("reconnect can only be used in convChannel");