天天看點

網絡程式設計聊天室---------伺服器線程類

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;

}

@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();

}

}

}

轉載于:https://www.cnblogs.com/yueguanguanyun/p/7092981.html