NetworkStream.Read

  • Thread starter Thread starter Kai Thorsrud
  • Start date Start date
K

Kai Thorsrud

Hi,

Thanks a lot for the short path solution to the app i'm working on by
including a Perl script ( App i'm converting from perl to .Net) for the part
i can't
do yet.
I'm communicating with a Cisco Router and i selected Sync reading since i
have an expected datapattern when this app runs.

However i thought that the read method where supposed to wait util the
entire stream was recieved before returning ?
I'm doing a write, wait for the echo to complete then i do a read to read
the result of the command i send.
The read command returnes long berfore the stream is complete. If i step
throu the code it works since i can't manage to step in the same speed as
the running app :)
I tried to put a 500msec sleep between the read of the echo and the read of
the result of the command (show running-config)

Am i doing this wrong ? here is my read function


Public Function ReadDataFromStream(ByVal objNetWorkStream As
NetworkStream) As String

Dim sTempBuffer As String
Dim sResultBuffer As String
Dim iBytesRead As Integer
Try

If objNetWorkStream.CanRead() Then

' When Called the Read method will wait until data is
available
iBytesRead = objNetWorkStream.Read(bReadBuffer, 0,
iReciveBufferSize)
sTempBuffer = Encoding.ASCII.GetString(bReadBuffer)

' Pull out the data we recieved into a string
sResultBuffer = Mid(sTempBuffer, 1, iBytesRead)

'DataAvailable returns true only if data is available on the
datastreams read buffer. Doing this to be sure that we fetch all the data

Do While objNetWorkStream.DataAvailable
iBytesRead = objNetWorkStream.Read(bReadBuffer, 0,
iReciveBufferSize)
sTempBuffer = Encoding.ASCII.GetString(bReadBuffer)
sResultBuffer = sResultBuffer & Mid(sTempBuffer, 1,
iBytesRead)
oEventLog.LogEvent(Me.EventLogName, "ReadDataFromStream
read: " & sTempBuffer, EventLogEntryType.Information)
Loop

End If

Catch ex As Exception
oEventLog.LogEvent(Me.EventLogName, "Failed reading from the
datastream of " & Me.RemoteHost & ":" & Me.RemotePort,
EventLogEntryType.Error)
printDebugMessage(ex.ToString())
End Try
ReadDataFromStream = sResultBuffer

End Function
 
Kai Thorsrud said:
Hi,

Thanks a lot for the short path solution to the app i'm working on by
including a Perl script ( App i'm converting from perl to .Net) for the part
i can't
do yet.
I'm communicating with a Cisco Router and i selected Sync reading since i
have an expected datapattern when this app runs.

However i thought that the read method where supposed to wait util the
entire stream was recieved before returning ?
I'm doing a write, wait for the echo to complete then i do a read to read
the result of the command i send.
The read command returnes long berfore the stream is complete. If i step
throu the code it works since i can't manage to step in the same speed as
the running app :)
I tried to put a 500msec sleep between the read of the echo and the read of
the result of the command (show running-config)

You need to do something like this:

iBytesRead = 0
totalBytesExpected = 1024 * 47
do while iBytesRead < totalBytesExpected
iBytesRead += objNetWorkStream.Read(bReadBuffer,
iBytesRead,iReciveBufferSize)
loop


David
 
But then i need to know the expected amount of data to be recieved before i
recieve the data ?
Wouldn't that make the read function pretty useless at communicating with a
telnetserve as a Cisco router ?
I'm gonna use this read function to poll 50 routers for their config file
and the config file differs in size for each router.

Isn't there a more generic approach of solving this ? I really didn't like
to but a sleep command there before i do a read since you never now what you
get in return.
This will give me the same problem... Or am i missing a point here ?

Kai
 
Kai Thorsrud said:
But then i need to know the expected amount of data to be recieved before i
recieve the data ?
Wouldn't that make the read function pretty useless at communicating with a
telnetserve as a Cisco router ?
I'm gonna use this read function to poll 50 routers for their config file
and the config file differs in size for each router.

Isn't there a more generic approach of solving this ? I really didn't like
to but a sleep command there before i do a read since you never now what you
get in return.
This will give me the same problem... Or am i missing a point here ?

I assumed that you know how big the files were.

As far as I know you have 4 options with TCP/IP:

1) Read until you have received a agreed number of bytes.

2) Read until the remote host closes the connection (ie read to the end).

3) Read until you see a special token in the stream.

4) Read until a read times out.

David
 
Thanks alot... I solved it by watching for a specific pattern in the
recieved stream :) Works like a dream.... jiiipiii

B Regards
Kai
 
Back
Top