互连仿真器实例的Andr​​oid [英] Interconnecting Emulator Instances Android

查看:204
本文介绍了互连仿真器实例的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过在DatagramSocket类Android版通信的两个仿真器。他们每个人是一个P2P系统中的节点。因此,他们每个人都有一台服务器线程和客户线程(每个GUI事件创建)。这是我如何创建服务器

 公共静态最后弦乐SERVERIP =10.0.2.15;
    // ...
    跑() {
                InetAddress类serverAddr = InetAddress.getByName(SERVERIP);
                DatagramSocket的插座=新的DatagramSocket(SERVERPORT,serverAddr);
                而(真){
                    字节[] buf中=新的字节[29];
                    DatagramPacket类包=新的DatagramPacket(BUF,buf.length);
                    socket.receive(包);
                // ...
                }
     }

的端口由用户初始化程序期间给出。
客户端部分(请求一些数据)

 的InetAddress serverAddr = InetAddress.getByName(10.0.2.2);
// ...
Log.i(申请人,试图连接到设备端口=+目标);
DatagramSocket的socketJ =新的DatagramSocket();
字节[] bufJ = Adaptor.createStringMsg(Adaptor.createJoingMsg(ID,Location.getX(),Location.getY()))的getBytes();
DatagramPacket类packetJ =新的DatagramPacket(bufJ,bufJ.length,serverAddr,目标);
Log.i(申请人,发送:'+新的String(bufJ)+');
socketJ.send(packetJ);
Log.i(申请人,完成。);

一些额外的信息。该节点1(emulatorA)对8000端口和节点2(emulatorB)的服务器在端口8001的服务器的客户端部分目标端口是正确读取。什么试图做的是设置重定向这样:

  // emulatorA
REDIR添加TCP:8000:8000
// emulatorB
REDIR添加TCP:8001:8001

不过,我不能让那些beetwen 2模拟器的任何通信。据我了解了android教程的它应该像这样工作 REDIR添加TCP:localhostPort:emulatorPort 。我坚持了下来:/。任何人都可以指出我错误或者给一些好的建议。为了记录在案,而我是一个单一的设备上测试通信(客户端伪造其他节点)的一切工作,所以我不认为这是在code的错误。

顺便说一下是否有任何人知道怎样才能得到这些仿真器2(洛加,logB)2的日志集?这将帮助我很多东西。

@Fred Grott
是的,我通过telnet conected每个模拟器。 THX与日志的提示。


解决方案

数据报套接字使用UDP。端口重定向你说,你的设置是TCP。如果您尝试:


  REDIR添加UDP:localhostPort:emulatorPort


您可能有更好的运气。
但是,如果你改变你的实现,以使用套接字,并通过流沟通,将工作过。

I want to communicate two emulators via DatagramSocket in Android. Each of them is a Node in a P2P system. Thus each of them has a server Thread and client Thread (created per GUI event). This is how I create server

public static final String SERVERIP = "10.0.2.15";
    //...
    run() {
                InetAddress serverAddr = InetAddress.getByName(SERVERIP);
                DatagramSocket socket = new DatagramSocket(SERVERPORT,serverAddr);
                while(true) {
                    byte[] buf = new byte[29];
                    DatagramPacket packet = new DatagramPacket(buf, buf.length);
                    socket.receive(packet);
                //... 
                }
     }

The port is given by the user during initializing application. The client part (requesting some data)

InetAddress serverAddr = InetAddress.getByName("10.0.2.2");
//...
Log.i("Requester", "Trying to connect to device port = "+target);
DatagramSocket socketJ = new DatagramSocket();
byte[] bufJ = Adaptor.createStringMsg(Adaptor.createJoingMsg(id, Location.getX(), Location.getY())).getBytes();             
DatagramPacket packetJ = new DatagramPacket(bufJ, bufJ.length, serverAddr, target);
Log.i("Requester", "Sending: '" + new String(bufJ) + "'");
socketJ.send(packetJ);
Log.i("Requester", "Done.");

Some additional info. The Node1 (emulatorA) has a server on port 8000 and Node2 (emulatorB) has a server on port 8001. The target port for "client part" is read properly. What tried to do is to set the redirection as such:

//emulatorA
redir add tcp:8000:8000
//emulatorB
redir add tcp:8001:8001

However I can not get any communication beetwen those 2 emulators. As far as I understood the android tutorial about it should work like this redir add tcp:localhostPort:emulatorPort. I'm stuck with it :/. Can anyone point me the mistake or give some good advice. For the record while I was testing communication on a single device (client faking other node) everything worked, so I don't think there is a bug in the code.

Btw does any one knows how can I get 2 set of logs for those 2 emulators (logA, logB)? It would help me a lot.

@Fred Grott Yes I have conected through telnet to each emulator. Thx for the tip with logs.

解决方案

Datagram Sockets use UDP. The port redirects you have stated that you setup are for TCP. If you try:

redir add udp:localhostPort:emulatorPort

You may have better luck. However if you change your implementation to use Sockets and communicate via streams that will work too.

这篇关于互连仿真器实例的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