有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

多线程如何在java中使用tcp向所有客户端广播消息

我正在构建一个多线程聊天服务器应用程序,它将一个客户端发送的消息广播给所有客户端。在互联网和Oracle网站上的大多数示例中,广播都是使用udp(多播socket)完成的,但我使用的是tcp。 有人知道如何在tcp连接中向所有连接的客户端发送消息吗? 以下是我当前的代码,它运行良好,只将从客户机收到的消息发送给该客户机:

EchoServer

import java.net.*;
import java.io.*;

public class EchoServer 
{
    public static void main(String[] args)
        throws IOException
    {
        if (args.length != 1) {
            System.err.println("Usage: java EchoServer <port number>");
            System.exit(1);
        }

        int portNumber = Integer.parseInt(args[0]);

        ServerSocket serverSocket = new ServerSocket(Integer.parseInt(args[0]));

        while (true) {
            try {
                Thread t = new Thread(new MultiServer(serverSocket.accept()));
                t.start();
            } catch(IOException e) {
                System.out.println("Accept Failed:");
                System.exit(-1);
            }
        }
    }
}

EchoClient

import java.io.*;
import java.util.Scanner;
import java.net.*;

public class EchoClient 
{
    public static void main(String[] args) throws IOException 
    {
        if (args.length != 2) {
            System.err.println("Usage: java EchoClient <host name><portnumber>");
            System.exit(1);
        }

        String hostName = "localhost";

        int portNumber = Integer.parseInt(args[1]);

        try (
            Socket echoSocket = new Socket(hostName, portNumber);
            PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        ) {
            String userInput;

            while ((userInput = stdIn.readLine()) != null)  {
                out.println(userInput);
                System.out.println("echo::" + in.readLine());
            }
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host " + hostName);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to " + hostName);
            System.exit(1);
        } 
    }
}

多服务器

import java.io.*;
import java.net.*;

public class MultiServer implements Runnable
{
    private Socket client;

    public MultiServer(Socket m)
    {
        this.client = m;
    }

    @Override
    public void run()
    {
        BufferedReader in = null;
        PrintWriter out = null;
        try {
            out = new PrintWriter(client.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        } catch(IOException ignored) {
        }

        while (true) {
            String line;
            try {
                while ((line = in.readLine()) != null)
                    out.println(line);
            } catch (IOException e) {
                System.out.println("Read Failed");
                System.exit(-1);
            }
        }
    }
}

共 (1) 个答案

  1. # 1 楼答案

    使用并发hashmap并在其中维护客户机列表。 并发hashmap是安全的,在添加/迭代/删除时不需要使用同步

    // Create and main list of active clients based on their host name / ip address
    ConcurrentHashMap<String, Socket> activeClients = new ConcurrentHashMap<String, Socket>();
    
    // message received
    activeClients.put(clientsocket.getInetAddress().getHostAddress(), clientsocket);
    
    // broadcast message to all available clients
    for(String clientHost : activeClients.keySet()) {
          // get each socket here and send a message to them.
    }
    

    Vector基本上是线程安全的,所以你不需要担心它