Solved

Network Stream error trying to read bytes

Posted on 2009-07-06
10
483 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Network ports are the threads that hold network communication together. They are an essential part of networking that can be easily ignore or misunderstood, my goals is to show those who don't have a strong network foundation how network ports opera…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

690 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