网络编程聊天室---------服务器线程类

/**
* 服务器聊天类
* @author Administrator
*/
public class ServerThread extends Thread {

//List集合用于保存每一个连接本服务器的客户端Socket对象
public static List<Socket>list = new ArrayList<Socket>();
public Socket client;

public ServerThread() {
super();
}


public ServerThread(Socket client) {
super();
this.client = client;
}


/**
* run方法
*/
@Override
public void run() {
try {
//读取客户端发送的信息
InputStream in = client.getInputStream();
byte[] b = new byte[1024];
int len = in.read(b);
System.out.println(new String(b));
//通过循环遍历把读取到的信息发送到每一个客户端
for (Socket clien : list) {
OutputStream os = clien.getOutputStream();
os.write(b,0,len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

posted @ 2017-06-29 10:08  云端观云  阅读(183)  评论(0编辑  收藏  举报