P2P message sending using NAT punch-through in Android -


i try send message 1 phone using kryonet (that uses socket communication) , intend use nat punch-through storing public address of clients.

the following code used:

public class mainactivity extends activity implements view.onclicklistener {   /**    * called when activity 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 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 {       (enumeration<networkinterface> en = networkinterface.getnetworkinterfaces(); en.hasmoreelements();) {         networkinterface intf = en.nextelement();         (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 sending: "+ip);           runonuithread(new runnable()           {             @override             public void run()             {               toast.maketext(mainactivity.this, "client sending: "+ip, toast.length_short).show();             }           });            string request = "here 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();   } } 

if phones on same wifi, works okay. if 1 of them on 3g, external ip address (like: 100.65.96..), , local address gateway. knowing external ip key nat pt, thought enough message, timeout.

what else should implement make 3g phone message?

you can using mediator server helps establishing connection between phones.

  1. create server program can accept udp sockets , can store public address of phone connects it
  2. connect server phone a register public address
  3. use phone b connect server asking public address of phone a
  4. once have address, can create direct socket phone a phone b using public address. nat rest, , router send packages phone's private address

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -