0

我创建了一个 android 应用程序,其中,android 应用程序充当客户端,服务器驻留在桌面应用程序中。

假设有 10 个 android 应用程序同时在 10 个不同的 android 平板电脑上运行,当从一台平板电脑收到一个更新时,桌面应用程序将更新发送到所有其他剩余的平板电脑。服务器如何知道连接了多少客户端以及如何将消息发送给所有客户端

我计划在所有 android 中运行一个服务器,这样当从一台平板电脑收到一个更新时,桌面应用程序会将更新发送到所有其他剩余的平板电脑。

谁能告诉我一些关于这个的建议。

客户端

private int SERVER_PORT = 9999;
class Client implements Runnable {
            private Socket client;
            private PrintWriter out;
            private Scanner in;

            @Override
            public void run() {
                try {
                    client = new Socket("localhost", SERVER_PORT);
                    Log.d("Client", "Connected to server at port " + SERVER_PORT);
                    out = new PrintWriter(client.getOutputStream());
                    in = new Scanner(client.getInputStream());
                    String line;

                    while ((line = in.nextLine()) != null) {
                        Log.d("Client", "Server says: " + line);
                        if (line.equals("Hello client")) {
                            out.println("Reply");
                            out.flush();
                        }
                    }

                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }

服务器类

class ServerThread implements Runnable {
        private ServerSocket server;

        @Override
        public void run() {
            try {
                server = new ServerSocket(SERVER_PORT);
                Log.d("Server", "Start the server at port " + SERVER_PORT
                        + " and waiting for clients...");
                while (true) {
                    Socket socket = server.accept();
                    Log.d("Server",
                            "Accept socket connection: "
                                    + socket.getLocalAddress());
                    new Thread(new ClientHandler(socket)).start();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    class ClientHandler implements Runnable {

        private Socket clientSocket;
        private PrintWriter out;
        private Scanner in;

        public ClientHandler(Socket clietSocket) {
            this.clientSocket = clietSocket;
        }

        @Override
        public void run() {
            try {
                out = new PrintWriter(clientSocket.getOutputStream());
                in = new Scanner(clientSocket.getInputStream());
                String line;
                Log.d("ClientHandlerThread", "Start communication with : "
                        + clientSocket.getLocalAddress());
                out.println("Hello client");
                out.flush();
                while ((line = in.nextLine()) != null) {
                    Log.d("ClientHandlerThread", "Client says: " + line);
                    if (line.equals("Reply")){
                        out.print("Server replies");
                        out.flush();
                    }
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }
4

2 回答 2

1

您可以将套接字保存在列表中,并通过该列表中的所有输出流发送消息:

Socket socket = Server.accept();
sockets.add(socket);  //sockets is an ArrayList<Socket>

public void sendMessageToEveryone(String msg) {
for(Socket s : sockets) {
  s.getOutputStream().write(msg.getBytes());
  s.getOutputStream().flush();
}
于 2013-12-07T12:08:33.917 回答
0

您可以使用谷歌云消息服务。

看看这个页面: https ://developer.android.com/google/gcm/index.html

于 2013-08-11T18:43:39.887 回答