为什么我的netstream会在“Console.Write”输入数据时停止读取?

时间:2016-09-27 14:15:00

标签: c# tcpclient

这是我非常简单的<project> <groupId>org.company</groupId> <artifactId>other-artifact</artifactId> <packaging>pom</packaging> <version>1.0</version> <properties> <guava.version>18.0</guava.version> <slf4j.version>1.7.7</slf4j.version> </properties> <modules> <module>module-one</module> <module>module-two</module> </modules> <dependencyManagement> <dependencies> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>${guava.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> </dependencies> </dependencyManagement> </project> 方法TcpClient

ClientHandler

如果没有注释行,我的 public static void ClientHandler(object c) { TcpClient client = (TcpClient)c; NetworkStream netstream = client.GetStream(); bool connected = true; while (connected) { try { byte[] bytes = new byte[client.ReceiveBufferSize]; netstream.Read(bytes, 0, bytes.Length); Console.Write(System.Text.Encoding.Default.GetString(bytes)); //This line causes the issue try { Record json_record = JsonConvert.DeserializeObject<Record>(System.Text.Encoding.Default.GetString(bytes)); Console.WriteLine(json_record.Station); } catch (Exception e) { } } catch (Exception e) { Console.WriteLine("No longer connected"); connected = false; Console.WriteLine(e); Console.WriteLine(e.StackTrace); } } } 会很好地接收数据。如果我尝试ClientHandler传入的数据用于调试目的,我只得到数据的第一个字节,线程似乎挂起。

提前感谢您的帮助!

0 个答案:

没有答案