Error getting the name of the client PC from the server

1

That, it turns out that I have a problem in my production site, that when I publish my code, I get the following error:

  

System.Net.Sockets.SocketException: Unknown host

The reason is because I am trying to get the name of the PC on a client computer. When I try with the following codes, the problem is the same:

String Hostname = System.Environment.MachineName;
String nombrePC = Hostname;

or with

String nombrePC =   System.Net.Dns.GetHostEntry(Request.ServerVariables["remote_addr"]).HostName

On my localhost development team, I have no problem, but when I navigate to the production site, that's what happens.

If anyone has any ideas, I would really appreciate it. I do not know what would be better, configure the server, or change the line of code to get the name of the computer. I preferred to make the correction from the code

    
asked by Danilo 20.12.2017 в 15:54
source

1 answer

1

Try this function at least is what worked for me on my production system, getting the DNS name of the production server:

public string GetClientHostIP() {
        string clientIP = string.Empty;
        string hostName = string.Empty;

        try
        {
            clientIP = Request.UserHostAddress;
            hostName = System.Net.Dns.GetHostEntry(Request.UserHostAddress).HostName;
        }

        catch (Exception)
        {
            //Ip solo para que no de error sino esta registrada la pc en el DNS
            clientIP = "0.0.0.0";
            hostName = "PCError";
        }

        return hostName + " - " + clientIP;

    }

I have invoked the function from where you want to get the name of the team.

    
answered by 20.02.2018 в 21:17