【问题标题】:Jeromq/ZeroMQ server (windows) - client (android) testJeromq/ZeroMQ 服务器 (windows) - 客户端 (android) 测试
【发布时间】:2015-07-03 12:17:17
【问题描述】:

我正在尝试使用 Jeromq 库和一个不起作用的简单应用程序进行测试。

这里是服务器:

导入 org.zeromq.ZMQ;

public class Server  {

     public static void main(String[] args) throws Exception {

            ZMQ.Context ctx = ZMQ.context(1);

            ZMQ.Socket socket = ctx.socket(ZMQ.REP);
            socket.bind("tcp://192.168.56.1:5570");
            System.out.println("Started");

            while (!Thread.currentThread().isInterrupted()) {

                byte[] request = socket.recv(0);
                String string = new String(request);
                System.out.println("Received request: ["+string+"].");

                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                socket.send("We got message".getBytes(), 0);
            }

            System.out.println("finished");
            socket.close();
            ctx.term();           
     } 
}

所以,启动它后,我有一个日志 - “已启动”。 192.168.56.1 是我的本地 IP 地址,而 netstat -na(我在 Windows 上工作)显示 192.168.56.1:5570 正在监听。所以,我想没关系。我在 Windows 中打开了这个套接字。

然后在android上我写了这个:

连接建立接口:

public interface ConnectionReadyListnener {

        void onServerConnectionEstablished();

}

然后 Connection AsyckTask 建立与服务器的连接:

public class Connection extends AsyncTask<Void, Void, Void> {

    public ConnectionReadyListnener listener=null;
    public static ZMQ.Context context;
    public static ZMQ.Socket socket;


    @Override
    protected Void doInBackground(Void... params) {

        context = ZMQ.context(1);
        socket = context.socket(ZMQ.DEALER);
        socket.setIdentity("12345".getBytes(ZMQ.CHARSET));
        socket.connect("tcp://192.168.56.1:5570"); 

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        listener.onServerConnectionEstablished();
    }

}

以及在建立连接时启动测试任务的 MainActivity:

public class MainActivity extends Activity implements ConnectionReadyListnener{
    Connection connection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        connection = new Connection();
        connection.listener=this;
        connection.execute();
    }

    @Override
    public void onServerConnectionEstablished() {
        Toast.makeText(getApplicationContext(), "connected", Toast.LENGTH_SHORT).show();
        new TestAsynk().execute();

    }


}

这是 TestAsynk 类,用于发送服务器应接收的测试消息并记录到控制台:

public class TestAsynk extends AsyncTask<Void, Void, Void>{

    @Override
    protected Void doInBackground(Void... params) {
        Connection.socket.send("Test".getBytes(), 0);
        return null;
    }

}

但它从不记录结果——它没有收到它。问题是我不知道为什么 - 电话(真实设备)和带有服务器的计算机在一个 WiFi 网络中。而且我还了解到 ZeroMQ 无法确定连接是否真的建立。

请帮我弄清楚。

【问题讨论】:

  • 您找到解决此问题的方法了吗?在过去的两天里,我在谷歌没有太多帮助的情况下被困住了。
  • @Ashok 抱歉,我没有尝试 ZMQ。此外,测试表明 websockets 比 zmq 更快
  • 哦,好的。无论如何感谢您的回复。我的一位同事似乎已经成功地做到了这一点。如果有,会在这里更新。
  • 我在 Android Manifest 中缺少 INTERNET 权限。由于某种原因,它也没有在日志中报告!这可能是它也不适合你的原因吗? _Thanks to [https://www.novoda.com/blog/minimal-zeromq-client-server] blog post. It mentions the permission on the last line. Reading between the lines is not enough sometimes :)_
  • 不,我有这个权限,但我在服务器端遇到了问题

标签: java android sockets zeromq jeromq


【解决方案1】:

这里有很多问题。

1) 永远不要在线程之间传递套接字。 ZeroMQ 中唯一的线程安全对象是 Context。在线程之间传递套接字几乎肯定是这里问题的根源。

2) 如果您在服务器端遇到问题,可能是 IP 地址不正确。如果可能的话,我不建议在绑定的一侧对 IP 地址进行硬编码。只需使用通配符“*”代替。所以在服务器端,它看起来像

    socket.bind("tcp://*:5570");

3) 你的异步模式有一个明显的错误...如果onPostExecute 在监听器设置为活动之前运行会发生什么?至少,向您的 AsyncTask 添加一个构造函数,该构造函数接受 Activity...类似于

public Connection(Activity listener){
    this.listener = listener;
}

4) 检查权限。过去,我需要 INTERNET 和 ACCESS_WIFI_STATE 权限。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多