SocketException:尝试从 Java 中的套接字读取时连接重置?

发布于 2024-12-17 13:50:36 字数 1305 浏览 0 评论 0原文

由于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

阳光的暖冬 2024-12-24 13:50:36

接收者正在等待行尾,而发送者永远不会发送。在发送端尝试类似 println() 的方法。

The receiver is waiting for end-of-line, which your sender never sends. Try something like println() on the sending side.

暗恋未遂 2024-12-24 13:50:36

你的代码对我来说工作得很好......你忘了添加

    writer.close();
    socket.close();

到你的作家吗?

Your code is working perfectly fine for me... have you forgotten to add

    writer.close();
    socket.close();

to your writer?

初雪 2024-12-24 13:50:36

你需要结合尼克和尼古拉所说的:
您需要使用 println 进行写入(因为 readLine 需要行尾)并且您需要在关闭它之前刷新您的 writer。

 Socket socket = new Socket(InetAddress.getLocalHost(), 4445);
 PrintWriter writer = new PrintWriter(socket.getOutputStream());
 writer.println("Test.jpg");
 writer.flush();

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.

 Socket socket = new Socket(InetAddress.getLocalHost(), 4445);
 PrintWriter writer = new PrintWriter(socket.getOutputStream());
 writer.println("Test.jpg");
 writer.flush();
困倦 2024-12-24 13:50:36

我建议您在尝试读取之前刷新您的写入器,在您尝试读取数据之前,数据有可能永远不会被发送。

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.

草莓酥 2024-12-24 13:50:36

这是因为你的服务器退出了。使用 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.

等你爱我 2024-12-24 13:50:36

克里斯是对的。当您使用 PrintWriter 或什至像 ObjectOutputStream (ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());) 这样的序列化时,您需要调用 .flush() 函数,之后您必须关闭 Writer 和套接字。您的解决方案如下:

 Socket socket = new Socket(InetAddress.getLocalHost(), 4445);
 PrintWriter writer = new PrintWriter(socket.getOutputStream());
 writer.println("Test.jpg");
 writer.flush();
 writer.close();
 socket.close();

下面有一个序列化示例:(通过网络发送对象或将其保存到原始状态的文件中以供将来复制)

    ...
ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(record);
oos.flush();
oos.close();
socket.close();

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:

 Socket socket = new Socket(InetAddress.getLocalHost(), 4445);
 PrintWriter writer = new PrintWriter(socket.getOutputStream());
 writer.println("Test.jpg");
 writer.flush();
 writer.close();
 socket.close();

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)

    ...
ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(record);
oos.flush();
oos.close();
socket.close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文