ServerSocket群发问题
while(true)这个程序我实现了多个客户端向服务器发送信息的功能,但是在向由服务器向多个客户端实现群发时出了问题,不能实现群发,只能向单个客户端发送信息,也就是说,由服务器发出的信息,只有一个客户端收到,接收到信息的客户端是随机的,不是固定,我要服务器发出信息,其他所有客户端都同时接到信息,该怎么做呢?这些是群发的代码,我个人觉得这出了问题,向高手求教,求解
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
for(int i = 0;i<vlist.size();i++){
Socket s = (Socket)vlist.get(i);
OutputStream os = s.getOutputStream();
String line = "服务器说:"+reader.readLine()+"--------"+s;
os.write(line.getBytes());
}
}
public class ServerOutputThread extends Thread
{
private Socket socket;
Vector vlist = new Vector();
public ServerOutputThread(Socket socket,Vector v)
{
this.socket = socket;
this.vlist = v;
}
@Override
public void run()
{
try
{
while(true)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
for(int i = 0;i<vlist.size();i++){
Socket s = (Socket)vlist.get(i);
OutputStream os = s.getOutputStream();
String line = "服务器说:"+reader.readLine()+"--------"+s;
os.write(line.getBytes());
os.flush();
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
String line = "服务器说:"+reader.readLine()+"--------"+s;线程阻塞了