ref:
http://zguide.zeromq.org/page:all
http://zguide.zeromq.org/java:hwserver
http://zguide.zeromq.org/java:rrclient
1. Server
4. running android client
5. response in server log
http://zguide.zeromq.org/page:all
http://zguide.zeromq.org/java:hwserver
http://zguide.zeromq.org/java:rrclient
1. Server
import org.zeromq.ZMQ; /** * * @author kirra */ public class PCSuite { /** * @param args the command line arguments */ public static void main(String[] args) throws Exception { // TODO code application logic here' ZMQ.Context context = ZMQ.context(1); // Socket to talk to clients ZMQ.Socket responder = context.socket(ZMQ.REP); responder.bind("tcp://*:30001"); System.out.println("Received"); while (!Thread.currentThread().isInterrupted()) { // Wait for next request from the client byte[] request = responder.recv(0); System.out.println("Received Hello"); // Do some 'work' Thread.sleep(1000); // Send reply back to client String reply = "World"; responder.send(reply.getBytes(), 0); } responder.close(); context.term(); } }2. Client (Android): ip -> 10.226.174.70 (IP computer server)
import android.content.Context; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import com.bahasakita.mobile.gkutulis.R; import org.zeromq.ZMQ; public class ZMQActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_zmq); new Thread(new MyRunnablee()).start(); } public class MyRunnablee implements Runnable { ZMQ.Context context; ZMQ.Socket requester; public MyRunnablee() { context = ZMQ.context(1); requester= context.socket(ZMQ.REQ); } public void run(){ requester.connect("tcp://10.226.174.70:30001"); for (int request_nbr = 0; request_nbr < 10; request_nbr++) { requester.send("Hello", 0); String reply = requester.recvStr(0); Log.e("ZMQ Activity", "onCreate1: " + request_nbr + " [" + reply + "]"); } // We never get here but clean up anyhow requester.close(); context.term(); } } public static void start(Context context) { Intent starter = new Intent(context, ZMQActivity.class); // starter.putExtra(); context.startActivity(starter); } }3. running server in netbeans
4. running android client
5. response in server log
run: Received Received Hello Received Hello Received Hello Received Hello Received Hello Received Hello Received Hello Received Hello Received Hello Received Hello6. response in client log
04-05 16:41:16.913 11522-11841/<package> E/ZMQ Activity: onCreate1: 0 [World] 04-05 16:41:17.917 11522-11841/<package> E/ZMQ Activity: onCreate1: 1 [World] 04-05 16:41:18.960 11522-11841/<package> E/ZMQ Activity: onCreate1: 2 [World] 04-05 16:41:20.085 11522-11841/<package> E/ZMQ Activity: onCreate1: 3 [World] 04-05 16:41:21.111 11522-11841/<package> E/ZMQ Activity: onCreate1: 4 [World] 04-05 16:41:22.235 11522-11841/<package> E/ZMQ Activity: onCreate1: 5 [World] 04-05 16:41:23.262 11522-11841/<package> E/ZMQ Activity: onCreate1: 6 [World] 04-05 16:41:24.386 11522-11841/<package> E/ZMQ Activity: onCreate1: 7 [World] 04-05 16:41:25.410 11522-11841/<package> E/ZMQ Activity: onCreate1: 8 [World] 04-05 16:41:26.433 11522-11841/<package> E/ZMQ Activity: onCreate1: 9 [World]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~DONE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
No comments:
Post a Comment