package lts;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JTextArea;
public class ClinetRecive extends Thread // 客户端消息接收线程,处理消息的接收
{
//属性
Socket socket;
String mess;
client client;
JTextArea showmess;
//构造方法
public ClinetRecive(Socket socket,JTextArea showmess) //Client类给你传的一个socket(来自服务端的socket)
{
this.socket = socket;
this.showmess=showmess;
}
//run方法
@Override
public void run()
{
while (true) // 不知道什么时候需要接收消息,不断循环等待
{
// System.out.println("客户端接收消息");
try {
//数据来了 请接收
InputStream is = socket.getInputStream();
//字节流转字符缓冲流
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//读取数据
mess=br.readLine();
//===========================================================
Date d=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
String t=sdf.format(d);
//============================================================
//做一个判断,是服务器还是客户端发来的
if(mess.indexOf("#")!=-1){
//如果是服务器发过来的消息
String s1[] = mess.split("#");//如果含有#号就以#分割
String message = s1[0];
showmess.append("系统消息"+" "+t+"\n"+" "+message+"\n");//两个接收-客户端发送来的接收-服务器发送来的接收 显示到客户端的文本区域上
}else{
//判断消息的类型
if(mess.indexOf("//")!=-1){
//如果是客户端发过来的消息 就含有"//"
String s3[]=mess.split("//");
String name = s3[0];
String message=s3[1];
//判断断开连接
if(message.indexOf("【断开连接】")!=-1){
showmess.append("系统消息"+" "+t+"\n"+" "+name+message+"\n");
//关闭用户端的输入流、socket、clientrecive线程
}else{
showmess.append(name+" "+t+"\n"+" "+message+"\n");
}
}else{//如果是某某连接成功消息发过来 **要做一个判断**
showmess.append("系统消息"+" "+t+"\n"+" "+mess+"\n");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//结束
}
最近下载更多
yuchunxing LV1
2023年9月2日
青柠编程 LV13
2023年4月18日
referrer LV1
2022年12月25日
qqyx777 LV1
2022年6月27日
微信网友_6003487859068928 LV5
2022年6月15日
123456qiqiqi LV1
2022年5月27日
tdcq123 LV14
2022年3月13日
bbbookworm LV1
2022年2月19日
weny_QAQ LV2
2021年12月22日
Sunknown LV1
2021年11月30日
最近浏览更多
13133117021 LV5
2024年12月24日
3263394665 LV9
2024年8月31日
周鸣郝 LV2
2024年5月26日
krispeng LV15
2024年4月16日
dapeng0011 LV15
2024年2月25日
陈小灏 LV18
2023年12月27日
微信网友_6762641760833536
2023年12月4日
暂无贡献等级
yuchunxing LV1
2023年9月2日
fuyouou LV5
2023年6月29日
CL200228 LV4
2023年5月5日

