How to send msg using Socket between two android devices.? -


here code

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_server);     serverstatus = (textview) findviewbyid(r.id.server_status);      serverip = getlocalipaddress();      thread fst = new thread(new serverthread());     fst.start(); }  public class serverthread implements runnable {      public void run() {         try {             if (serverip != null) {                 handler.post(new runnable() {                     @override                     public void run() {                         serverstatus.settext("listening on ip: " + serverip);                     }                 });                 serversocket = new serversocket(serverport);                 while (true) {                     // listen incoming clients                     socket client = serversocket.accept();                     handler.post(new runnable() {                         @override                         public void run() {                             serverstatus.settext("connected.");                         }                     });                      try {                         bufferedreader in = new bufferedreader(new inputstreamreader(client.getinputstream()));                         string line = null;                         while ((line = in.readline()) != null) {                             log.d("serveractivity", line);                             handler.post(new runnable() {                                 @override                                 public void run() {                                     // whatever want front end                                     // can creative                                 }                             });                         }                         break;                     } catch (exception e) {                         handler.post(new runnable() {                             @override                             public void run() {                                 serverstatus.settext("oops. connection interrupted. please reconnect phones.");                             }                         });                         e.printstacktrace();                     }                 }             } else {                 handler.post(new runnable() {                     @override                     public void run() {                         serverstatus.settext("couldn't detect internet connection.");                     }                 });             }         } catch (exception e) {             handler.post(new runnable() {                 @override                 public void run() {                     serverstatus.settext("error");                 }             });             e.printstacktrace();         }     } }  // gets ip address of phone's network private 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()) {                      boolean isipv4 = inetaddressutils.isipv4address(inetaddress.gethostaddress().tostring());                     if(isipv4)                     return inetaddress.gethostaddress().tostring(); }             }         }     } catch (socketexception ex) {         log.e("serveractivity", ex.tostring());     }     return null; }  @override protected void onstop() {     super.onstop();     try {          // make sure close socket upon exiting          serversocket.close();      } catch (ioexception e) {          e.printstacktrace();      } } 

client side

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_client);      serverip = (edittext) findviewbyid(r.id.server_ip);     connectphones = (button) findviewbyid(r.id.connect_phones);     connectphones.setonclicklistener(connectlistener); }  private onclicklistener connectlistener = new onclicklistener() {      @override     public void onclick(view v) {         if (!connected) {             log.e("clicked", "yes");             serveripaddress = serverip.gettext().tostring();             if (!serveripaddress.equals("")) {                 thread cthread = new thread(new clientthread());                 cthread.start();             }         }     } };  public class clientthread implements runnable {      public void run() {         try {             inetaddress serveraddr = inetaddress.getbyname(serveripaddress);             log.d("clientactivity", "c: connecting...");             socket socket = new socket(serveraddr, serveractivity.serverport);             connected = true;             while (connected) {                 try {                     log.d("clientactivity", "c: sending command.");                     printwriter out = new printwriter(new bufferedwriter(new outputstreamwriter(socket                                 .getoutputstream())), true);                         // issue commands                         out.println("hey server!");                         log.d("clientactivity", "c: sent.");                 } catch (exception e) {                     log.e("clientactivity", "s: error", e);                 }             }             socket.close();             log.d("clientactivity", "c: closed.");         } catch (exception e) {             log.e("clientactivity", "c: error", e);             connected = false;         }     } } } 

i have used between 2 emulators server showing listening on 10.0.2.15

but when go connection not connect

thers no error in logcat have checked between 2 physical devices running android 4.0 3g internet want push android devices running server activity client activity can perform action when client send something.

nothing happens when click connect button

permissions:

  <uses-permission android:name="android.permission.internet" /> 

there's demo bluetooth chat , wifi direct in android sdk. should use bluetooth or wifi. don't think can acvhieve direct connection using cellular network.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -