你那个clients没有声明呀,那来的?ChatSever和Client是两个不同的类呀!

解决方案 »

  1.   

    但是Client是内部类啊 而且我定义的是成员变量List<Client> clients = new ArrayList<Client>(); 应该可以才对啊 我在好好看看 谢啦!
      

  2.   

    你的不是内部类。吧 }
    class Client implements Runnable{
    }
    上的}移到最下面就好了 这就是内部类了。
    什么逻辑。一个客户端发来请求,要响应所有的客服端啊
      

  3.   

    绝对不是内部类,
    你将客户端类剪切掉,没有报错,说明你定义在了服务器端类外面,所以引用会失败package udp.demo;import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.EOFException;
    import java.io.IOException;
    import java.net.*;
    import java.util.*;public class ChartServer {
    ServerSocket ss = null;
    boolean started = false; List<Client> clients = new ArrayList<Client>(); public static void main(String[] args) {
    new ChartServer().start();
    } public void start() {
    try {
    ss = new ServerSocket(8888);
    started = true;
    } catch (BindException e) {
    System.out.println("端口使用中...");
    System.out.println("请关闭相关程序, 重新启动程序!");
    System.exit(0);
    } catch (IOException e) {
    e.printStackTrace();
    }
    try {
    while (started) {
    Socket s = ss.accept();
    Client client = new Client(s);
    clients.add(client);
    new Thread(client).start();
    System.out.println("socket a is connected");
    }
    } catch (EOFException e) {
    System.out.println("Socket is closed");
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    ss.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    } class Client implements Runnable {
    private boolean bconnected = false;
    private Socket s;
    private DataInputStream dis = null;
    private DataOutputStream dos = null; public Client(Socket s) {
    this.s = s;
    try {
    this.dis = new DataInputStream(s.getInputStream());
    this.dos = new DataOutputStream(s.getOutputStream());
    bconnected = true;
    } catch (IOException e) {
    e.printStackTrace();
    }
    } public void send(String str) {
    try {
    dos.writeUTF(str);
    } catch (IOException e) {
    e.printStackTrace();
    }
    } public void run() {
    try {
    while (bconnected) {
    String string = dis.readUTF();
    System.out.println(string);
    for (int i = 0; i < clients.size(); i++) {
    Client c = clients.get(i);// 这里的clients出现clients cannot
    // be resolved实在是没看懂 运行的时候又
    // 没报错 求教 感谢!
    c.send(string);
    }
    }
    } catch (EOFException e) {
    System.out.println("Client is closed!");
    } catch (IOException e) {
    e.printStackTrace();
    } finally { try {
    if (dis != null) {
    dis.close();
    }
    if (s != null) {
    s.close();
    }
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    }
    }
    }
    }这才是内部类,没有报错!
      

  4.   

    但是Client是内部类啊 而且我定义的是成员变量List<Client> clients = new ArrayList<Client>(); 应该可以才对啊 我在好好看看 谢啦!
    内部类要在外部类里面声明的,你那不是内部类!