[英]P2P message sending using NAT punch-through in Android
我嘗試使用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手機獲得信息?
您可以使用中介服務器來幫助建立電話之間的連接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.