前言
这玩意就是一个基于TCP的建议聊天室,有服务端与客户端。服务端负责对客户端的数据进行转发至另一个客户端。当然前提是每一个客户端连接服务器的时候需要提供一个用户名,也就是需要进行超简陋的登录。
流程图示
功能示意 代码流程示意
Dome结构预览
Message是我们传递的数据,我们传递消息对象 MessageType 是一个用来标注消息类型的
Message类
import java.io.Serializable;
public class Message implements Serializable{
private String from;
private String to;
private int type;
private String info;
public Message(){
}
public Message(String from, String to, int type, String info) {
this.from = from;
this.to = to;
this.type = type;
this.info = 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;
}
}
type类
public final class MessageType {
public static final int TYPE_LOGIN = 0x1;
public static final int TYPE_SEND = 0x2;
}
服务端用户登录与查询与消息转发
先将用户处理的线程加入列表 更据消息类型做适当反馈,如果是发送消息,那么就直接遍历列表中的用户线程,对比线程的用户名是否与我们的发送对象相等,之后使用该用户线程的输出流发送msg对象,实现通信。
服务端代码
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) {
ExecutorService es = Executors.newFixedThreadPool(6);
Vector<UserThread> vector = new Vector<>();
try{
ServerSocket server = new ServerSocket(8888);
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 Socket socket;
private Vector<UserThread> vector;
private String name;
private ObjectInputStream ois;
private ObjectOutputStream oos;
private boolean flag=true;
public UserThread(Socket socket, Vector<UserThread> vector) {
this.socket = socket;
this.vector = vector;
this.vector.add(this);
}
public UserThread() {
}
@Override
public void run() {
try{
System.out.println("[Host:"+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_LOGIN:{
this.name = msg.getFrom();
msg.setInfo("欢迎你:"+this.name);
oos.writeObject(msg);
break;
}
case MessageType.TYPE_SEND:{
String sendto = msg.getTo();
for(UserThread user:vector){
if(sendto.equals(user.name)&&user!=this){
user.oos.writeObject(msg);
System.out.println(this.name+"send message to"+msg.getTo()+"--success");
break;
}
else{
System.out.println(this.name+"send message to"+msg.getTo()+"--fail");
}
}
break;
}
}
}
oos.close();
ois.close();
}catch(IOException | ClassNotFoundException e){
System.out.println("Closed"+name);
return;
}
}
}
客户端登录
登录 登录后在服务端会发送欢迎消息,所以需要接收。
客户端消息接收与发送
消息接收 消息接受专门在开了一个子线程负责接收消息并展示 消息发送 这个的话我们直接在主线程当中进行发送即可
注意点
当客户端关闭后,我们在服务端的子线程必须关闭,否则继续运行会浪费内存。这里的话,在客户端关闭后,会在我们的服务端抛出IOException异常,那么我在在抛出异常后关闭即可。
客户端代码
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) {
ExecutorService es = Executors.newSingleThreadExecutor();
Scanner input = new Scanner(System.in);
try{
Socket socket = new Socket("localhost",8888);
System.out.println("已连接至服务器");
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
System.out.print("请输入用户名:");
String username = input.nextLine();
Message msg = new Message(username,null,MessageType.TYPE_LOGIN,null);
oos.writeObject(msg);
msg = (Message)ois.readObject();
System.out.println(msg.getInfo());
es.execute(new ReadInfoThread(ois));
boolean flag = true;
while(flag){
msg = new Message();
System.out.print("To:");
msg.setTo(input.nextLine());
msg.setFrom(username);
msg.setType(MessageType.TYPE_SEND);
System.out.print("\nmsg:");
msg.setInfo(input.nextLine());
oos.writeObject(msg);
}
}catch(IOException | ClassNotFoundException e){
e.printStackTrace();
}
}
}
class ReadInfoThread implements Runnable{
private ObjectInputStream ois;
private boolean flag = true;
public ReadInfoThread(ObjectInputStream ois){
this.ois = ois;
}
@Override
public void run() {
try {
while(flag){
Message msg=(Message)ois.readObject();
System.out.println();
System.out.println("From:["+msg.getFrom()+"]say:"+msg.getInfo());
System.out.print("To:");
}
if(ois!=null){
ois.close();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果示意
|