我收到以下错误:
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上测试过,一切正常。
答案 0 :(得分:1)
您的测试程序在不关闭套接字的情况下退出。这会导致重置连接(TCP RST),而不是发出有序关闭(TCP FIN)。在某些平台上。
另一个问题是你正在阅读客户端中的行,但是你没有发送一行。您需要发送一个行终止符。
答案 1 :(得分:0)
尝试任何一个
readLine()
方法查找以换行符\n
结尾的完整行或字符串。
使用包含Scanner
和hasNextLine()
方法的nextLine()
。
尝试任何一个
在邮件中添加换行符\n
,例如ok\n
。
使用PrintWriter
代替BufferedWriter
并使用println()
方法自动在邮件中附加新的换行符。使用auto-flush
的 PrintWriter
属性,以避免手动调用flush()
方法。
完整的示例代码:
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();
}
}
}