Android:创建ZeroMQ的客户端

这里我们将给出一个subscribe模式的客户端示例程序,如下:

 1 private class ZeroMQMessageTask extends AsyncTask<String, Void, String> {
 2     private String message = "";
 3 
 4     public ZeroMQMessageTask() {
 5     }
 6 
 7     @Override
 8     protected String doInBackground(String... params) {
 9 
10         ZMQ.Context context = ZMQ.context(1);
11         ZMQ.Socket subscriber = context.socket(ZMQ.SUB);
12         subscriber.subscribe("");
13         subscriber.connect("tcp://x.x.x.x:6666");  // 注意,这里必须是服务器的IP地址或DNS Name
14         message = new String(subscriber.recv(0));
15 
16         subscriber.close();
17         context.term();
18 
19         return message;
20     }
21 
22     @Override
23     protected void onPostExecute(String result) {
24         EditText text = (EditText) myView.findViewById(R.id.message); // 这里替换成对应的UI控件用于显示结果
25         searchEditText.setText(result);
26     }
27 }

 关于创建ZeroMQ的服务端,请参考这篇文章:http://www.cnblogs.com/ilovewindy/p/3984269.html

posted @ 2014-09-26 10:36  GreatK  阅读(3711)  评论(0编辑  收藏  举报