Solved

Network Stream error trying to read bytes

Posted on 2009-07-06
10
475 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

929 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now