Android实时绘制效果
通过长连接的Socket通信,实时发送绘制信息给各客户端,以实现实时显示效果。
长连接的Socket通信,支持多客户端。服务器和客户端都可主动发送消息。消息的话,将Object转成了json字符串了。
之前用ObjectOutputStream多客户端传送对象时遇到了问题,又搜索了下,参见该网址: 。也就是要建立了ObjectOutputStream的集合,来传消息了?总之,服务器通过Socket集合每次new个ObjectOutputStream来发消息是不成的!
ps:不想转json,还是想用ObjectOutputStream发送的,可参照《Java Socket通信》长连接通信内的对象消息样例。
-
-
-
-
-
- public class EasyServer extends Thread {
-
-
- public static final String EXIT_COMMAND = "end";
-
-
- private int port;
-
- private ServerSocket mServerSocket;
-
- private ExecutorService pool;
-
- private ArrayList<Socket> mClientList;
-
- private OnServerListener listener;
-
- public EasyServer(int port) {
- this.port = port;
- pool = Executors.newCachedThreadPool();
- mClientList = new ArrayList<Socket>();
- }
-
- @Override
- public void run() {
- try {
- mServerSocket = new ServerSocket(port);
- Socket client = null;
- while (true) {
- client = mServerSocket.accept();
- mClientList.add(client);
- if (null != listener) {
- listener.onClientConnected(client.getInetAddress());
- }
- pool.execute(new ThreadServer(client));
- }
- } catch (BindException e) {
- if (null != listener) {
- listener.onBindException();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
-
- private class ThreadServer extends Thread {
-
- private Socket client;
-
- public ThreadServer(Socket client) {
- this.client = client;
- }
-
- @Override
- public void run() {
- DataInputStream in = null;
- try {
- in = new DataInputStream(client.getInputStream());
- while (true) {
-
- String msg = in.readUTF();
- if (EXIT_COMMAND.equals(msg)) {
-
- DataOutputStream out = new DataOutputStream(
- client.getOutputStream());
- out.writeUTF(EXIT_COMMAND);
- out.flush();
- if (null != listener) {
- listener.onExited(client.getInetAddress());
- }
- break;
- } else {
-
- if (null != listener) {
- listener.onReceive(client.getInetAddress(), msg);
- }
- }
-
- }
- } catch (SocketException e) {
- if (null != listener) {
- listener.onSocketException(client.getInetAddress());
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- mClientList.remove(client);
- try {
- if (null != in) {
- in.close();
- }
- if (null != client) {
- client.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- }
-
-
- public void sendMessage(String msg) {
- int i = 0;
- try {
- DataOutputStream out = null;
- for (; i < mClientList.size(); i++) {
- out = new DataOutputStream(mClientList.get(i).getOutputStream());
- out.writeUTF(msg);
- out.flush();
- }
- } catch (SocketException e) {
- if (null != listener && mClientList.size() > 0) {
- listener.onSocketException(mClientList.get(i).getInetAddress());
- }
- mClientList.remove(i);
- } catch (IOException e) {
- e.printStackTrace();
- mClientList.remove(i);
- }
- }
-
-
- public void setOnServerListener(OnServerListener listener) {
- this.listener = listener;
- }
-
- }
-
-
-
-
-
- public class EasyClient extends Thread {
- (还报8W字符冗余,受不住了,就这么着吧T^T)...
- }
- public class JsonUtil {
-
-
-
-
-
-
-
- public static String objToJson(Object obj) {
- JSONStringer arrayJson = new JSONStringer();
- Class<?> clazz = obj.getClass();
- try {
- arrayJson.array().value(clazz.getName());
- JSONStringer objJson = new JSONStringer();
- objJson.object();
- Field[] fields = clazz.getDeclaredFields();
- for (Field field : fields) {
- field.setAccessible(true);
- objJson.key(field.getName()).value(field.get(obj));
- }
- objJson.endObject();
- arrayJson.value(objJson).endArray();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return arrayJson.toString();
- }
-
-
-
-
-
-
-
-
- public static Object jsonToObj(String json) {
- Object obj = null;
- try {
- JSONArray jarray = new JSONArray(json);
- String className = jarray.getString(0);
- Class<?> clazz = Class.forName(className);
- obj = clazz.newInstance();
-
- JSONObject jobj = new JSONObject(jarray.getString(1));
- @SuppressWarnings("unchecked")
- Iterator<String> it = jobj.keys();
- Field field;
- while (it.hasNext()) {
- String key = it.next();
- field = clazz.getDeclaredField(key);
- setValue(obj, field, jobj.getString(key));
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return obj;
- }
-
-
-
-
-
-
-
-
- private static void setValue(Object obj, Field field, String value)
- throws IllegalArgumentException, IllegalAccessException {
- ...
- }
-
- }
本文转自winorlose2000 51CTO博客,原文链接:http://blog.51cto.com/vaero/893886,如需转载请自行联系原作者