I am writing a small VB app in 2010 that will send a get request string created via TCP port 80. I created a sub based on some MSDN samples I googled:
Shared Sub Connect(ByVal server As [String], ByVal message As [String], ByRef formdebug As TextBox)
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.
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(message)
' 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)
formdebug.Text = "Sent:" + message + vbCrLf
' 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)
formdebug.Text = "Received:" + responseData + vbCrLf
' Close everything.
stream.Close()
client.Close()
'Catch err1 As ArgumentNullException
' MsgBox("ArgumentNullException")
Catch err2 As Exception
formdebug.Text = "Error" + vbCrLf
End Try
End Sub 'Connect
The idea is that I would pass the IP or hostname of the device I was sending the message to along w/ the message itself. I tried to add a text box param referenced by value so I could troubleshoot the send/response as the code doesn't seem to be working.
I can't seem to get any response. I believe I am having 2 issues here. One is the correct execution of the code and the other is debugging the send/receive. I tried to do a message box w/ the error but VB didn't like that.
Can anyone point me in the right direction?
Ultimately, the task for the function is to send out a get request string to port 80 in plain text. I am a bit confused by the encryption as I am doing what I need in the string already and want VB to only send out the string in plain text.
I have another piece of hardware that talks to the device already in a proprietary language that is sending the commands fine so I know the commands are good.
Also, please keep in mind I am relatively new to VB but have programmed in other environments so please keep the flame burning low!
Thanks!
Ed
Shared Sub Connect(ByVal server As [String], ByVal message As [String], ByRef formdebug As TextBox)
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.
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(message)
' 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)
formdebug.Text = "Sent:" + message + vbCrLf
' 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)
formdebug.Text = "Received:" + responseData + vbCrLf
' Close everything.
stream.Close()
client.Close()
'Catch err1 As ArgumentNullException
' MsgBox("ArgumentNullException")
Catch err2 As Exception
formdebug.Text = "Error" + vbCrLf
End Try
End Sub 'Connect
The idea is that I would pass the IP or hostname of the device I was sending the message to along w/ the message itself. I tried to add a text box param referenced by value so I could troubleshoot the send/response as the code doesn't seem to be working.
I can't seem to get any response. I believe I am having 2 issues here. One is the correct execution of the code and the other is debugging the send/receive. I tried to do a message box w/ the error but VB didn't like that.
Can anyone point me in the right direction?
Ultimately, the task for the function is to send out a get request string to port 80 in plain text. I am a bit confused by the encryption as I am doing what I need in the string already and want VB to only send out the string in plain text.
I have another piece of hardware that talks to the device already in a proprietary language that is sending the commands fine so I know the commands are good.
Also, please keep in mind I am relatively new to VB but have programmed in other environments so please keep the flame burning low!
Thanks!
Ed