【问题标题】:Android application with server to client messaging带有服务器到客户端消息传递的 Android 应用程序
【发布时间】:2015-03-23 13:40:22
【问题描述】:

我目前正在创建一个允许客户端到服务器消息传递的 Android 应用程序,但我正在寻找一种方法来允许服务器到客户端消息传递,以便客户端和服务器都可以来回传递消息。任何关于我如何做到这一点的建议将不胜感激。我知道我需要编辑我的 ChatServer 类,如下所示;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.LinkedList;
import java.util.List;

public class ChatServer {

private static final String USAGE = "Usage: java ChatServer";

/** Default port number on which this server to be run. */
private static final int PORT_NUMBER = 8008;

/**
 * List of print writers associated with current clients, one for each.
 */
private List<PrintWriter> clients;

/** Creates a new server. */
public ChatServer() {
    clients = new LinkedList<PrintWriter>();
}

/** Starts the server. */
public void start() {
    System.out.println("AndroidChatApplication server started on port "
            + PORT_NUMBER + "!");
    try {
        ServerSocket s = new ServerSocket(PORT_NUMBER);
        for (;;) {
            Socket incoming = s.accept();
            new ClientHandler(incoming).start();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("AndroidChatApplication server stopped.");
}

/** Adds a new client identified by the given print writer. */
private void addClient(PrintWriter out) {
    synchronized (clients) {
        clients.add(out);
    }
}

/** Adds the client with given print writer. */
private void removeClient(PrintWriter out) {
    synchronized (clients) {
        clients.remove(out);
    }
}

/** Broadcasts the given text to all clients. */
private void broadcast(String msg) {
    for (PrintWriter out : clients) {
        out.println(msg);
        out.flush();
    }
}

public static void main(String[] args) {
    if (args.length > 0) {
        System.out.println(USAGE);
        System.exit(-1);
    }
    new ChatServer().start();
}

/**
 * A thread to serve a client. This class receive messages from a client and
 * broadcasts them to all clients including the message sender.
 */
private class ClientHandler extends Thread {

    /** Socket to read client messages. */
    private Socket incoming;

    /** Creates a hander to serve the client on the given socket. */
    public ClientHandler(Socket incoming) {
        this.incoming = incoming;
    }

    /** Starts receiving and broadcasting messages. */
    public void run() {
        PrintWriter out = null;
        try {
            out = new PrintWriter(new OutputStreamWriter(
                    incoming.getOutputStream()));

            // inform the server of this new client
            ChatServer.this.addClient(out);

            out.print("Welcome to AndroidChatApplication! ");
            out.println("Enter BYE to exit.");
            out.flush();

            BufferedReader in = new BufferedReader(new InputStreamReader(
                    incoming.getInputStream()));
            for (;;) {
                String msg = in.readLine();
                if (msg == null) {
                    break;
                } else {
                    if (msg.trim().equals("BYE"))
                        break;
                    System.out.println("Received: " + msg);
                    // broadcast the receive message
                    ChatServer.this.broadcast(msg);
                }
            }
            incoming.close();
            ChatServer.this.removeClient(out);
        } catch (Exception e) {
            if (out != null) {
                ChatServer.this.removeClient(out);
            }
            e.printStackTrace();
        }
    }
 }
}

【问题讨论】:

    标签: android client server messaging


    【解决方案1】:

    您可以使用 Google 的推送通知服务,查看GoogleCloudMessaging 和朋友了解更多详情。这里有一些 simple tutorial 的使用方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-28
      • 1970-01-01
      • 2018-05-10
      • 2013-04-24
      • 2015-12-09
      • 1970-01-01
      • 2013-12-30
      相关资源
      最近更新 更多