连接重置异常,原因不明
Connection reset exception, unknown reason
我有一个简单的客户端-服务器程序,但我仍然遇到连接重置异常。我对 internet/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)
您没有关闭服务器或客户端中的套接字。这会导致某些平台上的连接重置。
我有一个简单的客户端-服务器程序,但我仍然遇到连接重置异常。我对 internet/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)
您没有关闭服务器或客户端中的套接字。这会导致某些平台上的连接重置。