Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. public class Server {
  2.  
  3. public static void main(String[] args) throws Exception {
  4.  
  5. ZMQ.Context ctx = ZMQ.context(1);
  6.  
  7. ZMQ.Socket socket = ctx.socket(ZMQ.REP);
  8. socket.bind("tcp://192.168.56.1:5570");
  9. System.out.println("Started");
  10.  
  11. while (!Thread.currentThread().isInterrupted()) {
  12.  
  13. byte[] request = socket.recv(0);
  14. String string = new String(request);
  15. System.out.println("Received request: ["+string+"].");
  16.  
  17. try {
  18. Thread.sleep(100);
  19. } catch (InterruptedException e) {
  20. e.printStackTrace();
  21. }
  22.  
  23. socket.send("We got message".getBytes(), 0);
  24. }
  25.  
  26. System.out.println("finished");
  27. socket.close();
  28. ctx.term();
  29. }
  30. }
  31.  
  32. public interface ConnectionReadyListnener {
  33.  
  34. void onServerConnectionEstablished();
  35.  
  36. }
  37.  
  38. public class Connection extends AsyncTask<Void, Void, Void> {
  39.  
  40. public ConnectionReadyListnener listener=null;
  41. public static ZMQ.Context context;
  42. public static ZMQ.Socket socket;
  43.  
  44.  
  45. @Override
  46. protected Void doInBackground(Void... params) {
  47.  
  48. context = ZMQ.context(1);
  49. socket = context.socket(ZMQ.DEALER);
  50. socket.setIdentity("12345".getBytes(ZMQ.CHARSET));
  51. socket.connect("tcp://192.168.56.1:5570");
  52.  
  53. return null;
  54. }
  55.  
  56. @Override
  57. protected void onPostExecute(Void result) {
  58. super.onPostExecute(result);
  59. listener.onServerConnectionEstablished();
  60. }
  61.  
  62. }
  63.  
  64. public class MainActivity extends Activity implements ConnectionReadyListnener{
  65. Connection connection;
  66.  
  67. @Override
  68. protected void onCreate(Bundle savedInstanceState) {
  69. super.onCreate(savedInstanceState);
  70. setContentView(R.layout.activity_main);
  71. connection = new Connection();
  72. connection.listener=this;
  73. connection.execute();
  74. }
  75.  
  76. @Override
  77. public void onServerConnectionEstablished() {
  78. Toast.makeText(getApplicationContext(), "connected", Toast.LENGTH_SHORT).show();
  79. new TestAsynk().execute();
  80.  
  81. }
  82.  
  83.  
  84. }
  85.  
  86. public class TestAsynk extends AsyncTask<Void, Void, Void>{
  87.  
  88. @Override
  89. protected Void doInBackground(Void... params) {
  90. Connection.socket.send("Test".getBytes(), 0);
  91. return null;
  92. }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement