SocketException:尝试从 Java 中的套接字读取时连接重置?
由于 Java 套接字存在一些恼人的问题,我的应用程序似乎在非常基本的级别上失败了。我的应用程序的一部分需要通过 TCP 连接写入文件名。接收者代码如下:
ServerSocket serverSocket = new ServerSocket(4445);
Socket socket = serverSocket.accept();
BufferedReader reader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String filename = reader.readLine();
虽然我的发送者代码如下:
Socket socket = new Socket(InetAddress.getLocalHost(), 4445);
PrintWriter writer = new PrintWriter(socket.getOutputStream());
writer.write("Test.jpg");
这里非常非常基本的东西,但由于某种原因,当我运行此程序时,我收到 SocketException: Connection Reset?这是完整的堆栈跟踪:
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at FileReceiver.main(FileReceiver.java:11)
FileReceiver.java:11 行是进行 reader.readLine() 调用的行。我一生都无法弄清楚出了什么问题,同样的 TCP 套接字的基本使用过去一直对我有用,为什么现在会发生这种情况?
Having some irritating trouble with Java sockets, my application seems to be failing at a very basic level. Part of my application requires writing filenames across a TCP connection. The receiver code is as follows:
ServerSocket serverSocket = new ServerSocket(4445);
Socket socket = serverSocket.accept();
BufferedReader reader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String filename = reader.readLine();
While my sender code is as follows:
Socket socket = new Socket(InetAddress.getLocalHost(), 4445);
PrintWriter writer = new PrintWriter(socket.getOutputStream());
writer.write("Test.jpg");
Very, very basic stuff here, but for some reason, I'm getting a SocketException: Connection Reset when I run this? This is the full stack trace:
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at FileReceiver.main(FileReceiver.java:11)
with the FileReceiver.java:11 line being the one where the reader.readLine()
call is made. I can't for the life of me figure out what is going wrong, similarly basic use of TCP sockets has always worked for me in the past, why is this happening now?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
接收者正在等待行尾,而发送者永远不会发送。在发送端尝试类似
println()
的方法。The receiver is waiting for end-of-line, which your sender never sends. Try something like
println()
on the sending side.你的代码对我来说工作得很好......你忘了添加
到你的作家吗?
Your code is working perfectly fine for me... have you forgotten to add
to your writer?
你需要结合尼克和尼古拉所说的:
您需要使用 println 进行写入(因为 readLine 需要行尾)并且您需要在关闭它之前刷新您的 writer。
You need to combine what both Nick and Nikolai said:
You need to write with println (since readLine is expecting an end-of-line) and you need to flush your writer before closing it.
我建议您在尝试读取之前刷新您的写入器,在您尝试读取数据之前,数据有可能永远不会被发送。
I would suggest flushing your writer before you try and read, there is a chance the data is never being sent before you try to read it.
这是因为你的服务器退出了。使用 while 循环来发送。请参阅此。这里发生的是在客户端检索数据之前,服务器程序退出,以便重置连接并且您看到的异常在客户端抛出。
This is because your server exits. Use a while loop to send. See this. Here what happens is before the data is retrieved at the client side, server program exits so that connection is reset and the exception you see is thrown at the client side.
克里斯是对的。当您使用 PrintWriter 或什至像 ObjectOutputStream (ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());) 这样的序列化时,您需要调用 .flush() 函数,之后您必须关闭 Writer 和套接字。您的解决方案如下:
下面有一个序列化示例:(通过网络发送对象或将其保存到原始状态的文件中以供将来复制)
Chris is right. When you're writhing usint PrintWriter or even for serialization like ObjectOutputStream (ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());) you need to call the .flush() function and after that you must close the Writer and the socket. Your Solution is bellow:
Below you have an example for serialization : (send an Object through the network or save it to a file in original state for future replication)