#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    151
    Rep Power
    19

    c# Dns.GetHostEntry issues


    I'm very new to c# and am trying to figure out what i'm doing wrong here. This is part of a simple PING application. I am getting an
    An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
    error when this line is run
    Code:
    IPHostEntry host = Dns.GetHostEntry(pingHost);
    . In all the examples i've looked at it appears as if i can pass a string into this, what am i doing wrong here?



    Code:
     string pingHost = txtHostName.Text.ToString(); //Get the target to ping     
                IPAddress pingHostResult;
                Ping pingSender = new Ping (); //Create the PING object          
    
                bool ipTest = IPAddress.TryParse(pingHost, out pingHostResult); //test to see if the pingHost value is an IP or host name
    
                if (!ipTest) //if it is not an IP
               
                {                
    
                    IPHostEntry host = Dns.GetHostEntry(pingHost);
    
                    string[] aliases = host.Aliases;
                    IPAddress[] addresses = host.AddressList;
    
    
                    pingHost = addresses.ToString();
                    MessageBox.Show(pingHost);
                }
    Any advice would be appreciated. Remember i've got about 2 hours of c# under my belt so It's probably something obvious.

    DSFX
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,435
    Rep Power
    9645
    According to the documentation, a SocketException will be thrown if there is an error resolving the address.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    151
    Rep Power
    19
    Originally Posted by requinix
    According to the documentation, a SocketException will be thrown if there is an error resolving the address.
    That's bizarre! As a test i was able to ping the target in a cmd window. To simplify things i tried to ping localhost using GetHostAddresses and GetHostEntry and got the same result both times. Thanks for the link to the docs, I've reviewed them more thoroughly and can't find a reason why there would be any issues resolving the address.

    I guess my question is what could possibly interfere with this? I'm going to disable my firewall and antivirus but this is just a wild guess.

    Thanks,

    DSFX
  6. #4
  7. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,435
    Rep Power
    9645
    How about catching the exception and checking what it has to say?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    151
    Rep Power
    19
    My apologies, still getting used to Visual Studios. When i click on "View Detail" in the exception popup it says:

    System.Net.Sockets.SocketException - A non-recoverable error occurred during a database lookup

    Here is some more detail but i don't expect you to sift through this.
    System.Net.Sockets.SocketException was unhandled
    ErrorCode=11003
    HResult=-2147467259
    Message=A non-recoverable error occurred during a database lookup
    NativeErrorCode=11003
    Source=System
    StackTrace:
    at System.Net.Dns.GetAddrInfo(String name)
    at System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)
    at System.Net.Dns.GetHostEntry(String hostNameOrAddress)
    at PING.Form1.btnPing_Click(Object sender, EventArgs e) in \\somedomain.ca\root$\docs\username\Visual Studio 2015\Projects\PING\PING\Form1.cs:line 38
    at System.Windows.Forms.Control.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.ButtonBase.WndProc(Message& m)
    at System.Windows.Forms.Button.WndProc(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
    at System.Windows.Forms.Application.Run(Form mainForm)
    at PING.Program.Main() in \\somedomain.ca\root$\docs\username\Visual Studio 2015\Projects\PING\PING\Program.cs:line 19
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart()
    InnerException:
  10. #6
  11. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,435
    Rep Power
    9645
    You're running this from a network share? Apparently there are permissions issues when doing that, and I'm not sure what all you would need to do to resolve it but I think it involves running Visual Studio as an admin so I wouldn't recommend that.

    Can you run it locally instead? Either copy the .exe to your computer, or even better move the whole project.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    151
    Rep Power
    19
    Voula! That seems to have resolved it. Thank you very much! I've moved all my projects to my D drive. Nothing here is critical so it doesn't matter if it's backed up.

IMN logo majestic logo threadwatch logo seochat tools logo