Valhalla Legends Archive

Programming => General Programming => .NET Platform => Topic started by: BaDDBLooD on January 22, 2005, 07:17 PM

Title: Determining If Resolve is Needed?
Post by: BaDDBLooD on January 22, 2005, 07:17 PM


   Public Sub Connect(ByVal Server As String, ByVal Port As Integer)
        Try
            Dim ipHostInfo As IPHostEntry = Dns.Resolve(Server)
            Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
            Dim Destination As New IPEndPoint(ipAddress, Port)
            Socket.BeginConnect(Destination, AddressOf Socket_Connected, Socket)
        Catch Ex As SocketException
            RaiseEvent OnError(Ex.Message)
            Socket.Close()
        End Try
    End Sub



How do you tell if you need to resolve the host, or not.

Eeverytime this procedure is called, my project stops, while it is resolving the host.  I only want it to resolve address's like www.google.com, and not 127.56.191.8

Thanks Alot
Title: Re: Determining If Resolve is Needed?
Post by: UserLoser. on January 22, 2005, 11:20 PM
For DNS lookups, you should create a thread which handles it so it doesn't cause your application to freeze.  This freezing is probably caused by it waiting for a response from whatever DNS server which is all done from the main thread.
Title: Re: Determining If Resolve is Needed?
Post by: BaDDBLooD on January 22, 2005, 11:29 PM
Yeah ok, i can do that.  I still have the problem of Resoving both a Dotted Quad address or whatever ( 255.255.255.255 ) and a Domain name like google.com.  I only want to resolve domain names.
Title: Re: Determining If Resolve is Needed?
Post by: UserLoser. on January 22, 2005, 11:56 PM
Not sure how to go about this in that stuff Microsoft calls .NET, but otherwise you'd be able to use getaddrinfo, getnameinfo, gethostbyname, gethostbyaddr (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/winsock_functions.asp)
Title: Re: Determining If Resolve is Needed?
Post by: l)ragon on January 23, 2005, 12:53 AM
    Private Function ResolveIP(ByVal strServer As String) As IPAddress
        Return Dns.Resolve(strServer).AddressList(0)
    End Function
Perhaps you'll like the shorter way of doing this.
Title: Re: Determining If Resolve is Needed?
Post by: dxoigmn on January 23, 2005, 01:05 AM
Why not just call Connect and let it handle the resolving?


public void Connect(string host, int port)
public System.IAsyncResult BeginConnect(string host, int port, System.AsyncCallback requestCallback, object state)


Note that is probably better that you use the latter so Connect doesn't block but rather fires a callback.
Title: Re: Determining If Resolve is Needed?
Post by: MyndFyre on January 23, 2005, 02:04 AM
Quote from: dxoigmn on January 23, 2005, 01:05 AM
Why not just call Connect and let it handle the resolving?


public void Connect(string host, int port)
public System.IAsyncResult BeginConnect(string host, int port, System.AsyncCallback requestCallback, object state)


Note that is probably better that you use the latter so Connect doesn't block but rather fires a callback.

I don't see those overloads.  The only method I see for Socket::BeginConnect is:

[Visual Basic]
Public Function BeginConnect( _
   ByVal remoteEP As EndPoint, _
   ByVal callback As AsyncCallback, _
   ByVal state As Object _
) As IAsyncResult

[C#]
public IAsyncResult BeginConnect(
   EndPoint remoteEP,
   AsyncCallback callback,
   object state
);

My local installation of the MSDN Library is October 2004; this is .NET 1.1.

For the question -- if it's in dotted-quad notation, IMHO you should still use DNS.Resolve.  It gives you a few perks --
1.) You don't need to try to connect; if DNS doesn't resolve the IP, then you can't connect anyway.
2.) You don't need to try and convert the dotted-quad string into a byte array to create a new IPAddress object.
Title: Re: Determining If Resolve is Needed?
Post by: dxoigmn on January 23, 2005, 03:09 AM
Quote from: MyndFyre on January 23, 2005, 02:04 AM
I don't see those overloads.  The only method I see for Socket::BeginConnect is:

My local installation of the MSDN Library is October 2004; this is .NET 1.1.

I am using .NET 2.0.
Title: Re: Determining If Resolve is Needed?
Post by: BaDDBLooD on January 25, 2005, 04:19 PM
Quote from: MyndFyre on January 23, 2005, 02:04 AM
Quote from: dxoigmn on January 23, 2005, 01:05 AM
Why not just call Connect and let it handle the resolving?


public void Connect(string host, int port)
public System.IAsyncResult BeginConnect(string host, int port, System.AsyncCallback requestCallback, object state)


Note that is probably better that you use the latter so Connect doesn't block but rather fires a callback.

I don't see those overloads.  The only method I see for Socket::BeginConnect is:

[Visual Basic]
Public Function BeginConnect( _
   ByVal remoteEP As EndPoint, _
   ByVal callback As AsyncCallback, _
   ByVal state As Object _
) As IAsyncResult

[C#]
public IAsyncResult BeginConnect(
   EndPoint remoteEP,
   AsyncCallback callback,
   object state
);

My local installation of the MSDN Library is October 2004; this is .NET 1.1.

For the question -- if it's in dotted-quad notation, IMHO you should still use DNS.Resolve.  It gives you a few perks --
1.) You don't need to try to connect; if DNS doesn't resolve the IP, then you can't connect anyway.
2.) You don't need to try and convert the dotted-quad string into a byte array to create a new IPAddress object.

What, exactly, do you mean by statements number one and two.