Re: How is this conditional able to execute?




"Stephany Young" <noone@localhost> wrote in message
news:e$BfbyJRFHA.2384@xxxxxxxxxxxxxxxxxxxxxxx
> Let's have a pause and figure out where we are at.
>
> You had, originally:
>
> Dim networkStream As NetworkStream = tcpClientObj.GetStream()
> ' Read the stream into a byte array
> Dim bytes(tcpClientObj.ReceiveBufferSize) As Byte
>
> If networkStream.DataAvailable() Then
> networkStream.Read(bytes, 0, CInt(tcpClientObj.ReceiveBufferSize))
> Dim mystring As String = Encoding.UTF8.GetString(bytes)
>
> Dim sendBytes_ As [Byte]() = Encoding.ASCII.GetBytes(mystring)
>
> networkStream.Write(sendBytes_, 0, sendBytes_.Length)
> sendBytes_.Clear(sendBytes_, 0, sendBytes_.GetLength(0))

--It looks more like this:

While True
Try

If networkStream.CanWrite And networkStream.CanRead Then

networkStream = tcpClient.GetStream()

Dim bytes(tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0,
tcpClient.ReceiveBufferSize)
Dim returndata As String =
Trim(Encoding.UTF8.GetString(bytes).Trim(Char.MinValue))

