服务器之家

服务器之家 > 正文

基于Java网络编程和多线程的多对多聊天系统

时间:2021-09-09 11:02     来源/作者:Dev-Flow

1.前言

程序实现基于星型结构(服务器接收来自各个客户端发送的信息,然后将信息传递给其他客户端界面并在其他客户端界面显示发送的信息)

基于Java网络编程和多线程的多对多聊天系统

2.类图

基于Java网络编程和多线程的多对多聊天系统

3.代码

客户端代码:

package netProgram;

import java.io.IOException;
import java.net.Socket;
import java.net.SocketAddress;

public class Client implements ScreenInputInterface{
    private SocketHandler socketHandler;

    public void start(){
        new Thread(new ScreenInputRunIns(this)).start();
        try {
            socketHandler =   new SocketHandler(new Socket("127.0.0.1",666),null);
            new Thread(socketHandler).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void send(String msg) {
        socketHandler.sendMsg(msg);
    }

    /*    @Override
    public void send(String msg, SocketAddress address) {
        socketHandler.sendMsg(msg);
    }*/
}

服务端代码:

package netProgram;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.List;

public class Server implements ScreenInputInterface{
    private List<SocketHandler> clients = new ArrayList<>();
    private ServerSocket serverSocket;

    public void start(){
//        new Thread(new ScreenInputRunIns(this)).start();
        try {
            serverSocket = new ServerSocket(666);
            while (true){
                SocketHandler socketHandler = new SocketHandler(serverSocket.accept(),this);
                clients.add(socketHandler);
                System.out.println(socketHandler.getSocket().getRemoteSocketAddress()+"进入聊天系统");
                new Thread(socketHandler).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void send(String msg) {
        for (SocketHandler s:clients) {
            s.sendMsg(msg);
        }
    }

    /*    @Override
    public void send(String msg, SocketAddress address) {
        for (SocketHandler s:clients) {
            if (s.getSocket().getRemoteSocketAddress().equals(address)){
                continue;
            }
            s.sendMsg(msg);
        }
    }*/

    public static void main(String[] args) {
        new Server().start();
    }
}

屏幕输入信息接口:

package netProgram;

import java.net.SocketAddress;

public interface ScreenInputInterface {
    void send(String msg);
//    void send(String msg, SocketAddress address);
}

用于各个端口输入信息的创建的线程对象:

package netProgram;

import java.util.Scanner;

public class ScreenInputRunIns implements Runnable{
    private ScreenInputInterface screenInputInterface;
    private Scanner input = new Scanner(System.in);

    public ScreenInputRunIns(ScreenInputInterface screenInputInterface) {
        this.screenInputInterface = screenInputInterface;
    }

    @Override
    public void run() {//屏幕实时输入
        while (true){
            String msg = input.nextLine();
//            screenInputInterface.send(msg,null);
            screenInputInterface.send(msg);
        }
    }
}

套接字处理器(用于传输数据:实际发送信息到输出流并实时监听各个端口的输入且负责显示):

package netProgram;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class SocketHandler implements Runnable{
    private String clientName;
    private Socket socket;
    private PrintWriter writer;
    private Server server;

    public SocketHandler(Socket socket,Server server) throws IOException {
        this.socket = socket;
        this.server = server;
        clientName  = "<"+socket.getLocalSocketAddress()+">";
        writer = new PrintWriter(this.socket.getOutputStream());
    }

    public Socket getSocket() {
        return socket;
    }

    public void sendMsg(String msg){
        if (server!=null) writer.println(msg);
        else writer.println(clientName+":"+msg);
        writer.flush();
    }

    @Override
    public void run() {
        while(true){//监听输入流的输入
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String receiveMsg = null;
                System.out.println();
                if ((receiveMsg = reader.readLine())!=null&&!(receiveMsg.equals(""))){
                    if (server!=null){
//                        server.send(receiveMsg,socket.getRemoteSocketAddress());
                        server.send(receiveMsg);
                    }else {
                        System.out.println(receiveMsg);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

负责启动的各个端口的客户端代码:

package netProgram;

public class ClientLinked {
    public static void main(String[] args) {
        new Client().start();
    }
}
package netProgram;

public class ClientLinked1 {
    public static void main(String[] args) {
        new Client().start();
    }
}
package netProgram;

public class ClientLinked2 {
    public static void main(String[] args) {
        new Client().start();
    }
}

服务端启动代码在服务端类的main方法中。

到此这篇关于基于Java网络编程和多线程的多对多聊天系统的文章就介绍到这了,更多相关java实现多对多聊天系统内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_46539113/article/details/115707094

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
返回顶部