Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TCP Comms from a VB Forms based app

Status
Not open for further replies.

edmana

Programmer
Jan 23, 2008
114
US
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
 
What doesn't seem to be working about it?

You aren't really doing any encryption - just encoding. The socket objects don't recognize plain text they need the bytes that you want to send. The System.Text.Encoding.ASCII.GetBytes allows you to convert your string to a byte array compatible with the socket - and the GetString method works the opposite way.

Also you may want to try just working with the client object rather than the stream - the client provides a way to send/receive as well.

I'm using 2008, but it should be much different (I haven't tested this but it should get the concept across):
Code:
        Dim client As New TcpClient("server", 80)
        Dim buffer(256) As Byte

        ...

        Dim err As SocketError
        client.Client.Send(buffer, 0, 256, SocketFlags.None, err)
        If err <> SocketError.Success Then
            'there was an error
        End If

        ...

        client.Client.Receive(buffer, 0, 256, SocketFlags.None, err)
        If err <> SocketError.Success Then
            'there was an error
        End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top