Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- }
- }
- public interface ConnectionReadyListnener {
- void onServerConnectionEstablished();
- }
- 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();
- }
- }
- 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();
- }
- }
- public class TestAsynk extends AsyncTask<Void, Void, Void>{
- @Override
- protected Void doInBackground(Void... params) {
- Connection.socket.send("Test".getBytes(), 0);
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement