`
dzgwt2004
  • 浏览: 165039 次
  • 来自: 浙江杭州
社区版块
存档分类
最新评论

Socket广播问题请教大家?急!

    博客分类:
  • java
阅读更多
服务端接受连接是建立处理线程,并将此线程加入队列中
	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();
		}

	}


当服务器接受到客户端消息的时候广播发送信息
	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);
		}
	}

客户端接受数据
while (true) {
			String str = wt.readLine();
			out.writeUTF("client send:" + str);
			if (str.equals("end")) {
				break;
			}
			System.out.println(in.readUTF());
		}

问题就在这里,此循环里面System.out.println(thread.counter);可以正常输出,但是writeUTF没有正常执行,测试结果只有刚刚发送消息的那个客户端接受到回应,其他的就没了,请教大家,帮忙看看
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics