共四个类文件
1:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Client {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ExecutorService es = Executors.newSingleThreadExecutor();
try {
Socket socket = new Socket("localhost",6000);
System.out.println("连接服务器成功");
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
//向服务器发送登入信息
System.out.println("请输入名称:");
String name = input.nextLine();
Message msg = new Message(name, null, MessageType.TYPE_LOGIN,null);
oos.writeObject(msg);
msg = (Message) ois.readObject();
System.out.println(msg.getFrom()+msg.getInfo());
//启动读取消息的线程
es.execute(new ReadInfoTread(ois));
//使用主线程发送消息
boolean flag = true;
while (flag){
msg = new Message();
System.out.println("to:");
msg.setTo(input.nextLine());
msg.setFrom(name);
msg.setType(MessageType.TYPE_SEND);
System.out.println("info:");
msg.setInfo(input.nextLine());
oos.writeObject(msg);
}
} catch (IOException |ClassNotFoundException e) {
e.printStackTrace();
}
}
}
//读取消息的线程
class ReadInfoTread implements Runnable{
private ObjectInputStream in;
private boolean flag = true;
public void setFlag(boolean flag) {
this.flag = flag;
}
public ReadInfoTread(ObjectInputStream in) {
this.in = in;
}
@Override
public void run() {
try {
while (flag) {
Message message = (Message) in.readObject();
System.out.println("[" + message.getFrom() + "]发送的消息为:" + message.getInfo());
}
if (in != null) {
in.close();
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
2:
import java.io.Serializable;
public class Message implements Serializable {
private String from;//发送的人
private String to;//接收的人
private int type;//消息类型
private String info;//消息内容
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public Message(String from, String to, int type, String info) {
this.from = from;
this.to = to;
this.type = type;
this.info = info;
}
public Message() {
}
@Override
public String toString() {
return "Message{" +
"from='" + from + '\'' +
", to='" + to + '\'' +
", type=" + type +
", info='" + info + '\'' +
'}';
}
}
3:
public final class MessageType {
public static final int TYPE_LOGIN = 001;//登入的消息类型
public static final int TYPE_SEND = 002;//发送消息的类型
}
4:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Server {
public static void main(String[] args) {
//保存用户端处理的线程
Vector<UserThread> vector = new Vector<>();
ExecutorService es = Executors.newFixedThreadPool(5);
//创建服务器端的Socket;
try {
ServerSocket server = new ServerSocket(6000);
System.out.println("服务器端已经启动啦,快来投靠我啊");
while (true) {
Socket socket = server.accept();
UserThread user = new UserThread(socket, vector);
es.execute(user);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 用户线程
*/
class UserThread implements Runnable {
private String name;//客户端的用户名称(唯一)
private Socket socket;
//某个线程要找到其他要发送消息的线程,要从线程列表查找,所以把线程列表扔过来
private Vector<UserThread> vector;
private ObjectInputStream ois;
private ObjectOutputStream oos;
private boolean flag = true;
public UserThread(Socket socket, Vector<UserThread> vector) {
this.socket = socket;
this.vector = vector;
vector.add(this);
}
@Override
public void run() {
try {
System.out.println("客户端" + socket.getInetAddress().getHostAddress() + "已连接");
ois = new ObjectInputStream(socket.getInputStream());
oos = new ObjectOutputStream(socket.getOutputStream());
while (flag) {
//读取消息对象
Message msg = (Message) ois.readObject();
int type = msg.getType();
switch (type) {
case MessageType.TYPE_SEND:
String to = msg.getTo();
UserThread ut;
int size = vector.size();
for (int i = 0; i < size; i++) {
ut = vector.get(i);
if (to.equals(ut.name) && ut != this) {
ut.oos.writeObject(msg);
break;
}
}
break;
case MessageType.TYPE_LOGIN:
name = msg.getFrom();
msg.setInfo("welcome you");
oos.writeObject(msg);
break;
}
}
ois.close();
oos.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}