####代码
服务端:public class ServerSocket02 {
private static CopyOnWriteArrayList list=new CopyOnWriteArrayList();
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(4444);
System.out.println("服务端创建成功");
boolean isRunning=true;
while (isRunning){
Socket accept = serverSocket.accept();
Server02 server02 = new Server02(accept);
list.add(server02);
new Thread(server02).start();
}
}
static class Server02 implements Runnable{
private Socket socket;
private DataOutputStream out=null;
private DataInputStream in=null;
public String name;
private boolean isRunning=true;
public Server02(Socket socket){
this.socket=socket;
try {
out=new DataOutputStream(socket.getOutputStream());
in=new DataInputStream(socket.getInputStream());
name=receive();
System.out.println(name+"已连接");
otherSend(name+"已上线",true);
send("欢迎你"+name);
}catch (IOException e){
closes();
//e.printStackTrace();
}
}
@Override
public void run() {
while (isRunning){
String receive = receive();
if(!receive.equals("")) {//判断接收的内容是否为空
otherSend(receive,false);
}
}
}
//接收
public String receive(){
String mes="";
try {
mes=in.readUTF();
return mes;
} catch (IOException e) {
closes();
// e.printStackTrace();
}
return mes;
}
//发送
public void send(String mes){
try {
out.writeUTF(mes);
} catch (IOException e) {
closes();
// e.printStackTrace();
}
}
//群发
public void otherSend(String mes,boolean f){
//封装判断是否为私聊
boolean isPrivate = mes.startsWith("@");
if(isPrivate){ //判断是否为私聊
int ind = mes.indexOf("~");
String name=mes.substring(1,ind);
mes=mes.substring(ind+1);
for (Server02 server02 : list) {
if(server02.name.equals(name)){
server02.send(this.name+"@你说:"+mes);
break;
}
}
}else {
if(f){
mes="系统消息"+mes;
}else {
mes=this.name+":"+mes;
}
for (Server02 server02 : list) {
if(this==server02){
continue;
}
server02.send(mes);
}
}
}
public void closes(){
isRunning=false;
Utiles.closes(in,out,socket);
System.out.println(this.name+"已退出连接");
otherSend(this.name+"离开了",true);
list.remove(this);
}
}
}
客户端
public class Client01 {
public static void main(String[] args) throws IOException {
System.out.print("请输入姓名:");
String name=new BufferedReader(new InputStreamReader(System.in)).readLine();
Socket socket = new Socket("localhost", 4444);
// socket.getOutputStream().write(name.getBytes());
//创建写出数据的线程
new Thread(()->{
OutputStream outputStream = null;
DataOutputStream dataOutputStream=null;
try {
outputStream = socket.getOutputStream();
dataOutputStream = new DataOutputStream(outputStream);
dataOutputStream.writeUTF(name);//发送姓名
} catch (IOException e) {
e.printStackTrace();
}
try {
while (true) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
dataOutputStream.writeUTF(reader.readLine());
}
} catch (IOException e) {
//关闭资源 自定义一个工具类 //Closeable//所有需要释放的都实现了该接口
Utiles.closes(dataOutputStream,outputStream,socket);
e.printStackTrace();
}
}).start();
//创建读取数据的线程
new Thread(()->{
DataInputStream inputStream = null;
try {
inputStream = new DataInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
try {
while (true) {
System.out.println(inputStream.readUTF());
}
} catch (IOException e) {
Utiles.closes(inputStream,socket);
e.printStackTrace();
}
}).start();
}
}
工具类
public class Utiles {
public static void closes(Closeable ... closeables){
for (Closeable closeable : closeables) {
try {
System.out.println("已关闭资源");
closeable.close();
}catch (IOException i){
i.printStackTrace();
}
}
}
}



