gpt4 book ai didi

java - 关闭服务器套接字

转载 作者:太空宇宙 更新时间:2023-11-04 12:00:38 26 4
gpt4 key购买 nike

我正在尝试关闭服务器,但我收到:

 java.net.SocketException: Socket closed    at java.net.PlainSocketImpl.socketAccept(Native Method)    at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:398)    at java.net.ServerSocket.implAccept(ServerSocket.java:530)    at java.net.ServerSocket.accept(ServerSocket.java:498)    at THREAD.MioServer.AvviaServer(MioServer.java:21)    at THREAD.Avviamento.run(Avviamento.java:16)    at java.lang.Thread.run(Thread.java:745)

This is my code:

package THREAD;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.*;
import java.util.Scanner;

public class MioServer {
public int port;

public MioServer(int port) {
super();
this.port = port;
}
ServerSocket serverSocket=null;
Socket socket=null;

public void AvviaServer() throws IOException {
serverSocket=new ServerSocket(port);
System.out.println("serverSocket partito "+ port);
socket=serverSocket.accept();
System.out.println("socket partita");
Scanner in=new Scanner(socket.getInputStream());
PrintWriter out=new PrintWriter(socket.getOutputStream());
while (true) {
String line=in.nextLine();
if (line=="quit") {
break;
}

}
}
public void ferma() throws IOException {
if (socket==null); {
socket=new Socket();
socket.close();
System.out.println("cretino");
serverSocket.close();
}
if (socket!=null) {
socket.close();
socket=null;
serverSocket.close();
System.out.println("chiuso server(?)");
serverSocket=null;
}
}
}

package THREAD;

import java.io.IOException;

public class Avviamento implements Runnable{
public MioServer server;

public Avviamento(MioServer server) {
super();
this.server = server;
}

@Override
public void run() {
try {
server.AvviaServer();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}



package THREAD;

import java.io.IOException;

public class Fermamento implements Runnable{
public MioServer server;

public Fermamento(MioServer server) {
super();
this.server = server;
}

@Override
public void run() {
try {
server.ferma();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

听众:

package LISTENERS;

import java.awt.event.*;
import java.io.IOException;

import javax.swing.*;

import THREAD.*;

public class MioListener implements ActionListener {
public int port=3000;
public MioServer server=new MioServer(3000);

@Override
public void actionPerformed(ActionEvent e) {
JButton b=(JButton) e.getSource();
Avviamento avvio=new Avviamento(server);
Thread avviamento=new Thread(avvio);
Fermamento ferma=new Fermamento(server);
Thread fermamento=new Thread(ferma);
if (b.getText().equals("Avvia")) {
System.out.println("Tasto avvia premuto");
avviamento.start();
}
if (b.getText().equals("Stop")) {
System.out.println("Tasto stop premuto");
fermamento.start();
}
}

感谢您的帮助。

最佳答案

I'm trying to close a server but i receive:

java.net.SocketException: Socket closed

毫无疑问,当 Fermamento 线程的 server.ferma() 关闭 serverSocket 时,您的服务器线程会抛出异常,就像从服务器脚下拉地毯一样。

您可以简单地使用以下内容捕获异常:

    try { socket=serverSocket.accept(); } catch (SocketException e) { return; }

关于java - 关闭服务器套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40947670/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com