/**
* 服务器聊天类
* @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();
}
}
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-10
  • 2022-01-12
  • 2021-05-12
  • 2021-06-07
猜你喜欢
  • 2021-12-24
  • 2022-12-23
  • 2021-10-26
  • 2021-04-20
  • 2021-07-14
  • 2021-08-03
  • 2022-12-23
相关资源
相似解决方案