多人聊天室原理圖

源碼
工具類:
該類用於關閉各種流。
public class CloseUtil {
public static void CloseAll(Closeable... closeable){
for(Closeable c:closeable){
if (c != null) {
try {
c.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服務器:
服務器端創建一個serverSocket對象通過accept()方法監聽是否有tcp連接,同時有一個儲存socket對象的集合將連接進來的對象儲存到List集合中,服務器將消息進行轉發。
//服務器
public class Server {
//存儲每一個連接進來的客戶端
public static List<MyChannel> list=new ArrayList<>();
public static void main(String[] args) throws Exception {
//創建ServerSocket對象
ServerSocket serverSocket = new ServerSocket(9999);
while (true){
//連接進來的客戶端
Socket client = serverSocket.accept();
System.out.println(client.getInetAddress()+"進入聊天室");
MyChannel myChannel = new MyChannel(client);
list.add(myChannel);
new Thread(myChannel).start();
}
}
}
消息轉發類:
具體的消息轉發實現類,將信息發給除發送消息以外的其他客戶端。
//用於信息轉發
public class MyChannel implements Runnable{
private DataInputStream dis;
private DataOutputStream dos;
private boolean flag=true;
public MyChannel(Socket socket) {
try{
dis=new DataInputStream(socket.getInputStream());
dos=new DataOutputStream(socket.getOutputStream());
}catch (IOException e){
flag=false;
CloseUtil.CloseAll(dis,dos);
}
}
//接收數據的方法
private String receive(){
String str="";
try{
str= dis.readUTF();
}catch (IOException e){
flag=false;
CloseUtil.CloseAll(dis,dos);
Server.list.remove(this);
}
return str;
}
//發送數據的方法
private void send(String str){
try {
if (str != null && str.length() != 0) {
dos.writeUTF(str);
dos.flush();
}
}catch (Exception exception){
flag=false;
CloseUtil.CloseAll(dos,dis);
Server.list.remove(this);
}
}
//轉發消息的方法
private void sendToOther(){
String str=this.receive();
List<MyChannel> list = Server.list;
for (MyChannel other:list) {
if(other==list){
continue;//不發送信息給自己
}
//將消息發送給其他客戶端
other.send(str);
}
}
@Override
public void run() {
while (flag){
sendToOther();
}
}
}
發送信息類:
用於從鍵盤上獲取數據然後將數據發送出去
public class Send implements Runnable{
//從鍵盤上獲取數據
private BufferedReader br;
private DataOutputStream dos;
private boolean flag=true;
public Send() {
br=new BufferedReader(new InputStreamReader(System.in));
}
public Send(Socket socket){
this();
try{
dos=new DataOutputStream(socket.getOutputStream());
}catch (Exception e){
flag=false;
CloseUtil.CloseAll(dos,socket);
e.printStackTrace();
}
}
private String getMessage(){
String str="";
try{
str=br.readLine();
}catch (IOException e){
flag=false;
CloseUtil.CloseAll(br);
}
return str;
}
private void send(String str){
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e) {
flag=false;
CloseUtil.CloseAll(dos);
e.printStackTrace();
}
}
@Override
public void run() {
while (flag){
this.send(getMessage());
}
}
}
信息接收類:
public class Receive implements Runnable{
//接受數據流
private DataInputStream dis;
private boolean flag=true;
public Receive(Socket socket){
try {
dis = new DataInputStream(socket.getInputStream());
}catch (Exception e){
flag=false;
CloseUtil.CloseAll(dis,socket);
}
}
private String getMessage(){
String str="";
try {
str=dis.readUTF();
} catch (IOException e) {
flag=false;
CloseUtil.CloseAll(dis);
e.printStackTrace();
}
return str;
}
@Override
public void run() {
while (flag){
System.out.println(this.getMessage());
}
}
}
客戶端:
public class client {
public static void main(String[] args) throws Exception{
Socket socket = new Socket(InetAddress.getLocalHost(),9999);
Send send = new Send(socket);
Receive receive = new Receive(socket);
new Thread(send).start();
new Thread(receive).start();
}
}
先將服務器啟動然後啟動客戶端:測試結果如下

有喜歡的小夥伴可以自己拿去玩,代碼複製直接有效。
原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/223299.html