关闭服务器套接字

时间:2016-12-03 12:20:10

标签: java sockets serversocket

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

 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)

这是我的代码:

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();
        }
    }

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

  

我试图关闭服务器,但收到了:

java.net.SocketException: Socket closed

毫无疑问,当Fermamento线程server.ferma()关闭serverSocket时,您的服务器线程会引发异常,准确地从地下拉出地毯服务器的脚。

您可以通过以下方式捕获异常:

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