本文介绍了如何调试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 出于某些原因,当我进入下面的代码时,它跳出了我在下面标记的第二行 迭代。没有其他事情发生 - 没有 错误。 Dim tcpClient As New System.Net.Sockets.TcpClient tcpClient.Connect( 127.0.0.1,9005) 正确 Dim networkStream As NetworkStream = tcpClient.GetStream() ''如果是networkStream.CanWrite和networkStream.CanRead那么 ''做一个简单的写。 Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(有人 那里) networkStream.Write(sendBytes,0,sendBytes.Length) ''将NetworkStream读入字节缓冲区。 Dim bytes(tcpClient.ReceiveBufferSize)As Byte networkStream.Read(bytes,0,CInt(tcpClient.ReceiveBufferSize)) ''将从主机收到的数据输出到控制台。 Dim returndata As String = Encoding.ASCII.GetString(bytes) 步入跳出线。其余的代码如下: Console.WriteLine((主机返回:" + returndata)) ''暂停所以用户可以查看控制台输出 Console.ReadLine() 结束时 tcpClient.Close() 有一个发送数据的应用程序的服务器版本。任何建议 如何追溯这个? 谢谢, Brett 解决方案 Not确定你的意思,'跳出''。这是否意味着抛出 异常?你不能进入''GetString(),但我不认为那是 你的意思。 一个快速的解决方案会是使用DataAvailable: 如果networkStream.DataAvailable()那么 networkStream.Read(...) 结束If 或危险的方式... 请 循环直到networkStream.DataAvailable() 也可以看看是否有任何问题。 (可能在那里放一个柜台并在几百个循环后退出 ) 但是我想你很快就会读到,如果你等待DataAvailable, 都应该按照代码方式工作。 MP 不确定你的意思, ''跳出''。这是否意味着抛出了一个异常?你不能进入''GetString(),但我不认为那是什么意思。 快速解决方案是使用DataAvailable: 如果是networkStream.DataAvailable()那么 networkStream.Read(...)结束如果 或危险的方式...... 做循环直到networkStream.DataAvailable() 也看看是否有任何事情发生。 (可能在那里放一个计数器并在几百个循环后退出) 但是我想你很快就会读到,如果你等待DataAvailable,所有应该工作代码明智。 MP 非常好。 如果是networkStream.DataAvailable()那么 networkStream.Read(...) 结束如果 代码工作正常。我有一个关于字节数组的一般问题。 通过tcp / ip流或文件流发送/接收数据总是使用 字节数组。为什么不能使用常规字符串?什么是特别的 关于字节数组? 谢谢, Brett 不确定你的意思, ''跳出''。这是否意味着抛出了一个异常?你不能进入''GetString(),但我不认为那是什么意思。 快速解决方案是使用DataAvailable: 如果是networkStream.DataAvailable()那么 networkStream.Read(...)结束如果 或危险的方式...... 做循环直到networkStream.DataAvailable() 也看看是否有任何事情发生。 (也许在那里放一个柜台,然后在几百个循环后退出:) 但是我觉得你很快就会读到,如果你等待 DataAvailable,都应该工作代码明智。 MP 非常好。 如果networkStream.DataAvailable()那么 networkStream.Read(...)结束如果 代码工作正常。我有一个关于字节数组的一般问题。通过tcp / ip流或文件流发送/接收数据始终使用字节数组。为什么不能使用常规字符串?什么字节数组是如此特别? 谢谢, Brett 我会说它主要与unicode有关。一个字节的字符 只是在全球范围内不做。通过让你使用 Encoding.ASCII / UTF8 / UTF7 / Unicode.GetString / GetBytes微软提醒你b $ b你的目标/省略市场。 有点模糊: http://msdn.microsoft.com/library/de...classtopic.asp MP For some reason when I step into the code below, it jumps out on the seconditeration at the line I have marked below. Nothing else happens - noerrors. Dim tcpClient As New System.Net.Sockets.TcpClienttcpClient.Connect("127.0.0.1", 9005) While TrueDim networkStream As NetworkStream = tcpClient.GetStream() ''If networkStream.CanWrite And networkStream.CanRead Then '' Do a simple write.Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybodythere")networkStream.Write(sendBytes, 0, sendBytes.Length)'' Read the NetworkStream into a byte buffer.Dim bytes(tcpClient.ReceiveBufferSize) As BytenetworkStream.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 outputConsole.ReadLine()End While tcpClient.Close() There is a server version of the app that is sending data. Any suggestionson how I can trace this down? Thanks,Brett 解决方案 Not sure what you mean, ''jumps out''. Does that mean it throws anexception? You can''t ''step into'' GetString() but I don''t think thatswhat you mean. A quick solution would be to use DataAvailable: If networkStream.DataAvailable() ThennetworkStream.Read(...)End If OR the dangerous way... DoLoop Until networkStream.DataAvailable() too see if anything ever comes. (maybe put a counter in there and exitafter a couple hundred loops :) but i think your trying to read to soon, if you wait on DataAvailable,all should work code wise. MP 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() ThennetworkStream.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 usesthe byte array. Why can''t a regular string be used? What is so specialabout the byte array? 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 charactersjust don''t do it on the global scale. By making you useEncoding.ASCII/UTF8/UTF7/Unicode.GetString/GetBytes Microsoft is remindingyou of which market your targeting/omitting. A little blurp: http://msdn.microsoft.com/library/de...classtopic.asp MP 这篇关于如何调试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-14 20:12