多线程实现
Send:客户端发送消息实现类:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class Send implements Runnable {
private Socket client;
private BufferedReader console;
private DataOutputStream dos;
private boolean isRunning=true;
public Send(Socket client,String name) {
this.client=client;
console=new BufferedReader(new InputStreamReader(System.in));
try {
dos=new DataOutputStream(client.getOutputStream());
send(name);
} catch (IOException e) {
System.out.println("客户端构建错误");
release();
}
}
@Override
public void run() {
while(isRunning){
String msg=getStrFromConsole();
if(!msg.equals("")){
send(msg);
}
}
}
private void send(String msg){
try {
dos.writeUTF(msg);
dos.flush();
} catch (IOException e) {
System.out.println("发送出错");
release();
}
}
private String getStrFromConsole(){
try {
return console.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
private void release(){
this.isRunning=false;
LsdUtils.close(client,console,dos);
}
}
Receive:客户端接收消息实现类:
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
public class Receive implements Runnable{
private Socket client;
private DataInputStream dis;
private boolean isRunning=true;
public Receive(Socket client) {
this.client=client;
try {
dis =new DataInputStream(client.getInputStream());
} catch (IOException e) {
System.out.println("客户端发送侯建错误");
release();
}
}
@Override
public void run() {
while(isRunning){
String msg=receive();
if(!msg.equals("")){
System.out.println(msg);
}
}
}
private String receive(){
String msg="";
try {
msg = dis.readUTF();
} catch (IOException e) {
System.out.println("客户端接收报错");
release();
}
return msg;
}
private void release(){
this.isRunning=false;
LsdUtils.close(client,dis);
}
}
客户端类:Client
/**
* 客户端:
* 一个客户能很快手法信息
*/
public class Client {
public static void main(String[] args) throws IOException {
System.out.println("=====Client=====");
Socket client=new Socket("localhost",8888);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入用户名:");
//客户端发送消息
new Thread(new Send(client,br.readLine())).start();
new Thread(new Receive(client)).start();
}
}
聊天室服务器:Chat
/**
* 在线聊天室 服务端
* 问题:其他客户必须等待之前的客户推出,才能继续排队
*/
public class Chat {
private static CopyOnWriteArrayList<Channel> all=new CopyOnWriteArrayList<>();
public static void main(String[] args) throws IOException {
System.out.println("=====Server=====");
ServerSocket server=new ServerSocket(8888);
while(true){
Socket socket=server.accept();
System.out.println("新来一个连接");
Channel c=new Channel(socket);
all.add(c);
new Thread(c).start();
}
}
//一个客户代表一个Channel
static class Channel implements Runnable{
private DataInputStream dis;
private DataOutputStream dos;
private Socket client;
private boolean isRunning=true;
private String name;
public Channel(Socket client) {
this.client=client;
try {
dis = new DataInputStream(client.getInputStream());
dos=new DataOutputStream(client.getOutputStream());
name=receive();
this.send("欢迎你的到来");
sendOthers(this.name+"来到了lsd的聊天室",true);
} catch (IOException e) {
// e.printStackTrace();
System.out.println("构建报错");
release();
}
}
private String receive(){
String msg="";
try {
msg = dis.readUTF();
} catch (IOException e) {
System.out.println("接收报错");
release();
// e.printStackTrace();
}
return msg;
}
private void sendOthers(String msg,boolean flag){
for(Channel other:all){
if(other==this){
continue;
}
if(!flag){
other.send(this.name+"对所有人说: "+msg);
}else{
other.send(msg);//系统消息
}
}
}
private void send(String msg){
try {
dos.writeUTF(msg);
dos.flush();
} catch (IOException e) {
System.out.println("发送出错");
release();
// e.printStackTrace();
}
}
public void release(){
this.isRunning=false;
LsdUtils.close(dis,dos,client);
all.remove(this);
sendOthers(this.name+"离开了大家庭",true);
}
@Override
public void run() {
while(isRunning){
String msg=receive();
if(!msg.equals("")){
sendOthers(msg,false);
}
}
}
}
}