菜鸟遇到了小问题 关于List的, 求教 你那个clients没有声明呀,那来的?ChatSever和Client是两个不同的类呀! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 但是Client是内部类啊 而且我定义的是成员变量List<Client> clients = new ArrayList<Client>(); 应该可以才对啊 我在好好看看 谢啦! 你的不是内部类。吧 } class Client implements Runnable{}上的}移到最下面就好了 这就是内部类了。什么逻辑。一个客户端发来请求,要响应所有的客服端啊 绝对不是内部类,你将客户端类剪切掉,没有报错,说明你定义在了服务器端类外面,所以引用会失败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(); } } } }}这才是内部类,没有报错! 但是Client是内部类啊 而且我定义的是成员变量List<Client> clients = new ArrayList<Client>(); 应该可以才对啊 我在好好看看 谢啦!内部类要在外部类里面声明的,你那不是内部类! java多线程生产者消费者,高手指点一下 sql取出的数据与java字符串数据比较问题 【十万火急】正则表达式的问题 ear 部署在weblogic的问题(jsp compiler error) 谢谢 关于java的内部类问题 jbuilder中如何实现使打开的一个新的Frame窗口最大化??? 请问我这个程序应该怎样使用线程? 如何比较时间大小,,请看!! JDK1.3 下命令行“java -cp ... ”超过256个字符怎么办? ERP和CRM的区别,有谁知道? 请教高手一个Swing程序的修改方法. 为何简单变量不能放在synchronized括号中?
class Client implements Runnable{
}
上的}移到最下面就好了 这就是内部类了。
什么逻辑。一个客户端发来请求,要响应所有的客服端啊
你将客户端类剪切掉,没有报错,说明你定义在了服务器端类外面,所以引用会失败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();
}
}
}
}
}这才是内部类,没有报错!
内部类要在外部类里面声明的,你那不是内部类!