Solved

Network Stream error trying to read bytes

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Configuring network clients can be a chore, especially if there are a large number of them or a lot of itinerant users.  DHCP dynamically manages this process, much to the relief of users and administrators alike!
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

17 Experts available now in Live!

Get 1:1 Help Now