[2005] Socket Host Not Found ?
Hi ,
i'am running an application that grab world news from some news website
some of them work & some not it say Unknown Host
here you my code some mod from MSDN one :
Code:
Private Shared Function ConnectSocket(ByVal server As String, ByVal port As Integer) As Socket
Dim s As Socket = Nothing
Dim hostEntry As IPHostEntry = Nothing
' Get host related information.
''''''''''''''''''
'Here the problem , the exception..
is there anyway without convert the host to ip ?
hostEntry = Dns.GetHostEntry(server)
' Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
' an exception that occurs when the host host IP Address is not compatible with the address family
' (typical in the IPv6 case).
Dim address As IPAddress
For Each address In hostEntry.AddressList
Dim endPoint As New IPEndPoint(address, port)
Dim tempSocket As New Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
tempSocket.Connect(endPoint)
If tempSocket.Connected Then
s = tempSocket
Exit For
End If
Next address
Return s
End Function
' This method requests the home page content for the specified server.
Private Shared Function SocketSendReceive(ByVal server As String, ByVal port As Integer) As String
'Set up variables and String to write to the server.
Dim ascii As Encoding = Encoding.ASCII
Dim request As String = "GET / HTTP/1.1" + ControlChars.Cr + ControlChars.Lf + "Host: " + server + ControlChars.Cr + ControlChars.Lf + "Connection: Close" + ControlChars.Cr + ControlChars.Lf + ControlChars.Cr + ControlChars.Lf
Dim bytesSent As [Byte]() = ascii.GetBytes(request)
Dim bytesReceived(1255) As [Byte]
' Create a socket connection with the specified server and port.
Dim s As Socket = ConnectSocket(server, port)
If s Is Nothing Then
Return "Connection failed"
End If
' Send request to the server.
s.Send(bytesSent, bytesSent.Length, 0)
' Receive the server home page content.
Dim bytes As Int32
' Read the first 256 bytes.
'Dim page As [String] = "Default HTML page on " + server + ":" + ControlChars.Cr + ControlChars.Lf
Dim page As [String] = ControlChars.Cr + ControlChars.Lf
' The following will block until the page is transmitted.
Do
bytes = s.Receive(bytesReceived, bytesReceived.Length, 0)
page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes)
Loop While bytes > 0
Return page
End Function
Private Sub btnnavigate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncheck.Click
Dim host As String = "http://www.breakthrough.tv"
Dim port As Integer = 80
Dim result As String = SocketSendReceive(host, port)
ListBox1.Items.Add(result)
'just showing on listbox to see if it work or not here
End Sub
Thanks :)
Re: [2005] Socket Host Not Found ?
Why do you need to resolve the hostname separately? Why not just use the Connect overload that takes a hostname string argument?
Edit: By the way, what is the value of the endpoint when you get Unknown host?
Re: [2005] Socket Host Not Found ?
Quote:
Originally Posted by Atheist
Why do you need to resolve the hostname separately? Why not just use the Connect overload that takes a hostname string argument?
Edit: By the way, what is the value of the endpoint when you get Unknown host?
yes Atheist i would like to connect to the website directly like with Webclient
but this only example i found on MSDN :)
Re: [2005] Socket Host Not Found ?
i agree with atheist about just use overload.
it is not unusual for Dns.GetHostEntry(server) to fail. without actually seeing what is in server it is hard to tell why it failed. it should be in a try catch block though.
Re: [2005] Socket Host Not Found ?
Quote:
Originally Posted by dbasnett
i agree with atheist about just use overload.
do you have a simple example ?
Re: [2005] Socket Host Not Found ?
aSocket.Connect("www.yahoo.com", 80)
Re: [2005] Socket Host Not Found ?
Re: [2005] Socket Host Not Found ?
What do you mean by "anyone"? dbasnett has given you the example you where asking for.
Re: [2005] Socket Host Not Found ?
Quote:
Originally Posted by Atheist
What do you mean by "anyone"? dbasnett has given you the example you where asking for.
sorry to say but this line doesnt helped me at all :blush:
Re: [2005] Socket Host Not Found ?
Well then you'll have to express yourself better. You asked for a simple example on the use of the connect overload that took a hostname argument, and thats what dbasnett provided. If you need more help, just say so.
Re: [2005] Socket Host Not Found ?
Yes i'am new to Socket
i searched for some simple example on MSDN & is the one i posted ,but this one have to convert the host to ip ,i just need to connect directly with website name to read webpage source .dbasnett posted this line of code which doesnt helped me really .i just need simple example & after i can elaborate it
Thanks
Re: [2005] Socket Host Not Found ?
Atheist can you help me ? :rolleyes:
Re: [2005] Socket Host Not Found ?
Code:
Dim IPhe As IPHostEntry
Debug.WriteLine("")
Try
IPhe = System.Net.Dns.GetHostEntry("www.microsoft.com") 'lookup address
Debug.WriteLine(IPhe.HostName)
Debug.WriteLine(IPhe.AddressList(0).ToString)
Catch ex As Exception
Debug.WriteLine("MS " & ex.Message)
End Try
Stop
Re: [2005] Socket Host Not Found ?
Quote:
Originally Posted by dbasnett
Code:
Dim IPhe As IPHostEntry
Debug.WriteLine("")
Try
IPhe = System.Net.Dns.GetHostEntry("www.microsoft.com") 'lookup address
Debug.WriteLine(IPhe.HostName)
Debug.WriteLine(IPhe.AddressList(0).ToString)
Catch ex As Exception
Debug.WriteLine("MS " & ex.Message)
End Try
Stop
Hi dbasnett ,
Try this
Code:
Dim IPhe As IPHostEntry
Try
IPhe = System.Net.Dns.GetHostEntry("http://www.gtcc.org/") 'lookup address
ListBox1.Items.Add(IPhe.HostName)
ListBox1.Items.Add(IPhe.AddressList(0).ToString)
Catch ex As Exception
MsgBox("MS " & ex.Message)
End Try
i'am lost with Socket , i just need to a simple example how to read a webpage source code via Socket
can you help me ?
Re: [2005] Socket Host Not Found ?
this "http://www.gtcc.org/" is not a FQDN. it is a web request TO an address
query this www.gtcc.org
and then add http://
in your original post you said
hostEntry = Dns.GetHostEntry(server)
if you had shown it like this
'server = http://www.gtcc.org/
hostEntry = Dns.GetHostEntry(server)
your problem would have been resolved.
we had no choice but to assume that you knew the difference between an address and a request.
Re: [2005] Socket Host Not Found ?
Thank you both for your help but i really really didnt get to work with Socket :mad:
That socket is really Hard & getting me insane :mad:
i'am just in need for simple code how get webpage source using Socket
is that so hard to be done ? :rolleyes:
i check in the forum here , microsoft Forum , MSDN & others but no result
seem that socket very hard... :rolleyes:
anyway Thank you for the help , i should stop searching how Socket work for now
hope next time i will be ready for it :)
Re: [2005] Socket Host Not Found ?
if you are doing http why not use the WebBrowser control?
Re: [2005] Socket Host Not Found ?
Quote:
Originally Posted by dbasnett
if you are doing http why not use the WebBrowser control?
i need socket :)
have you ever tried to get webpage source via Socket ?
Re: [2005] Socket Host Not Found ?
everything you wanted to know about http but were afraid to ask
http://www.faqs.org/rfcs/rfc2616.html
on the other hand
the DocumentText of the webbrowser control might be what you are looking for.