Whois programm

Hi,

I am making a whois programm.
I have the following function:
    Public Function SocketSendReceive(ByVal server As String, ByVal request As String, ByVal port As Integer) As String
        'Set up variables and String to write to the server.
        Dim ascii As System.Text.Encoding = System.Text.Encoding.ASCII
        Dim bytesSent As [Byte]() = ascii.GetBytes(request)
        Dim bytesReceived(255) As [Byte]

        ' Create a socket connection with the specified server and port.
        Dim s As System.Net.Sockets.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] = ControlChars.Cr + ControlChars.Lf

        ' The following will block until the page is transmitted.
        Do
            bytes = s.Receive(bytesReceived, bytesReceived.Length, 0)
            page = page + System.Text.Encoding.ASCII.GetString(bytesReceived, 0, bytes)
        Loop While bytes > 0

        Return page
    End Function

And this is how I call the function: socket.SocketSendReceive("whois.crsnic.net", "google.com", 43)

But the programm crashes every time.

Does someone know what I am doing wrong

Greetings,
Robin


Answer this question

Whois programm

  • Jawad Munir

     

    ACk.....

     

    Certainly you need a timeout or it's always going to loop.  I'd recomend something like this: (typing from memory and no compling)

    Public Class RobinvasForm

    Protectect bTimeOut as boolean = false

     

    Protected Friend with events Timer1 as new Timer

    Public Sub Load_RobinvasForm()

    Timer1.interval = 40*1000
    timer 1.enabled=false

    end sub

    Private Sub Timer1_Ticks(sender, arg) handles Timer1_OnTick

    bTimeOut = true

    end sub

    Your Loop:

     

    bTimeout=false :Timer1.enabled = true: timer1.start()

     Do
                bytes = s.Receive(bytesReceived, bytesReceived.Length, 0)
                page = page + System.Text.Encoding.ASCII.GetString(bytesReceived, 0, bytes)

      system.threading.threads.sleep(100)
     Loop While bytes > 0 and not bTimeout

    bTimeout=false : Timer1.enabled = true : timer1.stop()


    I've done some coding in this area and I've never had to resort to this. I don't know what web IO class you are calling but please see if it does have a timeout and a way to check counts before you comitt yourself to a loop like this.

     

     

     

     



  • rosin

    I debugged it a little and it comes down to this:
    When I run de code, the programm keeps on looping this:
            Do
                bytes = s.Receive(bytesReceived, bytesReceived.Length, 0)
                page = page + System.Text.Encoding.ASCII.GetString(bytesReceived, 0, bytes)
            Loop While bytes > 0
    So that means that the server doesn't return any data.
    How can I resolve this

  • TallMike

    Well, when I run the code, my mouse cursor changes after a while in a wait icon.
    So it doens't return an error.

    Edit: When I'm trying to connect to the .nl whois server it does work, only not for the other TLD's.

  • CBDenver

    robinva,

     

    Where is the error and what is it



  • JurgenJ

    I have implented your code in my programm, but I get an error on this one: Protected Friend with events Timer1 as new Timer
    Error: 'New' is not valid in this context.

    My complete socket class:
    Public Class GetSocket2

        Public Function ConnectSocket(ByVal server As String, ByVal port As Integer) As System.Net.Sockets.Socket
            Dim s As System.Net.Sockets.Socket = Nothing
            Dim hostEntry As System.Net.IPHostEntry = Nothing

            ' Get host related information.
            hostEntry = System.Net.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 System.Net.IPAddress

            For Each address In hostEntry.AddressList
                Dim endPoint As New System.Net.IPEndPoint(address, port)
                Dim tempSocket As New System.Net.Sockets.Socket(endPoint.AddressFamily, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.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.

        Public Function SocketSendReceive(ByVal server As String, ByVal request As String, ByVal port As Integer) As String
            'Set up variables and String to write to the server.
            Dim ascii As System.Text.Encoding = System.Text.Encoding.ASCII
            Dim bytesSent As [Byte]() = ascii.GetBytes(request)
            Dim bytesReceived(255) As [Byte]

            ' Create a socket connection with the specified server and port.
            Dim s As System.Net.Sockets.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] = ControlChars.Cr + ControlChars.Lf

            ' The following will block until the page is transmitted.
            Form1.bTimeOut = False : Form1.Timer1.Enabled = True : Form1.Timer1.Start()

            Do
                bytes = s.Receive(bytesReceived, bytesReceived.Length, 0)
                page = page + System.Text.Encoding.ASCII.GetString(bytesReceived, 0, bytes)

                Threading.Thread.Sleep(100)
            Loop While bytes > 0 And Not Form1.bTimeOut

            Form1.bTimeOut = False : Form1.Timer1.Enabled = True : Form1.Timer1.Stop()

            Return page
        End Function
    End Class

    But I think the main problem is that I don't get any data from the server, that's why it keeps looping.
    So am I calling the function in the wrong way or something


    Thanks,
    Robin

  • Whois programm