Skip to main content

Local IP Address

Here is a usefull block of code to retrieve a host IPAddress:

private static class Network

{

#region DNS

public static IPAddress FindIPAddress(bool localPreference)

{

return FindIPAddress(Dns.GetHostEntry(Dns.GetHostName()),

localPreference);

}

public static IPAddress FindIPAddress(IPHostEntry host, bool

localPreference)

{

if (host == null)

throw new ArgumentNullException("host");

if (host.AddressList.Length == 1)

return host.AddressList[0];

else

{

foreach (System.Net.IPAddress address in host.AddressList)

{

bool local = IsLocal(address);

if (local && localPreference)

return address;

else if (!local && !localPreference)

return address;

}

return host.AddressList[0];

}

}

public static bool IsLocal(IPAddress address)

{

return address.AddressFamily.ToString() == "InterNetwork";

}


Comments

Popular posts from this blog

Understanding Monte Carlo Simulation C#

This method has been introduced to resolve numerically complex physics problems, such as neutron diffusion, that were to complex for an analytical solution. This method is based on generating random values for parameters or inputs to explore the behaviour of complex systems, now this method is used in various domains like: Engineering, science and finance. These approaches tend to follow a particular pattern: 1- Define a domain of possible inputs. 2- Generate inputs randomly from the domain using a certain specified probability distribution. 3- Perform a deterministic computation using the inputs. 4- Aggregate the results of the individual computations into the final result. Here is an example of using Monte Carlo simulation to approximate the value of Pi: In this case we have 2 parameters x,y which defines a location in the plane (e.g The picture on the left). We will calculate the probability to have a point in the 1/4 Circle area, with a radius of 1. To calculate Pi we...

Full Text Search using Entity Framework

I've been working on a project where I needed to implement full time search on one table. My current solution was based on SQL Server db and Entity Framework 6. I had two choices implement the full text search in C# or use the functionality available in SQL server, since the data was stored in SQL Server the obvious solution was to use the built in full text search. How this works: 1. You need to activate and configure the full text search: Activate on the sql server table by using SSMS, and specify which columns are going to be included. 2. To perform a full text search in a T-SQL query you have the choice between 2 Boolean functions: Contains and Freetext or two functions that returns 2 columns tables. In my case I need a function that could be used in a where clause (Boolean), and decided to use 'Contains'. For more details about the difference between Freetext and contains have a look at this article . 3. I need to instruct EF6 to generate a particular T-SQL stateme...

Preparing for Azure Development Certification (70-532)

I've decided to get certified in AZURE, I'm aiming to get the MCSD certification which requires to pass 3 Exams: Developing Microsoft Azure Solutions 70-532 Implementing Microsoft Azure Infrastructure Solutions 70-533 Architecting Microsoft Azure Solutions 70-534 You can take the exams in the order you choose, in my case I've decided to take them in the order above. My strategy: I started by buying the recommended training books:                         After reading through the first book, I decided to take some mock tests, I quickly realised that the books are not sufficient :). Especially for 70-532. Therefor I decided to more studying online especially on the PowerShell cmdlets available to manage Azure Resources. As part of this effort I'm going to maintain in this blog a list of post for each set of PowerShell scripts, it is going to be a way to memorize it for me, and a way to ha...