import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class ChatServer {
public static void start(int port){
try {
ServerSocket server = new ServerSocket(port);
List<Socket> clients = new ArrayList<Socket>();
System.out.println("聊天室创建成功,等待成员加入!");
Socket client = null;
while(true){
client = server.accept();
clients.add(client); //加入客户端列表
//创建一个监听线程,接受客户端的消息并转发
new ServerListener(client, clients).start();
System.out.println("聊天室在线人数:"+clients.size());
}
} catch (IOException e) {
System.out.println("聊天室服务创建失败!");
e.printStackTrace();
}
}
public static void main(String[] args) {
ChatServer.start(Integer.parseInt(args[0])); //启动聊天服务
}
}
class ServerListener extends Thread{
private Socket client;
private List<Socket> clients;
public ServerListener(Socket client, List<Socket> clients) {
this.client = client;
this.clients = clients;
}
@Override
public void run() {
BufferedReader br = null;
PrintWriter pw = null;
PrintWriter clientspw = null;
if(!client.isClosed()&&client.isConnected()){
try {
br = new BufferedReader(new InputStreamReader(client.getInputStream()));
pw = new PrintWriter(client.getOutputStream());
String welcome = "欢迎"+client.getInetAddress().getHostName()+"加入聊天室!";
System.out.println(welcome);
pw.println(welcome);
pw.flush();
while(true){
if(!client.isClosed()&&client.isConnected()){
String message = client.getInetAddress().getHostName() + " 说:" + br.readLine(); //读取用户发来的消息
System.out.println(message);
for(Socket s : clients){
if(!client.getInetAddress().getHostAddress().equals(s.getInetAddress().getHostAddress())){
clientspw = new PrintWriter(s.getOutputStream());
clientspw.println(message);
clientspw.flush();
}
}
}
else{
clients.remove(client);
}
}
} catch (IOException ie) {
System.out.println("用户"+client.getInetAddress().getHostName()+"退出聊天室!");
try{
for(Socket s : clients){
if(!client.getInetAddress().getHostAddress().equals(s.getInetAddress().getHostAddress())){
clientspw = new PrintWriter(s.getOutputStream());
clientspw.println("用户"+client.getInetAddress().getHostName()+"退出聊天室!");
clientspw.flush();
}
}
}catch(IOException e){
e.printStackTrace();
}
clients.remove(client);
} finally{
try {
if(br!=null) br.close();
if(pw!=null) pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}else{
clients.remove(client);
}
}
}
hanwei19880207
- 粉丝: 7
- 资源: 3
最新资源
- order system final.c
- 2024注册测绘师考试讲义-大地测量与海洋测绘
- SAP ECC到SAP S4 HANA系统的对比分析(PP模块).xlsx
- Java small game (Java桌面版小游戏)
- 工程经济学自考必备软件下载
- (176647222)基于遗传算法(GA)优化门控循环单元(GA-GRU)的数据分类预测 优化参数为学习率,隐藏层节点个数,正则化参数,matla
- (176685204)基于遗传算法优化BP神经网络(GA-BP)的时间序列预测,matlab代码 模型评价指标包括:R2、MAE、MSE、RMSE
- (176724010)遗传算法(GA)优化随机森林(RF)的分类预测,GA-RF分类预测模型,多输入单输出模型 多特征输入单输出的二分类及多分类模
- 基于vc2010+easyx的贪吃蛇源码
- ieee-p1687-internal-jtag-taps-embedded-instrumentation-white-paper.pdf
- IHI0024D_amba_apb4_protocol_spec.pdf
- 2024注册测绘师《综合能力》讲义-大地测量(2)
- amba_axi4.pdf
- 2024注册测绘师《综合能力》讲义:大地测量中水准网与重力网的布设及技术规范
- 2024注册测绘师《综合能力》讲义-第2章海洋测绘(1)
- 从Python开发到打包成EXE可执行文件的辅助工具分享
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
前往页