none
How to Get PC Name or IP Address?

    Question

  • Hello, all

    Does Silverlight have class to support to get local PC-Name or IP Address?

    I want to do one app which could see the server name or ip from client side. Any suggestions?

    Thanks...

    Thursday, November 12, 2009 12:51 AM

Answers

  • Hi,
    Using any of the below u will get what u require


    1.HttpContext.Current.Server.MachineName;

    2.System.Net.Dns.GetHostByAddress(System.Net.IPAddress.Parse(System.Web.HttpContext.Current.Request.UserHostName)).HostName;

     now as far as sending it to client is concern you can send it eigher way ot initParams or web services as u wish...

    Thursday, November 12, 2009 11:13 PM
  •  you can get server ip address by following code in silverlight itself ( Client side code).

     string serverIP = Application.Current.Host.Source.DnsSafeHost.ToString(); //address of the machine where you have hosted ur application

     Hence no need to pass initparm or no need to pass server ip address from server to client by service call.

    Avtar

    Friday, November 13, 2009 12:13 AM

All replies

  •  Hi Spider,

     There is no direct support to get client ip address in silverlight.

    Now there are two approach to get it.

    one is Javascript and another is use defualt.aspx.cs file page load event.

    Using java script you can get ip address and you can pass it to silverlight.

    For silverlight and java script communication you can refer to below link

    http://weblogs.asp.net/albertpascual/archive/2008/08/13/javascript-communication-to-silverlight-2-0.aspx

     But I wouldn't suggest this apporach because of its heavily  dependency on browser.

     I would suggest you to use defualt.aspx.cs to get client ip address and pass it to silverlight using initparam

    here are step...

     

    Go to Default.aspx.cs and add the following code in Page_load event.

    Silverlight1.InitParameters = "IpAddress=" +Request.UserHostAddress or Request.UserHostName
     
    Now you can pass this value to app.xaml.cs Application_startup event using initparm.
    you can use following link for your reference for understanding how to pass value to silverlight from defualt.aspx.cs using initparam.
     
    http://laumania.net/post/Dynamically-changing-urlendpoint-for-WCF-service-in-Silverlight-20.aspx
     

    Avtar
    Thursday, November 12, 2009 1:30 AM
  • Oh, sorry, may be my confused expression.

    I don not want the client ip address.

    oppositely, I want to get server ip which is displayed in client side.

    For example

    My server IP is 127.0.0.1 and Name is Server_PC

    Client IP? of couse i don;t know it.

    And i want it (127.0.0.1 Server_PC) to be displayed in client side

    Any suggestions? 

    Thanks for all your advance.

    Thursday, November 12, 2009 8:20 PM
  • Hi,
    Using any of the below u will get what u require


    1.HttpContext.Current.Server.MachineName;

    2.System.Net.Dns.GetHostByAddress(System.Net.IPAddress.Parse(System.Web.HttpContext.Current.Request.UserHostName)).HostName;

     now as far as sending it to client is concern you can send it eigher way ot initParams or web services as u wish...

    Thursday, November 12, 2009 11:13 PM
  •  you can get server ip address by following code in silverlight itself ( Client side code).

     string serverIP = Application.Current.Host.Source.DnsSafeHost.ToString(); //address of the machine where you have hosted ur application

     Hence no need to pass initparm or no need to pass server ip address from server to client by service call.

    Avtar

    Friday, November 13, 2009 12:13 AM
  • Hi, Bhagirath

    Thanks for your reply.

    So I can;t get HostName from Silverlight Apl directly.

    And have to get it from Http and send it to my silverlight app?


    Friday, November 13, 2009 12:16 AM
  • Hi , avtar

    Thanks for your reply.

    Now I can get the IP Address using your solution.

    But how to get the Server Name in silverlight?

    Friday, November 13, 2009 2:06 AM
  •  You can get host name by following code in silverlight

    string hostName = Application.Current.Host.Source.Host;
     
    Avtar 
    Friday, November 13, 2009 2:42 AM
  •  Hi spider,

    Application.Current.Host.Source gives you the Uri instance

    Now you can investigate this Uri and get whatever is required in your applicaiton.

    Avtar

     

     

     

    Friday, November 13, 2009 2:44 AM
  • Hi Spider,

    As far as I know, there is no direct access to get the Server IP in Silverlight. You have to depend on the InitParams for doing so.

    But you can easily get the Server name using either "Application.Current.Host.Source.DnsSafeHost" or "Application.Current.Host.Source.Host".

    Friday, November 13, 2009 7:39 AM