我想用一個簡單的應用程序不工作與Jeromq庫測試。Jeromq/ZeroMQ服務器(Windows) - 客戶端(安卓)測試
這裏的服務器:
進口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是Listening。所以,我想沒關係。我在Windows中打開此套接字。
然後在Android上我寫了這個:
接口連接建立:
public interface ConnectionReadyListnener {
void onServerConnectionEstablished();
}
然後連接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
@Ashok對不起,我沒有,並退出嘗試ZMQ。另外,測試顯示websockets比zmq – user2976267
還好。無論如何感謝您的迴應。看來我的一位同事已經成功地做到了這一點。如果是的話,會在這裏更新 – Ashok