在Android中使用NAT穿通的P2P消息发送

时间:2014-06-27 22:24:01

标签: android p2p nat kryonet

我尝试使用Kryonet(使用套接字通信)从一部手机向另一部手机发送消息,我打算通过存储客户端的公共地址来使用NAT穿越。

使用以下代码:

public class MainActivity extends Activity implements View.OnClickListener
{
  /**
   * Called when the activity is first created.
   */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button button = ((Button) findViewById(R.id.btnTest));
    button.setOnClickListener(this);
    TextView textViewOwn = ((TextView) findViewById(R.id.lblOwnIP));
    textViewOwn.setText(getLocalIpAddress()+"\n"+GetGateway());
    try
    {
      Server server = new Server();
      server.start();
      server.bind(54555, 54777);
      Log.i("DEBUG", "Server is listening");
      final TextView textView = ((TextView) findViewById(R.id.txtMessage));
      server.addListener(new Listener()
      {
        public void received(Connection connection, Object object)
        {
          if (object instanceof String)
          {
            String request = (String) object;
            Log.i("DEBUG", request);
            ShowMessage(request, textView);
            String response = "Thanks";
            connection.sendTCP(response);
          }
        }
      });

    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  public static String getLocalIpAddress() {
    try {
      for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
        NetworkInterface intf = en.nextElement();
        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
          InetAddress inetAddress = enumIpAddr.nextElement();
          if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
            return inetAddress.getHostAddress();
          }
        }
      }
    } catch (SocketException ex) {
      ex.printStackTrace();
    }
    return null;
  }

  private String GetGateway()
  {
    DhcpInfo d;
    WifiManager wifii;
    wifii= (WifiManager) getSystemService(Context.WIFI_SERVICE);
    d=wifii.getDhcpInfo();
    int gatewayip = d.gateway;
    String mask = Formatter.formatIpAddress(gatewayip);
    return mask;
  }

  private void ShowMessage(final String request, final TextView textView)
  {
    runOnUiThread(new Runnable()
    {
      @Override
      public void run()
      {
        textView.setText(request);
      }
    });
  }

  @Override
  public void onClick(View view)
  {
    final String ip = ((EditText) findViewById(R.id.txtIP)).getText().toString();
    if (TextUtils.isEmpty(ip))
    {
      Toast.makeText(this, "No IP address", Toast.LENGTH_SHORT).show();
      return;
    }
    Runnable runnable = new Runnable()
    {
      public void run()
      {
        try
        {
          Client client = new Client();
          client.start();

          client.connect(5000, ip, 54555, 54777);
          Log.i("DEBUG", "Client is sending: "+ip);
          runOnUiThread(new Runnable()
          {
            @Override
            public void run()
            {
              Toast.makeText(MainActivity.this, "Client is sending: "+ip, Toast.LENGTH_SHORT).show();
            }
          });

          String request = "Here is the request";
          client.sendTCP(request);
        }
        catch (final Exception e)
        {
          e.printStackTrace();
          runOnUiThread(new Runnable()
          {
            @Override
            public void run()
            {
              Toast.makeText(MainActivity.this, "Client has errors: "+e.toString(), Toast.LENGTH_SHORT).show();

            }
          });
        }
      }
    };
       Thread thread=new Thread(runnable);
    thread.start();
  }
}

如果手机使用相同的WIFI,则可以正常使用。如果其中一个在3G上,我得到一个外部IP地址(如:100.65.96 ..),以及一个本地地址作为网关。由于知道外部IP是NAT PT的关键,我认为它足以获取消息,但我得到超时

我应该采取什么措施让3G手机获得信息?

1 个答案:

答案 0 :(得分:1)

您可以使用中介服务器来帮助建立手机之间的连接。

  1. 创建一个可以接受UDP套接字的服务器程序,并可以存储连接到它的手机的公共地址
  2. 使用电话A 连接到该服务器以注册其公共地址
  3. 使用手机B 连接服务器,询问手机A的公开地址
  4. 获得该地址后,您可以使用其公共地址从手机A 手机B 创建直接套接字。 NAT将完成剩下的工作,路由器会将软件包发送到手机的私人地址