Friday 15 March 2013

android - my app could not send a message from my real device to emulator! What is goes wrong? is needed to more settings? -



android - my app could not send a message from my real device to emulator! What is goes wrong? is needed to more settings? -

i new android.i have started work simple chat app. wrote short code udp socket 1 client , 1 server.now have issues @ connectivity android real device client app installed on , , emulator device server app run on it. client app simple code must set ip connection emulator , has edittext sending message.(i @ point set 10.0.2.2 or 10.0.2.15. app not send message real device emulator! goes wrong? needed more settings?) server app has textview getting , showing recived message. here client code:

import java.io.ioexception; import java.net.datagrampacket; import java.net.datagramsocket; import java.net.inetaddress; import java.net.socketexception; import java.net.unknownhostexception; import android.app.activity; import android.os.bundle; import android.os.handler; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; public class clienttextchatactivity extends activity { private string udpmsg=null; handler hand=new handler(); edittext edtsetip=null , edttext=null; button btnsetip=null , btnsend=null; static string ip=null; static final string log_tag = "udpstream"; static final int port = 8888; static final int buf_size=4096; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); edttext=(edittext)findviewbyid(r.id.edttext); edtsetip=(edittext)findviewbyid(r.id.edtsetip); btnsetip=(button)findviewbyid(r.id.btnsetip); btnsend=(button)findviewbyid(r.id.btnsend); btnsetip.setonclicklistener(new onclicklistener() { public void onclick(view v) { ip=edtsetip.gettext().tostring(); } }); btnsend.setonclicklistener(new onclicklistener() { public void onclick(view v) { thread mythread=new thread(new th1() ); mythread.start(); } }); } //############################################# public class th1 implements runnable { public void run() { udpmsg =edttext.gettext().tostring(); log.d(log_tag,udpmsg); datagramsocket ds = null; seek { ds = new datagramsocket(); inetaddress serveraddr = inetaddress.getbyname(ip); log.d(log_tag,"address server address created."); datagrampacket dp; dp = new datagrampacket(udpmsg.getbytes(), udpmsg.length(), serveraddr, port); ds.send(dp); log.d(log_tag,"packet send."); } grab (socketexception e) { e.printstacktrace(); } grab (unknownhostexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } grab (exception e) { e.printstacktrace(); } { if (ds != null) { ds.close(); } } }//end run }//end class th }

make sure emulator started, in adb shell, type command, show ip address of emulator.

adb shell ifconfig etho

android client-server emulator

No comments:

Post a Comment