我在Java Socket中遇到意外错误

时间:2014-08-08 19:43:47

标签: java sockets exception serversocket

我收到以下错误:

java.net.SocketException: Connection reset
  at java.net.SocketInputStream.read(SocketInputStream.java:189)
  at java.net.SocketInputStream.read(SocketInputStream.java:121)
  at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
  at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
  at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
  at java.io.InputStreamReader.read(InputStreamReader.java:184)
  at java.io.BufferedReader.fill(BufferedReader.java:161)
  at java.io.BufferedReader.readLine(BufferedReader.java:324)
  at java.io.BufferedReader.readLine(BufferedReader.java:389)
  at test.SocketTest.main(SocketTest.java:26)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:483)
  at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

我不知道为什么会得到它。有什么想法吗?

我正在使用此代码:

客户

public class SocketTest {

public static void main(String[] args) {
    try {
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 5000);
        ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        Request request = new Request();
        request.setInputId("user_1233423333");
        request.setOperation(3);
        outputStream.writeObject(request);
        outputStream.flush();
        String s;
        while ((s = reader.readLine()) != null) {
            System.out.println(s);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

和服务器:

public class ServerSocketTest {

public static void main(String[] args) {
    try {
        ServerSocket serverSocket = new ServerSocket(5000);
        Socket socket = serverSocket.accept();
        ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        Request request = (Request) inputStream.readObject();
        System.out.println(request.getInputId());
        System.out.println(request.getOperation());
        writer.write("ok");
        writer.flush();
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    }
}

}

我认为这些代码应该可行,但我不知道为什么我会收到此错误。伙计们,不管怎么说?

编辑:

OS = Windows 8.1 64位

我在我的Ubuntu上测试过,一切正常。

2 个答案:

答案 0 :(得分:1)

您的测试程序在不关闭套接字的情况下退出。这会导致重置连接(TCP RST),而不是发出有序关闭(TCP FIN)。在某些平台上。

另一个问题是你正在阅读客户端中的行,但是你没有发送一行。您需要发送一个行终止符。

答案 1 :(得分:0)

客户端

尝试任何一个

  1. readLine()方法查找以换行符\n结尾的完整行或字符串。

  2. 使用包含ScannerhasNextLine()方法的nextLine()

  3. 服务器端

    尝试任何一个

    1. 在邮件中添加换行符\n,例如ok\n

    2. 使用PrintWriter代替BufferedWriter并使用println()方法自动在邮件中附加新的换行符。使用auto-flush PrintWriter 属性,以避免手动调用flush()方法。

    3. 完整的示例代码:

      ServerSocketTest.java

      public class ServerSocketTest {
      
          public static void main(String[] args) {
              try {
                  ServerSocket serverSocket = new ServerSocket(5000);
                  Socket socket = serverSocket.accept();
                  ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
                  PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),
                          true);
                  System.out.println(inputStream.readObject());
                  writer.println("ok");
              } catch (IOException | ClassNotFoundException e) {
                  e.printStackTrace();
              }
          }
      }
      

      SocketTest.java

      public class SocketTest {
      
          public static void main(String[] args) {
              try {
                  Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 5000);
                  ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
                  Scanner scanner = new Scanner(new InputStreamReader(socket.getInputStream()));
                  outputStream.writeObject("Hello");
                  String s;
                  while (scanner.hasNextLine()) {
                      System.out.println(scanner.nextLine());
                  }
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      
      }