Results 1 to 19 of 19

Thread: [2005] Socket Host Not Found ?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    [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

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    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

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    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 ?

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2005] Socket Host Not Found ?

    aSocket.Connect("www.yahoo.com", 80)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Re: [2005] Socket Host Not Found ?

    anyone ?

  8. #8
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Socket Host Not Found ?

    What do you mean by "anyone"? dbasnett has given you the example you where asking for.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    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

  10. #10
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    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

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Re: [2005] Socket Host Not Found ?

    Atheist can you help me ?

  13. #13
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    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 ?

  15. #15
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    Last edited by dbasnett; Nov 14th, 2008 at 01:36 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Re: [2005] Socket Host Not Found ?

    Thank you both for your help but i really really didnt get to work with Socket
    That socket is really Hard & getting me insane

    i'am just in need for simple code how get webpage source using Socket
    is that so hard to be done ?

    i check in the forum here , microsoft Forum , MSDN & others but no result
    seem that socket very hard...

    anyway Thank you for the help , i should stop searching how Socket work for now
    hope next time i will be ready for it

  17. #17
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2005] Socket Host Not Found ?

    if you are doing http why not use the WebBrowser control?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    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 ?

  19. #19
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    Last edited by dbasnett; Nov 14th, 2008 at 02:22 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width