Solved

Network Stream error trying to read bytes

Posted on 2009-07-06
10
479 Views
Last Modified: 2013-11-07
I have a service written in VB.Net that listens on a port for incoming messages.  While I try to read the stream, the service gets locked up.  I put some Log Entries in help me determine where the code fails and I actually get all the data that is being sent to the service, however, it hangs up after it gets the data.  Some of the errors in the log entries say:
Unable to read data from the transport connection.  

Here's the code:

Dim ms As New MemoryStream
Dim tcpClient As TcpClient = listener.AcceptTcpClient()
Dim NWStream As NetworkStream = tcpClient.GetStream
Dim bytesToRead(tcpClient.ReceiveBufferSize) As Byte
Dim sr As StreamReader
Dim numBytesRead As Integer = 1024
Dim bufferSize = tcpClient.ReceiveBufferSize

numBytesRead = NWStream.Read(bytesToRead, 0, bufferSize)
log.WriteEntry("starting try")
While numBytesRead > 0
     Try
         log.WriteEntry("Num: " & numBytesRead & " - " & bufferSize)
         If numBytesRead > 0 Then
                 ms.Write(bytesToRead, 0, numBytesRead)
                 log.WriteEntry("Bytes ( " & numBytesRead & "): " & Encoding.ASCII.GetString(bytesToRead, 0, numBytesRead))
                 numBytesRead = NWStream.Read(bytesToRead, 0, bufferSize)
                 If numBytesRead <= 0 Then Exit While
     Else
            Exit While
     End If
 Catch ex As Exception
        log.WriteEntry("Error: " & ex.Message)
  End Try
End While
log.WriteEntry("end try")
NWStream.Close()
0
Comment
Question by:robnhood00
  • 3
10 Comments
 

Author Comment

by:robnhood00
ID: 24792885
Very Strange... I've changed some of my code to this and it seems like the data is coming through however, now it hangs up and the data becomes unreadable after I write it in the log entry.  If I take the log entry out then sometimes it goes through... and sometimes I only get 1024 characters.

                Dim numBytesRead As Integer
                Dim bufferSize As Integer = 10
                Do
                    Try
                        numBytesRead = NWStream.Read(bytesToRead, 0, bufferSize)
                        If numBytesRead > 0 Then
                            ms.Write(bytesToRead, 0, numBytesRead)
                        Else
                            Exit Do
                        End If
                    Catch ex As Exception

                    End Try
                Loop Until Not NWStream.DataAvailable

               ms.Seek(0, SeekOrigin.Begin)
                sr = New StreamReader(ms, Encoding.ASCII)
                ms.Seek(0, SeekOrigin.Begin)
                clientdata(0) = sr.ReadToEnd                
                sr.Close()
                ms.Close()
                log.WriteEntry(clientdata(0))
         ''''rest of code to process data
0
 

Author Comment

by:robnhood00
ID: 24796064
Another strange thing here...
If I uninstall the service.... reinstall the service... clear the Event Log (MyLog)... Then the first message comes through in FULL and it works great.  However, subsequent messages fail and are truncated after 1024 bytes.  
If I repeat the above process... then the service works fine for the first message.  Note... that for some reason, I have to clear the Event Log or it won't work.
0
 

Author Comment

by:robnhood00
ID: 24808092
My messages are consistently getting cut off at 1024 characters.  Is it posslbly a network issue with the server?  Something not running in one of the services?  I used a third party port listener and it can see all the data.  I used a 3rd party .NET Port Listener and it also consistently gets cut off at 1024 characters.
0
 
LVL 4

Accepted Solution

by:
pepepaco earned 500 total points
ID: 24826676
create a variable  Dim numBytesToRead As Integer = CInt(s.Length)

instead of compare  " If numBytesRead <= 0 Then Exit While" decrement the numBytesToRead to  numBytesToRead -numBytesRead  

example from  http://msdn.microsoft.com/en-us/library/system.io.stream.read.aspx:


Imports System
Imports System.IO
Imports Microsoft.VisualBasic
 
Public Class Block
 
    Public Shared Sub Main()
        Dim s As New MemoryStream()
        Dim i As Integer
        For i = 0 To 99
            s.WriteByte(CByte(i))
        Next i
        s.Position = 0
 
        ' Now read in s into a byte buffer.
        Dim bytes(s.Length) As Byte
        Dim numBytesToRead As Integer = CInt(s.Length)
        Dim numBytesRead As Integer = 0
        While numBytesToRead > 0
            ' Read can return anything from 0 to numBytesToRead.
            Dim n As Integer = s.Read(bytes, numBytesRead, numBytesToRead)
            ' The end of the file has been reached.
            If n = 0 Then
                Exit While
            End If
            numBytesRead += n
            numBytesToRead -= n
        End While
        s.Close()
        ' numBytesToRead should be 0 now, and numBytesRead should
        ' equal 100.
        Console.WriteLine("number of bytes read: " & numBytesRead.ToString())
    End Sub
End Class

Open in new window

0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question