服务端接受连接是建立处理线程,并将此线程加入队列中
[code]
public void run() {
try {
int i = 1;
while (true) {
Socket client = listen.accept();
System.out.println("Thread...." + i); // count spawn
ThreadedEchoHandler r = new ThreadedEchoHandler(this,client, i);
r.start();
i++;
// 添加到客户端队列中
connections.addElement(r);
}
} catch (IOException e) {
e.printStackTrace();
}
}
[/code]
当服务器接受到客户端消息的时候广播发送信息
[code]
public void broadCast(String msg) throws IOException {
int i;
for (i = 0; i < connections.size(); i++) {
ThreadedEchoHandler thread = (ThreadedEchoHandler) connections.elementAt(i);
//向客户端输出消息
thread.out.writeUTF("BroadCast:"+"Client "+thread.counter+":"+msg);
System.out.println(thread.counter);
}
}
[/code]
客户端接受数据
[code]
while (true) {
String str = wt.readLine();
out.writeUTF("client send:" + str);
if (str.equals("end")) {
break;
}
System.out.println(in.readUTF());
}
[/code]
问题就在这里,此循环里面System.out.println(thread.counter);可以正常输出,但是writeUTF没有正常执行,测试结果只有刚刚发送消息的那个客户端接受到回应,其他的就没了,请教大家,帮忙看看
[code]
public void run() {
try {
int i = 1;
while (true) {
Socket client = listen.accept();
System.out.println("Thread...." + i); // count spawn
ThreadedEchoHandler r = new ThreadedEchoHandler(this,client, i);
r.start();
i++;
// 添加到客户端队列中
connections.addElement(r);
}
} catch (IOException e) {
e.printStackTrace();
}
}
[/code]
当服务器接受到客户端消息的时候广播发送信息
[code]
public void broadCast(String msg) throws IOException {
int i;
for (i = 0; i < connections.size(); i++) {
ThreadedEchoHandler thread = (ThreadedEchoHandler) connections.elementAt(i);
//向客户端输出消息
thread.out.writeUTF("BroadCast:"+"Client "+thread.counter+":"+msg);
System.out.println(thread.counter);
}
}
[/code]
客户端接受数据
[code]
while (true) {
String str = wt.readLine();
out.writeUTF("client send:" + str);
if (str.equals("end")) {
break;
}
System.out.println(in.readUTF());
}
[/code]
问题就在这里,此循环里面System.out.println(thread.counter);可以正常输出,但是writeUTF没有正常执行,测试结果只有刚刚发送消息的那个客户端接受到回应,其他的就没了,请教大家,帮忙看看