BufferedReader 从套接字读取
我有一个 bufferedreader,由于某种原因,它不会从我从客户端发送的打印流中读取文本。这是每次 line = in.readline 时失败的点
我也检查过并且服务器已连接。
这是错误,
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at Server.ServerListener$getXML.run(ServerListener.java:82)
at java.lang.Thread.run(Thread.java:662)
提前致谢
BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));
//PrintStream out = new PrintStream(server.getOutputStream());
System.out.println("Start");
//read the xml
boolean connected = server.isConnected();
System.out.println("xml: "+ connected);
line = in.readLine();
System.out.println("Postread");
while ((line = in.readLine()) != null) {
System.out.println("while1");
xml = xml + line;
System.out.println("while2");
}`
I have a bufferedreader and for some reason it wont read the text from the print stream I am sending from my client. This is the point at which it fails every time the line = in.readline
Also I have checked and the server is connected.
This is the error
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at Server.ServerListener$getXML.run(ServerListener.java:82)
at java.lang.Thread.run(Thread.java:662)
Thanks in advance
BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));
//PrintStream out = new PrintStream(server.getOutputStream());
System.out.println("Start");
//read the xml
boolean connected = server.isConnected();
System.out.println("xml: "+ connected);
line = in.readLine();
System.out.println("Postread");
while ((line = in.readLine()) != null) {
System.out.println("while1");
xml = xml + line;
System.out.println("while2");
}`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
isConnected() 告诉您您的套接字是否已连接到该连接,而不是该连接是否仍连接到对等方。显然,您根本没有保持连接。 “连接重置”有几个可能的原因:您写入的连接已被另一端关闭(应用程序协议错误);另一端中止连接;本地 TCP 堆栈在发送时遇到网络错误并已放弃。第一个最有可能是可疑的。并且不要通过网络使用 PrintStreams/Writers,因为它们会吞掉您需要了解的异常。并且您在第一个 readLine() 调用中丢弃了一行数据:将其删除并将其保留在循环中。
isConnected() tells you whether your socket is connected to the connection, not whether the connection is still connected to the peer. Obviously you aren't still connected at all. 'Connection reset' has several possible causes: you wrote to a connection that had already been closed by the other end (application protocol error); the other end aborted the connection; the local TCP stack has encountered network errors sending and has given up. First of those is then most likely suspect. And don't use PrintStreams/Writers across the network, as they swallow exceptions you need to know about. And you are throwing away a line of data with the first readLine() call: remove it and just leave the one in the loop.