package com.wangbin.timu5;
import java.io.*;
import java.net.Socket;
import java.util.Set;
public class ThreadDemo implements Runnable{
private Socket a;
private static int i=0;
private String name;
public ThreadDemo(Socket a,String name) {
this.a = a;
this.name=name;
}
@Override
public void run() {
BufferedWriter bw = null;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(a.getInputStream()));
// while (true){
Set<String> strings1 = ServerDemo.hm.keySet();
for(String sss:strings1){
if(!sss.equals(this.name)){
Socket socket = ServerDemo.hm.get(sss);
bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write(this.name+"已上线");
bw.newLine();
bw.flush();
}
}
String s;
// 别的客户端发送消息
while ((s = br.readLine()) != null) {
if(s.equals("exit")){
bw.write(this.name+"退出聊天室");
bw.newLine();
bw.flush();
break;
}
// 像别的客户端发送消息
// 定义一个参数判断@的人存不存在
boolean flag=false;
Set<String> strings = ServerDemo.hm.keySet();
for(String sss:strings){
// 单独发送
// 判断是否有私聊情况
if(s.indexOf("@")==0){
String use = s.substring(1);
// 测试
if(use.substring(0,sss.length()).equals(sss)){
Socket socket = ServerDemo.hm.get(sss);
bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write(this.name+"给你偷偷发了一句>>");
//获取@+姓名后面的信息
String ss = s.substring(1+sss.length());
bw.write(ss);
bw.newLine();
bw.flush();
}
}else if(!sss.equals(this.name)){
Socket socket = ServerDemo.hm.get(sss);
bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write(this.name+":");
bw.write(s);
bw.newLine();
bw.flush();
}
}
System.out.println("来自" + this.name+ "的消息>>" + s);
}
System.out.println("客户端" + this.name+"退出聊天室");
} catch (IOException e) {
// e.printStackTrace();
System.out.println("有一位用户下线");
}finally {
try {
a.close();
assert bw != null;
bw.close();
} catch (IOException e) {
// e.printStackTrace();
System.out.println("未知错误");
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Java作业网络编程.zip (49个子文件)
Java作业网络编程
out
production
work
com
wangbin
timu1
Server.class 2KB
Client.class 2KB
timu2
Server.class 2KB
Client.class 2KB
timu3
ThreadDemo.class 2KB
Client1.class 2KB
ServerDemo.class 841B
Client2.class 2KB
timu5
Client3.class 2KB
ClientThreadDemoRead.class 1KB
ThreadDemo.class 4KB
Client1.class 2KB
ServerDemo.class 2KB
Client2.class 2KB
timu4
Client3.class 2KB
ClientThreadDemoRead.class 1KB
ThreadDemo.class 3KB
Client1.class 2KB
ServerDemo.class 2KB
Client2.class 2KB
.idea
uiDesigner.xml 9KB
workspace.xml 8KB
misc.xml 263B
modules.xml 405B
Java作业网络编程.iml 344B
.gitignore 251B
hs_err_pid73100.log 4KB
work
src
com
wangbin
timu1
Client.java 1KB
Server.java 1KB
timu2
Client.java 1KB
Server.java 1KB
timu3
Client2.java 1KB
ThreadDemo.java 1KB
ServerDemo.java 476B
Client1.java 1KB
timu5
Client2.java 1KB
ThreadDemo.java 4KB
ServerDemo.java 954B
Client3.java 2KB
ClientThreadDemoRead.java 805B
Client1.java 2KB
timu4
Client2.java 1KB
ThreadDemo.java 3KB
ServerDemo.java 898B
Client3.java 2KB
ClientThreadDemoRead.java 728B
Client1.java 2KB
work.iml 433B
hs_err_pid174752.log 10KB
共 49 条
- 1
资源评论
一只java小菜鸡
- 粉丝: 2690
- 资源: 145
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功