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