简体   繁体   English

不能访问类型为...的封闭实例

[英]No enclosing instance of type … is accessible

For educational purposes I tried to make a server and client where the server receives data from multiple clients and echoes each message. 出于教育目的,我尝试创建服务器和客户端,服务器从多个客户端接收数据并回显每条消息。 The problem is when I try to make the server send the echo to all of the clients at once. 问题是当我尝试让服务器立即向所有客户端发送回声时。

public class SocketServer {
  ArrayList<MyRunnable> ts = new ArrayList<MyRunnable>();
  ServerSocket serv;
  static MainServerThread mst = new MainServerThread();
//                                  ^ IDE(eclipse) underlines this as the problem
  SocketServer() {
    EventQueue.invokeLater(mst);
  }

  public static void main(String[] args) { 
    Thread tr = new Thread(mst);
    tr.start();
  }

  void send(String s) {
    for (int i = 0; i < ts.size(); i++) {
      MyRunnable tmp = ts.get(i);
      tmp.sendToClient(s);
    }
  }

  class MainServerThread implements Runnable {
    public void run() {
      try {
        serv = new ServerSocket(13131);
        boolean done = false;
        while (!done) {
          Socket s = serv.accept();
          MyRunnable r = new MyRunnable(s);
          Thread t = new Thread(r);
          ts.add(r);
          t.start();
        }
      } catch(Exception e) {
        e.printStackTrace();
      }
    }
  }

  class MyRunnable implements Runnable {
    Socket sock;
    PrintWriter out;
    Scanner in;
    MyRunnable(Socket soc) {
      sock = soc;
    }

    public void run() {
      try {
        try {
          out = new PrintWriter(sock.getOutputStream(), true);
          in = new Scanner(sock.getInputStream());
          boolean done = false;
          while (!done) {
            String line = in.nextLine();
            send("Echo: " + line);
            System.out.println("Echo: " + line);
            if (line.trim().equals("BYE")) done = true;
          }
        } finally {
          sock.close();
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    public void sendToClient(String s) {
      out.println(s);
    }
  }
}    

I have searched for and answer and saw many similar questions, but none of them helped me. 我搜索并回答并看到了很多类似的问题,但没有一个能帮助我。 Hope you can point out my mistake. 希望你能指出我的错误。 Thanks in advance. 提前致谢。

Your nested class requires an instance of the outer class, because it's not static - but you don't have an instance of the outer class. 您的嵌套类需要外部类的一个实例,因为它不是静态的-但你没有外部类的一个实例。

Try making both of your nested classes static . 尝试将两个嵌套类static It doesn't look like they need a reference to the outer class anyway. 看起来他们不需要引用外部类。

In fact, I'd be tempted to avoid using nested classes at all for this - while nested classes can certainly be useful sometimes, they have various corner cases, and it's typically cleaner to create separate top-level types. 事实上,我总是试图避免使用嵌套类 - 虽然嵌套类有时候肯定是有用的,它们有各种各样的极端情况,而且创建单独的顶级类型通常更简洁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM