I'm trying to do a test with Jeromq library with a simple app which doesn't work.
Here's Server :
import 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();
}
}
So, after starting it I have a Log - "Started". 192.168.56.1 is my local ip address and netstat -na (I'm working on Windows) shows me that 192.168.56.1:5570 is Listening. So, I guess it's ok. I opened this socket in Windows.
Then on android I wrote this :
Interface for connection establishing :
public interface ConnectionReadyListnener {
void onServerConnectionEstablished();
}
Then Connection AsyckTask to establish connection to th server :
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();
}
}
and MainActivity that launches test task when connection is established:
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();
}
}
And here's the TestAsynk class to send a test message that server should recieve and Log to the console :
public class TestAsynk extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
Connection.socket.send("Test".getBytes(), 0);
return null;
}
}
But it never logs the result out - it doesn't receive it. The problem is I don't know why - phone (real device) and computer with server are within one WiFi network. And also I learned that there is no way For ZeroMQ to find out if the connection is really established or not.
Please help me figure it out .