Hi Folks
Im struggling along with what is essentially my first TcpClient and first .Net project.
I have a windows forms application project with a TcpClient and NetworkStream opened OK, CanWrite and CanRead are tested True, and NetworkStream.Write() is working fine.
However when I attempt a read I only ever seem to get the first five bytes of the data the server sends. For data less than 5 bytes long this obv. has little effect but is not very useful. There is no exception caught that might indicate the data that was read was to short, it just acts like there is only 5 bytes to be had :confused:
The server I am communicating with is an embedded device that essentially takes a raw TCP connection on a certain port and sends the byte data out on a serial port to a microcontroller which reads the data and generates a response back out onto the serial line, to the server and thus back onto the network and becomes the data my VB TcpClient should be receiving.
All parts of this system have been tested to be working fine, except the read from the stream - when I use the VB application the server holds the serial lines high after the first 5 bytes are recieved, preventing the uC from sending any more data, as if the server is waiting on an ACK or something... as I said, with Hyperterminal in place of the VB app it works perfectly - so frustrating!
so I guess my question is, is there any ACK or stream flush or anything like that that my application is missing that would cause the connection to the server to make the server believe further bytes should not be sent (yet)??
Thanks
This is the application code - it's pretty stripped down at this point. I am working in VS2012 and 2010 for .Net 4 (the issue appear in both versions of the IDE):
Im struggling along with what is essentially my first TcpClient and first .Net project.
I have a windows forms application project with a TcpClient and NetworkStream opened OK, CanWrite and CanRead are tested True, and NetworkStream.Write() is working fine.
However when I attempt a read I only ever seem to get the first five bytes of the data the server sends. For data less than 5 bytes long this obv. has little effect but is not very useful. There is no exception caught that might indicate the data that was read was to short, it just acts like there is only 5 bytes to be had :confused:
The server I am communicating with is an embedded device that essentially takes a raw TCP connection on a certain port and sends the byte data out on a serial port to a microcontroller which reads the data and generates a response back out onto the serial line, to the server and thus back onto the network and becomes the data my VB TcpClient should be receiving.
All parts of this system have been tested to be working fine, except the read from the stream - when I use the VB application the server holds the serial lines high after the first 5 bytes are recieved, preventing the uC from sending any more data, as if the server is waiting on an ACK or something... as I said, with Hyperterminal in place of the VB app it works perfectly - so frustrating!
so I guess my question is, is there any ACK or stream flush or anything like that that my application is missing that would cause the connection to the server to make the server believe further bytes should not be sent (yet)??
Thanks
This is the application code - it's pretty stripped down at this point. I am working in VS2012 and 2010 for .Net 4 (the issue appear in both versions of the IDE):
vb Code:
Imports System.Text Imports System.Net Imports System.Net.Sockets Public Class NetReadWriteTestForm Dim defaultRemoteIpAddress As IPAddress = IPAddress.Parse("169.254.11.150") Dim defaultRemotePort As Integer = 10001 Dim remoteEndPoint As IPEndPoint Dim clt As New TcpClient() Dim stm As NetworkStream Dim eot As Byte = &H4 Dim nl As Byte = &HA Private Sub NetReadWriteTestForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load RemoteIPBox.Text = defaultRemoteIpAddress.ToString RemotePortBox.Text = defaultRemotePort.ToString Me.Show() End Sub Private Sub ConnectButton_Click(sender As Object, e As EventArgs) Handles ConnectButton.Click If Not clt.Connected Then ' Checkclient is not already connected. clt = New TcpClient() remoteEndPoint = New IPEndPoint(IPAddress.Parse(RemoteIPBox.Text), CInt(RemotePortBox.Text)) Try ' Open the client and stream. clt.Connect(remoteEndPoint) stm = clt.GetStream Catch ex As Exception MessageBox.Show("Client not connected or stream not opened.") Return End Try If Not stm.CanRead And Not stm.CanWrite Then ' Check the stream can be read and written to. MessageBox.Show("Could not read or write to the new stream") stm.Close() clt.Close() Return End If stm.ReadTimeout = 1000 ConsoleBox.AppendText("Connected." + vbNewLine) End If ConnectButton.BackColor = Color.LimeGreen End Sub Private Sub DisconnectButton_Click(sender As Object, e As EventArgs) Handles DisconnectButton.Click If clt.Connected Then ' Check client is already open. Try ' Close client and stream. stm.Close() clt.Close() Catch ex As Exception MessageBox.Show("Client or stream not closed.") Return End Try ConsoleBox.AppendText("Disconnected." + vbNewLine) End If ConnectButton.BackColor = SystemColors.Control End Sub Private Sub SendButton_Click(sender As Object, e As EventArgs) Handles SendButton.Click Dim wBuf() As Byte = Encoding.ASCII.GetBytes(CommandBox.Text & Chr(eot) & Chr(nl)) Dim rBuf(1024) As Byte ' Expected ResponseLength not used yet. If clt.Connected Then If CommandBox.Text.Length = 0 Then MessageBox.Show("Command box is empty." + vbNewLine + "Try Again.") Return End If If clt.Connected Then ' Check client is connected. ConsoleBox.AppendText("Data:" + Encoding.ASCII.GetString(wBuf) + vbNewLine) Try ' Write data. stm.Write(wBuf, 0, wBuf.Length) Catch ex As Exception MessageBox.Show("Could not write data.") Return End Try End If If ResponseLengthBox.TextLength > 0 Then If CInt(ResponseLengthBox.Text) > 0 Then ConsoleBox.AppendText("Reading..." + vbNewLine) Try ' Read data. stm.Read(rBuf, 0, rBuf.Length) Catch ex As Exception MessageBox.Show("Could not read response.") End Try ConsoleBox.AppendText(Encoding.ASCII.GetString(rBuf)) ConsoleBox.AppendText(vbNewLine) End If End If Else MessageBox.Show("Client is not connected yet.") End If End Sub End Class