Q1: I'm trying to convert a working c++ program to VB.NET 2012 just to see how networkstream works. First send a stream to web address then get response. Send seems to work ok but read() blocks (line 34 of the code). Sometimes it doesn't block but doesn't return any data either. Anyone know why that would happen?

The code originally was copied from a tutorial I found.

Q2: Why is "String" in brackets, e.g. "[String]"? Are the brackets just optional?

   Shared Sub Connect(server As [String], message As [String], ByRef x As Form1)
        Dim MyText As String
        Try
            ' Create a TcpClient. 
            ' Note, for this client to work you need to have a TcpServer  
            ' connected to the same address as specified by the server, port 
            ' combination. 
            MyText = server & message

            Dim port As Int32 = 80
            Dim client As New TcpClient(server, port)

            ' Translate the passed message into ASCII and store it as a Byte array. 
            Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(MyText)

            ' Get a client stream for reading and writing. 
            '  Stream stream = client.GetStream(); 
            Dim stream As NetworkStream = client.GetStream()

            ' Send the message to the connected TcpServer. 
            stream.Write(data, 0, data.Length)
            MyText = "Sent: {" & MyText & "}" & vbCrLf
            x.txtOut.Text = x.txtOut.Text + MyText & vbCrLf
            'Console.WriteLine("Sent: {0}", message)

            ' Receive the TcpServer.response. 
            ' Buffer to store the response bytes.
            data = New [Byte](256) {}

            ' String to store the response ASCII representation. 
            Dim responseData As [String] = [String].Empty

            ' Read the first batch of the TcpServer response bytes. 
            Dim bytes As Int32 ' = stream.Read(data, 0, data.Length)
            responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
            MyText = "Received: " & responseData
            x.txtOut.Text = x.txtOut.Text + MyText & vbCrLf
            'Console.WriteLine("Received: {0}", responseData)

            ' Close everything.
            stream.Close()
            client.Close()
        Catch e As ArgumentNullException
            MyText = "ArgumentNullException: " & e.ToString()
            x.txtOut.Text = x.txtOut.Text + MyText & vbCrLf

            'Console.WriteLine("ArgumentNullException: {0}", e)
        Catch e As SocketException
            MyText = "SocketException: " & e.ToString()
            x.txtOut.Text = x.txtOut.Text + MyText & vbCrLf
            'Console.WriteLine("SocketException: {0}", e)
        End Try

        '        Console.WriteLine(ControlChars.Cr + " Press Enter to continue...")
        '        Console.Read()
    End Sub 'Connect
    Private Sub btnChkSpam_Click(sender As Object, e As EventArgs) Handles btnChkSpam.Click
        Connect("www.stopforumspam.com", "/api?ip=178.137.161.133", Me)
    End Sub

Edited 3 Years Ago by Ancient Dragon

The brakets allow you to name a variable the same as a reserved word.

Here is an article on the brakets.

As for the network stream - I have used this video for a reference a while back. I hope it may help you.

Edited 3 Years Ago by Begginnerdev

To supplement BD's comment on brackets, this is from the documentation;

Any program element — such as a variable, class, or member — can have the same name as a restricted keyword. For example, you can create a variable named Loop. However, to refer to your version of it — which has the same name as the restricted Loop keyword — you must either precede it with a full qualification string or enclose it in square brackets ([ ]), as the following example shows.

My synopsis of this is that brackets tell the compiler and/or IDE that the bracketed word is not a VB keyword. No harm in using them.

This article has been dead for over six months. Start a new discussion instead.