Re: How to debug this?
- From: MeltingPoint <none@xxxxxxx>
- Date: Wed, 30 Mar 2005 22:56:16 -0600
"Brett" <no@xxxxxxxx> wrote in
news:ubaN0QaNFHA.3844@xxxxxxxxxxxxxxxxxxxx:
>
> "MeltingPoint" <none@xxxxxxx> wrote in message
> news:_s6dnStYC5jC9tbfRVn-jg@xxxxxxxxxxxxx
>> "Brett" <no@xxxxxxxx> wrote in
>> news:eWx8RjZNFHA.1096@xxxxxxxxxxxxxxxxxxxx:
>>
>>> For some reason when I step into the code below, it jumps out on the
>>> second iteration at the line I have marked below. Nothing else
>>> happens - no errors.
>>>
>>> Dim tcpClient As New System.Net.Sockets.TcpClient
>>> tcpClient.Connect("127.0.0.1", 9005)
>>>
>>> While True
>>> Dim networkStream As NetworkStream =
>>> tcpClient.GetStream()
>>>
>>> 'If networkStream.CanWrite And networkStream.CanRead
>>> Then
>>>
>>> ' Do a simple write.
>>> Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is
>>> anybody
>>> there")
>>> networkStream.Write(sendBytes, 0, sendBytes.Length)
>>> ' Read the NetworkStream into a byte buffer.
>>> Dim bytes(tcpClient.ReceiveBufferSize) As Byte
>>> networkStream.Read(bytes, 0,
>>> CInt(tcpClient.ReceiveBufferSize)) ' Output the data
>>> received from the host to the console. Dim returndata As
>>> String = Encoding.ASCII.GetString(bytes)
>>>
>>>
>>>
>>> Step into jumps out at above line. The rest of the code follows:
>>>
>>> Console.WriteLine(("Host returned: " + returndata))
>>>
>>> ' pause so user can view the console output
>>> Console.ReadLine()
>>> End While
>>>
>>> tcpClient.Close()
>>>
>>> There is a server version of the app that is sending data. Any
>>> suggestions on how I can trace this down?
>>>
>>> Thanks,
>>> Brett
>>>
>>>
>>>
>>
>> Not sure what you mean, 'jumps out'. Does that mean it throws an
>> exception? You can't 'step into' GetString() but I don't think thats
>> what you mean.
>>
>> A quick solution would be to use DataAvailable:
>>
>> If networkStream.DataAvailable() Then
>> networkStream.Read(...)
>> End If
>>
>> OR the dangerous way...
>>
>> Do
>> Loop Until networkStream.DataAvailable()
>>
>> too see if anything ever comes. (maybe put a counter in there and
>> exit after a couple hundred loops :)
>>
>> but i think your trying to read to soon, if you wait on
>> DataAvailable, all should work code wise.
>>
>> MP
>
> Very nice. The
>
> If networkStream.DataAvailable() Then
> networkStream.Read(...)
> End If
>
> code is working fine. I have a general question about the byte
> array. Sending/receiving data via the tcp/ip streams or file streams
> always uses the byte array. Why can't a regular string be used? What
> is so special about the byte array?
>
> Thanks,
> Brett
>
>
>
I would say it mostly has to do with unicode. One byte characters
just don't do it on the global scale. By making you use
Encoding.ASCII/UTF8/UTF7/Unicode.GetString/GetBytes Microsoft is reminding
you of which market your targeting/omitting.
A little blurp:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtextencodingclasstopic.asp
MP
.
- Follow-Ups:
- Re: How to debug this?
- From: Brett
- Re: How to debug this?
- From: Brett
- Re: How to debug this?
- References:
- How to debug this?
- From: Brett
- Re: How to debug this?
- From: MeltingPoint
- Re: How to debug this?
- From: Brett
- How to debug this?
- Prev by Date: Re: Line Numbers in RichTextBox..
- Next by Date: Crystal Reports Images visible
- Previous by thread: Re: How to debug this?
- Next by thread: Re: How to debug this?
- Index(es):
Relevant Pages
|
Loading