?
Solved

Network Stream error trying to read bytes

Posted on 2009-07-06
10
Medium Priority
?
484 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

777 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