Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between ListenAnyIP and ListenLocalhost in Kestrel

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

What is the difference between ListenAnyIP and ListenLocalhost in Kestrel? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Local Loopback address (Loopback Address):

Baidu's definition, 127.0.0.1, is often referred to as a local loopback address (Loopback Address) and does not belong to any classful address class. It represents the local virtual interface of the device, so it is considered by default to be an interface that will never go down. There is a similar definition in the Windows operating system, so it is usually possible to ping this local loopback address before installing the network card. It is generally used to check whether the local network protocol and basic data interface are normal.

The form of the local loopback address of IPv6 is: 0 windows 0, 0 7, 0 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0

Ip address Typ

Public address

The public address (Public address) is the responsibility of Inter NIC (Internet Network Information Center Internet Information Center). These IP addresses are assigned to organizations that register and apply to Inter NIC. Access the Internet directly through it.

Private address

Private addresses (Private address) are unregistered addresses and are intended for use within the organization. The internal private addresses reserved are listed below

Category A 10.0.0.0Mutual 10.255.255.255

Category B 172.16.0.0Mutual 172.31.255.255

Class C 192.168.0.0While 192.168.255.255

IPv6 [:] (0000 IPv4 0.0.0.0 meaning:

Wikipedia explains that invalid, unknown, unavailable targets

In the server, it is often used to listen for all ip addresses on the local machine. Generally, when we bind the port on the server side, we can choose to bind to 0.0.0.0, so that we can access my service through multiple ip addresses.

The difference between ListenLocalhost and ListenAnyIP

There are three ways to configure the Kestrel listening port through coding to implement ListenLocalhost, ListenAnyIP and Listen, in which ListenLocalhost is equivalent to Listen's IPAddress.IPv6Loopback and IPAddress.Loopback,ListenAnyIP is equivalent to Listen's IPAddress.IPv6Any and IPAddress.Any. Let me see if I can see their source code.

The source code of ListenLocalhost and ListenAnyIP methods

/ / /

/ Listens on:: 1 and 127.0.0.1 with the given port. Requesting a dynamic port by specifying 0 is not supported

/ for this type of endpoint.

/ / /

Public void ListenLocalhost (int port, Action configure)

{

If (configure = = null)

{

Throw new ArgumentNullException (nameof (configure))

}

Var listenOptions = new LocalhostListenOptions (port)

ApplyEndpointDefaults (listenOptions)

Configure (listenOptions)

ListenOptions.Add (listenOptions)

}

/ / /

/ Listens on all IPs using IPv6 [::], or IPv4 0.0.0.0 if IPv6 is not supported.

/ / /

Public void ListenAnyIP (int port, Action configure)

{

If (configure = = null)

{

Throw new ArgumentNullException (nameof (configure))

}

Var listenOptions = new AnyIPListenOptions (port)

ApplyEndpointDefaults (listenOptions)

Configure (listenOptions)

ListenOptions.Add (listenOptions)

}

Through the source code, we can find that the difference between them is that they are different in constructing listenopthons objects, using LocalhostListenOptions and AnyIPListenOptions for new to create instances, while AnyIPListenOptions and LocalhostListenOptions both inherit the class ListenOptions and override the BindAsync method. The source code is as follows

Internal sealed class LocalhostListenOptions: ListenOptions

{

Internal LocalhostListenOptions (int port)

: base (new IPEndPoint (IPAddress.Loopback, port))

{

If (port = = 0)

{

Throw new InvalidOperationException (CoreStrings.DynamicPortOnLocalhostNotSupported)

}

}

/ / bind loopback address ipv4 is 127.0.0.1, iPV6 is:: 1

Internal override async Task BindAsync (AddressBindContext context)

{

Var exceptions = new List ()

Try

{

Var v4Options = Clone (IPAddress.Loopback)

Await AddressBinder.BindEndpointAsync (v4Options, context) .ConfigureAwait (false)

}

Catch (Exception ex) when (! (ex is IOException)

{

Context.Logger.LogWarning (0, CoreStrings.NetworkInterfaceBindingFailed, GetDisplayName (), "IPv4 loopback", ex.Message)

Exceptions.Add (ex)

}

Try

{

Var v6Options = Clone (IPAddress.IPv6Loopback)

Await AddressBinder.BindEndpointAsync (v6Options, context) .ConfigureAwait (false)

}

Catch (Exception ex) when (! (ex is IOException)

{

Context.Logger.LogWarning (0, CoreStrings.NetworkInterfaceBindingFailed, GetDisplayName (), "IPv6 loopback", ex.Message)

Exceptions.Add (ex)

}

If (exceptions.Count = = 2)

{

Throw new IOException (CoreStrings.FormatAddressBindingFailed (GetDisplayName ()), new AggregateException (exceptions))

}

/ / If StartLocalhost doesn't throw, there is at least one listener.

/ / The port cannot change for "localhost".

Context.Addresses.Add (GetDisplayName ())

}

}

Internal sealed class AnyIPListenOptions: ListenOptions

{

Internal AnyIPListenOptions (int port)

: base (new IPEndPoint (IPAddress.IPv6Any, port))

{

}

/ / bind IPv6 if the native does not support ipv4

Internal override async Task BindAsync (AddressBindContext context)

{

/ / when address is' http://hostname:port', 'http://*:port', or' http://+:port'

Try

{

Await base.BindAsync (context) .ConfigureAwait (false)

}

Catch (Exception ex) when (! (ex is IOException)

{

Context.Logger.LogDebug (CoreStrings.FormatFallbackToIPv4Any (IPEndPoint.Port))

/ / for machines that do not support IPv6

EndPoint = new IPEndPoint (IPAddress.Any, IPEndPoint.Port)

Await base.BindAsync (context) .ConfigureAwait (false)

}

}

}

Summary: according to the above analysis, it is recommended to use IPAddress.Any when binding ports, which can support ipv6 and ipv4 addresses.

WebBuilder.ConfigureKestrel (options = >

{

/ / 1.ListenLocalhost method

/ / options.ListenLocalhost (8081)

/ / 2.ListenAnyIP method

Options.ListenAnyIP (8081)

/ / 3.Listen method

/ / options.Listen (IPAddress.Loopback, 8081)

/ / Setup a HTTP/2 endpoint without TLS.

Options.ListenAnyIP (18081, o = > o.Protocols =

HttpProtocols.Http1AndHttp2)

});

After reading the above, have you mastered the difference between ListenAnyIP and ListenLocalhost in Kestrel? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report