如何使用java中的tcp向所有客户端广播消息

时间:2016-07-28 00:59:57

标签: java multithreading sockets networking tcp

我正在构建一个多线程聊天服务器应用程序,它将一个客户端发送的消息广播给所有客户端。在互联网和 Oracle网站上的大部分示例中,广播也已完成使用udp(Multicast 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 个答案:

答案 0 :(得分:0)

使用并发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基本上是一个线程安全的,所以你不必担心那个。