If InitialIDSent Then
Packet = "Nothing to Write from: " &
Thread.CurrentThread.Name
Else
Packet = "Connected: " & ClientForm.Packet
InitialIDSent = True
'write to log file
ClientForm.utility.WriteToFile(ClientForm.CurrentDirectory.GetCurrentDirectory,
ClientForm.LogFileExec, ClientForm.utility.CurrentDateTime(0) & " -
Connected: " & ClientForm.Packet)
End If

' Do a simple write.
Dim sendBytes As [Byte]() =
Encoding.ASCII.GetBytes(Packet.TrimEnd)
networkStream.Write(sendBytes, 0, sendBytes.Length)

If networkStream.DataAvailable() Then
If (returndata <> "") And returndata.ToString <>
ClientForm.ReturnData Then
ClientForm.ReturnData = returndata
returndata = Nothing

Else
End If
End If

End If

Catch ex As Exception

If ex.Message = "Operation not allowed on non-connected
sockets." Then
ConnectInit()
ConnectAttempts = +1

If ConnectAttempts > 5 Then
Exit Sub
End If
Else
End If
Exit While
End Try

Thread.CurrentThread.Sleep(500)
End While


During the time I was testing, the try/catch was never hit so it shouldn't
be a factor.


>
> Note that you are only showing us a small fragment here as the the 'End'
> for the 'If' is obviously missing. Is there perhaps some other code that
> could be affecting your results?
>
> That aside, as I understand it, the fragment now reads:
>
> Dim networkStream As NetworkStream = tcpClientObj.GetStream()
> ' Read the stream into a byte array
> Dim bytes(tcpClientObj.ReceiveBufferSize) As Byte
>
>
> If networkStream.DataAvailable() Then
> networkStream.Read(bytes, 0, CInt(tcpClientObj.ReceiveBufferSize))
> Dim mystring As String =
> Trim(Encoding.UTF8.GetString(bytes).Trim(Char.MinValue))
>
> Dim sendBytes_ As [Byte]() = Encoding.ASCII.GetBytes(mystring)
>
>
> networkStream.Write(sendBytes_, 0, sendBytes_.Length)
> sendBytes_.Clear(sendBytes_, 0, sendBytes_.GetLength(0))
>
> The additional Trim appears redundant and the line should read:
>
> Dim mystring As String =
> Encoding.UTF8.GetString(bytes).Trim(Char.MinValue)
>
> This is bourne out by our small test block that shows that this actually
> works correctly.
>
> Now we need to consider what other 'stray' characters may be affecting the
> string:
>
> A loop such as:
>
> Console.Writeline("mystring.Length = {0}", mystring.Length)
> For _i As Integer = 0 To mystring.Length
> Select Case CType(mystring.Chars(_i), Integer)
> Case Is < 32, Is > 126
> Console.Writeline("Character {0} found at position {1}",
> CType(mystring.Chars(_i), Integer), _i)
> End Select
> Next
>

I pasted in the above but get the following error on both occurances of the
following:
mystring.Chars(_i)

'Char' values cannot be converted to 'Integer'. Use
'Microsoft.VisualBasic.AscW' to interpret a character as a unicode value or
'Microsoft.Visualbasic.Val' to interpret it as a digit.

> Once these, if any have been identified we need to figure out how to deal
> with them.
>
>
> "Brett" <no@xxxxxxxx> wrote in message
> news:OqCz6MJRFHA.2972@xxxxxxxxxxxxxxxxxxxxxxx
>> OK, sorry. Here are my results in a new application:
>>
>> _b.Length = 50
>> _s.Length = 50
>> _s.Length (after trim) = 35
>> _s = *Nothing to Write from: Development3*
>>
>> This is also the same thing you got. Looks as though we might be on an
>> even playing field, which is good to move forward. What do you suggest
>> at this point?
>>
>> "Stephany Young" <noone@localhost> wrote in message
>> news:OqeXJwIRFHA.248@xxxxxxxxxxxxxxxxxxxxxxx
>>> The point of the code I posted was so that you could see if you got the
>>> EXACT same results when you executed the EXACT same code.
>>>
>>> Simply create a new Windows Application project, plonk a button on the
>>> form and copy and paste the code I posted into the button Click event
>>> handler, run the project, make sure the Output window is showing, click
>>> the button and observe the results.
>>>
>>> If you don't get the EXACT same results a I did, then there's something
>>> else going on that either, you don't know about or you aren't telling us
>>> about.
>>>
>>> If you do get the EXACT same results then that will prove that the
>>> 'trimming' of the Null characters does actually work as expected on your
>>> machine.
>>>
>>> Remember that the chance of someone contributing to this thread having
>>> the same hardware/software configuration as you is practically nil.
>>> There may well be something in your configuration that is contributing
>>> to the problems that you are observing. If that is the case then it is
>>> anyones guess as to what it is.
>>>
>>> Remember also, that we can only point you in the right direction. You
>>> have to do the legwork yourself.
>>
>> grrrrr...but yes - you are right.
>>
>>>
>>>
>>> "Brett" <no@xxxxxxxx> wrote in message
>>> news:%23KGoTbIRFHA.2132@xxxxxxxxxxxxxxxxxxxxxxx
>>>>I really can't implement what you have tested here. returned data is
>>>>still "" and its length is 8192 (depending if the 1 is subtracted).
>>>>There are leading and trailing zeros. I'm doing
>>>>
>>>> Dim returndata As String =
>>>> Trim(Encoding.UTF8.GetString(bytes).Trim(Char.MinValue))
>>>>
>>>> and that has no effect. returndata still has a large length and value
>>>> of "". I follow the above line by this code, which also has no effect:
>>>>
>>>> returndata = Trim(returndata.Trim(Char.MinValue))
>>>>
>>>> debugger doesn't even run over that code. It skips to the next line.
>>>> Rather than highlight a whole line, it only highlights parts and only
>>>> runs on certain lines. That's another story though.
>>>>
>>>> Thanks,
>>>> Brett
>>>> "Stephany Young" <noone@localhost> wrote in message
>>>> news:eID0C6BRFHA.3716@xxxxxxxxxxxxxxxxxxxxxxx
>>>>> OK. So far so good
>>>>>
>>>>> Using the following:
>>>>>
>>>>> Dim _b() As Byte = New Byte(49) {0, 78, 111, 116, 104, 105, 110,
>>>>> 103, 32, 116, 111, 32, 87, 114, 105, 116, 101, 32, 102, 114, 111, 109,
>>>>> 58, 32, 68, 101, 118, 101, 108, 111, 112, 109, 101, 110, 116, 51, 0,
>>>>> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
>>>>>
>>>>> Console.WriteLine("_b.Length = {0}", _b.Length)
>>>>>
>>>>> Dim _s As String = System.Text.Encoding.UTF8.GetString(_b)
>>>>>
>>>>> Console.WriteLine("_s.Length = {0}", _s.Length)
>>>>>
>>>>> _s = _s.Trim(Char.MinValue)
>>>>>
>>>>> Console.WriteLine("_s.Length (after trim) = {0}", _s.Length)
>>>>>
>>>>> Console.WriteLine("_s = *{0}*", _s)
>>>>>
>>>>> I get the following results:
>>>>>
>>>>> _b.Length = 50
>>>>> _s.Length = 50
>>>>> _s.Length (after trim) = 35
>>>>> _s = *Nothing to Write from: Development3*
>>>>>
>>>>> Which is exactly what I would expect to get from the byte values you
>>>>> supplied.
>>>>>
>>>>> Note that the _s.Trim(Char.MinValue) has stripped the Null character
>>>>> from the beginning and the 14 Null characters from the end which
>>>>> leaves 35 'good' characters (see above).
>>>>>
>>>>> It is the Null character at the beginning that is causing your string
>>>>> to be interpreted as an empty string because the Null character is
>>>>> treated as an 'end of string' marker. Any leading Null characters must
>>>>> be 'trimmed' off and it is recommended that you 'trim' any trailing
>>>>> Null characters as well.
>>>>>
>>>>>
>>>>>
>>>>> "Brett" <no@xxxxxxxx> wrote in message
>>>>> news:%2326T0lBRFHA.164@xxxxxxxxxxxxxxxxxxxxxxx
>>>>>>
>>>>>> "Stephany Young" <noone@localhost> wrote in message
>>>>>> news:uYXW0%23ARFHA.3364@xxxxxxxxxxxxxxxxxxxxxxx
>>>>>>> This is getting scary :)
>>>>>>>
>>>>>>> Lets take this one step at a time.
>>>>>>>
>>>>>>> I'm a bit perplexed as to why a recieve buffer would be such a
>>>>>>> strange size. Could it be that the value of
>>>>>>> tcpClient.ReceiveBufferSize is actually 8192 (8KB)? If so, then the
>>>>>>> array declaration should be:
>>>>>>>
>>>>>>> Dim bytes(tcpClient.ReceiveBufferSize - 1) As Byte
>>>>>>
>>>>>> Yes - it is 8192 after putting in the above code.
>>>>>>
>>>>>>>
>>>>>>> In VB.NET, arrays are 0 based and the value specified is the upper
>>>>>>> bound of the array as opposed to the number of elements (as in
>>>>>>> C#.NET).
>>>>>>>
>>>>>>> Can you dump the first 50 bytes of the array on the first call.
>>>>>>> Directly after the line:
>>>>>>>
>>>>>>> networkStream.Read(bytes, 0, tcpClient.ReceiveBufferSize)
>>>>>>>
>>>>>>> insert:
>>>>>>>
>>>>>>> For _i As Integer = 0 to 48
>>>>>>> Console.Write("{0},", bytes(_i).ToString())
>>>>>>> Next
>>>>>>> Console.WriteLine("{0}", bytes(_i).ToString())
>>>>>>>
>>>>>>> Add another line:
>>>>>>>
>>>>>>> Console.WriteLine()
>>>>>>>
>>>>>>> and place a breakpoint on this line.
>>>>>>>
>>>>>>> Run the code then copy and paste the result into a post to this
>>>>>>> thread.
>>>>>>
>>>>>> When returndata is "", the above code gives this:
>>>>>>
>>>>>> 0,78,111,116,104,105,110,103,32,116,111,32,87,114,105,116,101,32,102,114,111,109,58,32,68,101,118,101,108,111,112,109,101,110,116,51,0,0,0,0,0,0,0,0,0,0,0,0,0,0
>>>>>>
>>>>>>>
>>>>>>> It will be interesting to see just what characters are arriving.
>>>>>>>
>>>>>>> A new byte array is created each time the networkStream object is
>>>>>>> read so there is nothing from the previous red remaining. Therefore
>>>>>>> the comment about the 'values shifting' is a red herring. What you
>>>>>>> are getting is actually what is coming from the networkStream
>>>>>>> object.
>>>>>>>
>>>>>>> Also, is there any particular reason why you are using UTF8 encoding
>>>>>>> to interpret the data? Is it specified somewhere that the data
>>>>>>> arriving in the networkStream object is actually UTF8 or is just a
>>>>>>> stab in the dark?
>>>>>>
>>>>>> Yes - I read on experts-exchange.com that this would work with Trim
>>>>>> but it didn't. I never removed it because it didn't seem to make a
>>>>>> difference.
>>>>>>
>>>>>> On the listener, I haven't declared a size that should be 8KB. I do
>>>>>> this:
>>>>>>
>>>>>> Dim networkStream As NetworkStream =
>>>>>> tcpClientObj.GetStream()
>>>>>> ' Read the stream into a byte array
>>>>>> Dim bytes(tcpClientObj.ReceiveBufferSize) As Byte
>>>>>>
>>>>>> If networkStream.DataAvailable() Then
>>>>>> networkStream.Read(bytes, 0,
>>>>>> CInt(tcpClientObj.ReceiveBufferSize))
>>>>>>
>>>>>> Dim mystring As String =
>>>>>> Encoding.UTF8.GetString(bytes)
>>>>>>
>>>>>> Dim sendBytes_ As [Byte]() =
>>>>>> Encoding.ASCII.GetBytes(mystring)
>>>>>>
>>>>>> networkStream.Write(sendBytes_, 0, sendBytes_.Length)
>>>>>> sendBytes_.Clear(sendBytes_, 0,
>>>>>> sendBytes_.GetLength(0))
>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> "Brett" <no@xxxxxxxx> wrote in message
>>>>>>> news:%23aS0EDARFHA.2744@xxxxxxxxxxxxxxxxxxxxxxx
>>>>>>>>I tried this but it isn't working either. At one point, the bytes
>>>>>>>>array has non zero elements up to element 35. returndata has the
>>>>>>>>value of "" (or so it seems) and length of 8193, same as the bytes
>>>>>>>>array. Through each pass of the loop, the non zero elements increase
>>>>>>>>in the bytes array. So it goes from element 35 to 36, 37 and so on.
>>>>>>>>The values are shifting so that the first 10 are now zero. Then the
>>>>>>>>first 11, 12, 13 and so on.
>>>>>>>>
>>>>>>>> Any other suggestions?
>>>>>>>>
>>>>>>>> Thanks,
>>>>>>>> Brett
>>>>>>>>
>>>>>>>> "Stephany Young" <noone@localhost> wrote in message
>>>>>>>> news:u1TlDb9QFHA.2680@xxxxxxxxxxxxxxxxxxxxxxx
>>>>>>>>> Sorry, my mistake. Null (Chr(0)) is not treated as whitespace by
>>>>>>>>> the String.Trim Method.
>>>>>>>>>
>>>>>>>>> Instead, you can can use one of the overloaded String.Trim
>>>>>>>>> methods.
>>>>>>>>>
>>>>>>>>> Dim returndata As String =
>>>>>>>>> Encoding.UTF8.GetString(bytes).Trim(Char.MinValue)
>>>>>>>>>
>>>>>>>>> Char.MinValue is a constant for 0x00 which equates to Null or
>>>>>>>>> Chr(0).
>>>>>>>>>
>>>>>>>>> This overload removes all occurrences of a set of characters
>>>>>>>>> specified in an array from the beginning and end of the string
>>>>>>>>> instance.
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> "Brett" <no@xxxxxxxx> wrote in message
>>>>>>>>> news:uPLcAZ6QFHA.3988@xxxxxxxxxxxxxxxxxxxxxxx
>>>>>>>>>>
>>>>>>>>>> "Stephany Young" <noone@localhost> wrote in message
>>>>>>>>>> news:%23hNf834QFHA.3988@xxxxxxxxxxxxxxxxxxxxxxx
>>>>>>>>>>> What test are you using to determine that returndata.ToString =
>>>>>>>>>>> ""?
>>>>>>>>>>>
>>>>>>>>>>> Are you , for example, executing a:
>>>>>>>>>>>
>>>>>>>>>>> Console.Writeline("returndata.ToString = " &
>>>>>>>>>>> returndata.ToString)
>>>>>>>>>>>
>>>>>>>>>>> and getting the result:
>>>>>>>>>>>
>>>>>>>>>>> returndata.ToString =
>>>>>>>>>>>
>>>>>>>>>>> Your comment about returndata 'always' being 8193 bytes gives us
>>>>>>>>>>> the clue as what is happening.
>>>>>>>>>>>
>>>>>>>>>>> I suspect that returndata is padded with null characters
>>>>>>>>>>> (chr(0)).
>>>>>>>>>>>
>>>>>>>>>>> The Trim function (aka Microsoft.VisualBasic.Trim) only 'trims'
>>>>>>>>>>> leading and trailing Space characters.
>>>>>>>>>>>
>>>>>>>>>>> The String.Trim Method 'trims' leading and trailing whitespace
>>>>>>>>>>> characters. 'whitespace' comprises a number of characters
>>>>>>>>>>> including the null character (chr(0)).
>>>>>>>>>>>
>>>>>>>>>>> I think that if you change the line:
>>>>>>>>>>>
>>>>>>>>>>> Dim returndata As String =
>>>>>>>>>>> Trim(Encoding.UTF8.GetString(bytes))
>>>>>>>>>>>
>>>>>>>>>>> to
>>>>>>>>>>>
>>>>>>>>>>> Dim returndata As String =
>>>>>>>>>>> Encoding.UTF8.GetString(bytes).Trim()
>>>>>>>>>>>
>>>>>>>>>>> then the test will work correctly.
>>>>>>>>>>
>>>>>>>>>> That doesn't work either. In some cases, bytes will come in as a
>>>>>>>>>> zero byte array with length 8193. Every element in that array
>>>>>>>>>> will be zero. returndata is assigned and its length becomes 8193
>>>>>>>>>> as well.
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>


.


Loading