连接重置异常,原因不明

时间:2017-03-05 21:04:16

标签: java sockets connection-reset

我有一个简单的客户端 - 服务器程序,但我仍然得到连接重置异常。我对互联网/ SO的简短研究无法得出我的代码中的错误

public class SocketServer {

    public static void main (String [] args) throws Exception{
        ServerSocket ss = new ServerSocket (11060);
        Socket s = ss.accept();
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String str = br.readLine();
        System.out.println("Client Data :" + str);
    }
}

public class SocketClient {

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

        String ipAddress = "localhost" ;
        int port = 11060;

        Socket s = new Socket (ipAddress, port);

        String str = "Hello World";

        OutputStreamWriter osw = new OutputStreamWriter(s.getOutputStream());
        PrintWriter pw = new PrintWriter (osw);
        osw.write(str);
        osw.flush();
    }
}

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 tanvi.SocketServer.main(SocketServer.java:14)

1 个答案:

答案 0 :(得分:2)

您无法关闭服务器或客户端中的套接字。这将导致某些平台上的连接重置。