我正在为学校编写服务器/客户端应用程序。
每次关闭客户端时,服务器都会抛出' java.net.SocketException:Connection reset'。我想,我必须在关闭JavaFX窗口之前关闭客户端中的套接字,但在哪里?或者你怎么看?
我已经看过类似的问题,但我不知道如何在我的程序中正确实现它。
例外:
20:05:27.132 [Thread-2] ERROR OnlineBank - java.net.BindException: Address already in use: JVM_Bind
19:51:06.580 [Thread-1] ERROR ServerHandler - java.net.SocketException: Connection reset
19:51:06.580 [Thread-2] ERROR ServerHandler - java.net.SocketException: Connection reset
MyProgramm:
private MyProgramm() {
server = new MyServer(Constants.REMOTE_PORT);
try
server.start();
} catch (ClassNotFoundException | IOException e) {
logger.error(e);
}
}
MyServer的:
public void start() throws ClassNotFoundException, IOException {
try {
serverSocket = new ServerSocket(this.port);
while (true) {
Socket client = serverSocket.accept();
logger.debug(getClass().getName() + " accepts");
Thread threadHandler = new Thread(new ServerHandler(client));
threadHandler.start();
}
} finally {
if (serverSocket != null) {
serverSocket.close();
}
}
}
ServerHandler:
@Override
public void run() {
try (InputStream in = clientSocket.getInputStream();
OutputStream out = clientSocket.getOutputStream();
ObjectInputStream oin = new ObjectInputStream(in);
ObjectOutputStream oos = new ObjectOutputStream(out)) {
while (true) {
try {
Object receivedObject = oin.readObject();
handleReceivedObject(oos, receivedObject);
} catch (ClassNotFoundException e) {
logger.error(e);
break;
}
}
} catch (IOException e) {
logger.error(e);
}
}
MainClient:
public class MainClient extends Application {
private static Client client;
public static void main(String[] args) {
launch(args);
try {
client = new Client();
} catch (IOException e) {
logger.error(e);
}
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/welcome.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
@Override
public void stop() throws Exception {
super.stop();
// to avoid NullPointerException
if (Client != null && Client.getSocket() != null) {
Client.getSocket().close();
}
}
}
客户端:
public class Client {
private Socket socket;
public Client(String remoteHost, int port) throws UnknownHostException, IOException {
if (getSocket() == null) {
socket = new Socket(remoteHost, port);
}
}
public Client() throws UnknownHostException, IOException {
this(Constants.REMOTE_HOST, Constants.REMOTE_PORT);
}
public Socket getSocket() {
return socket;
}
}
答案 0 :(得分:1)
你是对的。您必须在退出客户端之前正确关闭客户端套接字。否则,操作系统可能会重置连接而不是关闭连接。不是Android的人我不能告诉你这个闭合的位置。
您的服务器也有问题。它需要在读取循环中捕获EOFException
并静默地中断。目前您没有正确处理流的结尾:您将通过catch (EOFException )
捕获它并将其记录为错误,但事实并非如此。