Reading data from TCPip SBC server

Supporting Visual Studio on Windows
spider
Phidgeteer!
Posts: 87
Joined: Sun Feb 03, 2008 2:23 pm

Reading data from TCPip SBC server

Postby spider » Fri Dec 04, 2009 12:56 pm

Hi I am trying to get data from the SBC using this code:

Friend Function SocketsClientReadWrite(ByVal Server As String, _
ByVal Port As Integer, _
ByVal WriteData As Boolean, _
ByVal message As [String]) As String

'If reading, returns data string, if writing returns empty 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.


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.
If WriteData Then stream.Write(data, 0, data.Length)

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

' 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)
SocketsClientReadWrite = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
'Console.WriteLine("Received: {0}", responseData)

' Close everything.
stream.Close()
client.Close()
SocketError = False
Catch e As ArgumentNullException
SocketsClientReadWrite = ""
MsgBox("Ethernet sockets client error (ArgumentNullException)" & e.Message)
SocketError = True
Catch e As SocketException
SocketsClientReadWrite = ""
MsgBox("Ethernet sockets client error (SocketException)" & e.Message)
SocketError = True
Catch ex As Exception
SocketsClientReadWrite = ""
MsgBox("General ethernet sockets client exception; " & ex.Message)
SocketError = True
End Try
End Function

I get as far as the:

Dim bytes As Int32 = stream.Read(data, 0, data.Length)

and then it just hangs. Has anybody successfully been able to do this and if so, could they please paste a snipit.

Return to “VB .NET”

Who is online

Users browsing this forum: No registered users and 1 guest