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
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.
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.
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)
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.
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.
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.
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.
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.