笔记:控制台聊天室
package com.xuedao.pojo;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
//服务端
public class Sever {
//用户集合
private List<Socket> clis = new ArrayList<Socket>();
public static void main(String[] args) throws IOException {
new Sever().run();
}
public void run() throws IOException {
ServerSocket ser =new ServerSocket(9999);
while(true) {
Socket cli = ser.accept();
clis.add(cli);
new Thread(new UserRunner(cli, clis)).start();
}
}
}
package com.xuedao.pojo;
//客户端
import java.io.IOException;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
try {
Socket cli = new Socket("127.0.0.1",9999);
new Thread(new SendMsg(cli)).start();
new Thread(new GetMsg(cli)).start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.xuedao.pojo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import com.xuedao.utils.CloseStream;
//发消息的线程
public class SendMsg implements Runnable{
private Socket cli;
private BufferedWriter out;
private BufferedReader in;
private boolean lock = true;
public SendMsg(Socket cli) {
try {
this.cli = cli;
out = new BufferedWriter(new OutputStreamWriter(cli.getOutputStream()));
in = new BufferedReader(new InputStreamReader(System.in));
} catch (IOException e) {
CloseStream.close(out,in);
lock = false;
System.err.println("初始化发送消息线程异常");
}
}
//接收消息
public String getMsg() {
try {
return in.readLine();
} catch (IOException e) {
CloseStream.close(in);
lock = false;
System.err.println("接收控制台消息异常");
return null;
}
}
//发送消息
public void sendMsg(String msg) {
if(msg == null || msg.equals("")) {
return;
}
try {
out.write(msg);
out.flush();
out.newLine();
out.flush();
} catch (IOException e) {
CloseStream.close(out);
lock = false;
System.err.println("发送消息线程异常");
}
}
//循环执行
@Override
public void run() {
while(lock) {
sendMsg(getMsg());
}
}
}
package com.xuedao.pojo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import com.xuedao.utils.CloseStream;
//接收消息线程
public class GetMsg implements Runnable{
private Socket cli;
private BufferedReader in;
private boolean lock = true;
public GetMsg(Socket cli) {
try {
this.cli = cli;
in =new BufferedReader(new InputStreamReader(cli.getInputStream()));
} catch (IOException e) {
CloseStream.close(in);
lock = false;
System.out.println("接收消息线程初始化异常");
}
}
//接收消息
public String getMsg() {
try {
return in.readLine();
} catch (IOException e) {
CloseStream.close(in);
lock = false;
System.out.println("接收消息服务器异常");
return null;
}
}
@Override
public void run() {
while(lock) {
System.out.println("收到消息:"+getMsg());
}
}
}
package com.xuedao.pojo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.List;
import com.xuedao.utils.CloseStream;
public class UserRunner implements Runnable{
private Socket cli;
private List<Socket> clis;
private BufferedWriter out;
private BufferedReader in;
public UserRunner(Socket cli, List<Socket> clis) {
this.cli = cli;
this.clis = clis;
try {
in = new BufferedReader(new InputStreamReader(cli.getInputStream()));
} catch (IOException e) {
CloseStream.close(in);
clis.remove(cli);
}
}
//接收消息
public String getMsg() {
try {
return in.readLine();
} catch (IOException e) {
CloseStream.close(in);
clis.remove(cli);
return null;
}
}
//发送消息
public void sendMsg(String msg) {
if(msg == null ||msg.equals("")) {
return;
}
for (Socket c : clis) {
if(c != cli) {
try {
out = new BufferedWriter(new OutputStreamWriter(c.getOutputStream()));
out.write(msg);
out.flush();
out.newLine();
out.flush();
} catch (IOException e) {
CloseStream.close(out);
//foreach的过程中不能删
}
}
}
}
@Override
public void run() {
while(true) {
sendMsg(getMsg());
}
}
}
工具类
package com.xuedao.utils;
//所有流的父类-Closeable
import java.io.Closeable;
import java.io.IOException;
public class CloseStream {
public static void close(Closeable...io) {
for (Closeable o : io) {
if(o != null) {
try {
o.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}