出现错误“java.net.SocketException:连接重置”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43406275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:24:30  来源:igfitidea点击:

Getting error "java.net.SocketException: Connection reset"

javasockets

提问by Swapnil P.

I am learning Socket Programming in Java. I am getting java.net.SocketException: Connection reset.

我正在学习 Java 套接字编程。我得到java.net.SocketException: Connection reset.

Client Side Code

客户端代码

    package com.socket;

    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.Socket;

    public class ClientSock {

        public static void main(String[] args) throws Exception {

            Socket skt = new Socket("localhost", 8888);     
            String str = "Hello Server";
            OutputStreamWriter osw = new OutputStreamWriter(skt.getOutputStream()); 

            PrintWriter out = new PrintWriter(osw);
            osw.write(str);
            osw.flush();        
        }
    }

//Server Side Code:

    package com.socket;

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.ServerSocket;
    import java.net.Socket;

    public class ServerSock {

        public static void main(String[] args) throws Exception {

            System.out.println("Server is Started");
            ServerSocket ss = new ServerSocket(8888);

            Socket s = ss.accept();

            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            String str = br.readLine();

            System.out.println("Client Says : " + str);         
        }
    }

Here is my console after run client code, I am getting Exception Connection reset, where I am doing wrong?

这是运行客户端代码后的控制台,我正在重置异常连接,我做错了什么?

 Server is Started
Exception in thread "main" java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    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 com.socket.ServerSock.main(ServerSock.java:19)

回答by Seongmin Gwon

"Exception in thread "main" java.net.SocketException: Connection reset" error occurs when the opponent is forcibly terminated without calling close().

“线程“main”中的异常java.net.SocketException: Connection reset”是在没有调用close()的情况下强行终止对方时出现的错误。

Add this line to ClientSock

将此行添加到 ClientSock

skt.close();

and I recommend this too to ServerSock.

我也向 ServerSock 推荐这个。

ss.close();

java.io.Closeable implementing object must call close ().

java.io.Closeable 实现对象必须调用close()。

回答by oneat

You forgot "\n" in your "Hello Server".
Reader can't get the full line and throws this exception.

您在“Hello Server”中忘记了“\n”。
Reader 无法获取完整行并抛出此异常。