dpi10335 2015-11-06 14:47
浏览 241

Zeromq PUB SUB不接收消息(Golang作为服务器,Android作为客户端)

I am running My Server Side Zeromq in Golang (PUB) . I am testing those features with the basic example provided. My Server is in Digitalocen Droplet . Its running fine . And I am able to connect and receive message on my local machine via Golang client as SUB but when I try the same logic in android then I get nothing . It stuck in the loop . And No errors.

Server Side PUB : Golang

//
//  Pubsub envelope publisher.
//

package main

import (
    zmq "github.com/pebbe/zmq4"
    "time"
)

func main() {
    //  Prepare our publisher
    publisher, _ := zmq.NewSocket(zmq.PUB)
    defer publisher.Close()
    publisher.Bind("tcp://*:5563")

    for {
        //  Write two messages, each with an envelope and content
        publisher.Send("A", zmq.SNDMORE)
        publisher.Send("We don't want to see this", 0)
        publisher.Send("B", zmq.SNDMORE)
        publisher.Send("We would like to see this", 0)
        time.Sleep(time.Second)
    }
}

Client Side SUB : Android

I also have the Internet Permission

package com.example.fahim.zeromq;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import org.zeromq.ZMQ;

public class MainActivity extends AppCompatActivity {

    TextView output;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        new Zconnect().execute();

    }

    private  class Zconnect extends AsyncTask<Void, Void, Void>{

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

            ZMQ.Context context = ZMQ.context(1);
            ZMQ.Socket subscriber = context.socket(ZMQ.SUB);

            subscriber.connect("tcp://server-ip:5563");
            subscriber.subscribe("A".getBytes());
            while (true) {
                String string = new String(subscriber.recv(0));
                Log.i("Message :", string);
                Log.i("Info :", "This line never shows.");

            }
        }
    }

}

What I came to realize that it can not get passed this line . I could not find the problem .

 String string = new String(subscriber.recv(0));
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
    • ¥65 汇编语言除法溢出问题
    • ¥15 Visual Studio问题
    • ¥20 求一个html代码,有偿
    • ¥100 关于使用MATLAB中copularnd函数的问题
    • ¥20 在虚拟机的pycharm上
    • ¥15 jupyterthemes 设置完毕后没有效果