2

Recently, a strange bug has popped up with some clients of ours. After some deep diving into the logs provided by unity's developer console build, I've come across the following error: SocketException: Could not resolve host 'ן§¹ב· ו·©'

I've traced this error back to this line of code:

host = Dns.GetHostEntry(Dns.GetHostName());

where host is an IPHostEntry object. This is essentially just there to get the clients IP eventually, but was written by a previous programmer that no longer works here.

I'm not 100% sure what to make of this host name in the error log. From what I know, Dns.GetHostName() essentially returns your machine name as the DNS recognizes it, but it looks like for this specific client it returns some sort of gibberish.

We're from Israel, so is it possible the clients machine is named in Hebrew and thus the characters can't be resolved by Dns.GetHostEntry()?

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • That looks like a text-encoding issue. I'd look into what format the ```GetHostEntry()``` expects. – Immersive Oct 07 '20 at 06:16
  • There's nothing strange about that code, nothing that needs help from the programmer that wrote this. If you check the docs for [Dns.GetHostName()](https://learn.microsoft.com/en-us/dotnet/api/system.net.dns.gethostname?view=netcore-3.1) and [GetHostEntry](https://learn.microsoft.com/en-us/dotnet/api/system.net.dns.gethostentry?view=netcore-3.1) you'll see what those methods do - those two calls try to retrieve the DNS entry for the local machine. Split the two calls in separate lines - what does `Dns.GetHostName()` return? If you use that name with `nslookup` would you get a result? – Panagiotis Kanavos Oct 07 '20 at 06:19
  • 1
    @Immersive Unicode, just like all .NET strings. That's a [BCL method](https://learn.microsoft.com/en-us/dotnet/api/system.net.dns.gethostname?view=netcore-3.1) returning the local machine's DNS entry, not custom code – Panagiotis Kanavos Oct 07 '20 at 06:20
  • This question shouldn'te be closed - the problem is clear and has a valid answer – Panagiotis Kanavos Oct 07 '20 at 06:36
  • I can't post the answer I was writing until the question is reopened. While DNS technically allows Unicode, it only uses a *subset* of ASCII. Unicode names have to be encoded using Punycode. You can do that in .NET with the [IdnMapping.GetAscii](https://learn.microsoft.com/en-us/dotnet/api/system.globalization.idnmapping?view=netcore-3.1) method – Panagiotis Kanavos Oct 07 '20 at 06:38
  • BTW using non-English machine names is *very rare*. IT people definitely don't do this - they don't even allow a user's name if they can help it. Saves *a lot* of time when that machine is given to someone else. I bet that machine is some manager's machine? Someone they couldn't force to use eg `Manager34-PC` as his/her machine name? – Panagiotis Kanavos Oct 07 '20 at 06:43
  • Possible duplicates [How to convert IDN to ASCII](https://stackoverflow.com/questions/852274/how-to-convert-idn-to-ascii), [Cannot read Unicode URL in C#](https://stackoverflow.com/questions/12381578/can-not-read-unicode-url-in-c-sharp) . Similar [A hostname with Unicode characters is valid in Windows 8](https://stackoverflow.com/questions/14075023/a-hostname-with-unicode-characters-is-valid-in-windows-8/14075108#14075108) – Panagiotis Kanavos Oct 07 '20 at 06:50
  • To solve your *actual problem*, retrieving the local machine's IP addresses, a far better solution [is to retrieve the ip configuration](https://stackoverflow.com/a/24814027/134204) through [NetworkInterface.GetAllNetworkInterfaces()](https://learn.microsoft.com/en-us/dotnet/api/system.net.networkinformation.networkinterface.getallnetworkinterfaces?view=netcore-3.1). No need to connect to a DNS server, no DNS issues. You'll notice the other answers use `Dns.GetHostEntry(Dns.GetHostName())`, which failed in your case – Panagiotis Kanavos Oct 07 '20 at 06:55
  • @PanagiotisKanavos Actually no, this specific machine is a personal PC the client uses at his workplace. The thing is, he has our software both at home and at work, but he is experiencing this issue only in his work PC, hence why I'm curious what could be the difference that's causing this mess in the first place. I'll take a look at the NetworkInterface class, I'm still kinda new to the networking scene so I didn't look for alternative ways to get IP addresses. Thank you for all the information. – The Manly Fairy Oct 07 '20 at 06:58
  • And I've been doing this for 20 years but haven't encountered this yet. Unless you're a networking guy you wouldn't know the problems caused by DNS, routing etc. In your case `NetworkingInterfaces` is even more important as you can't know what DNS is used with residential machines - the home router's? The mobile telco's? The cafe's router? – Panagiotis Kanavos Oct 07 '20 at 07:04

0 Answers0