sockets - Android TCP Client in phone has problems in communicating with external server -


i have tcp client in android (java program in eclipse). server java app running in eclipse. works fine in situation.

when tried receive message colleague's app (developed in rhapsody , think c++), receive message after app closed , not while app running , sending messages. have idea why happens?

thank time , effort on this.

cheers, madhu

the java server this:

public class tcpsendserver implements runnable{     public static final string serverip = "192.168.178.24";     public static final int serverport = 1200;     //static category cat = category.getinstance(tcpsendserver.class.getname());     //cat.debug("start of main()");      public void run() {          try {              system.out.println("s: connecting...");                           serversocket serversocket = new serversocket(serverport);              string msg = "<msg><n>shiftdirection</n><v>1</v></msg>";              string msg1 = "<msg><n>vehiclespeed</n><v>120</v></msg>";              string msg2 = "sd<!n><v>0<!v><!msg>";               //while (true) {                  socket client = serversocket.accept();                    try {                       system.out.println("s: sending: '" + msg + "'");                       printwriter out = new printwriter( new bufferedwriter( new outputstreamwriter(client.getoutputstream())),true);                                           thread.sleep (5000);                       out.println(msg);                       thread.sleep (5000);                       //out.println(msg2);                       thread.sleep (5000);                       out.println(msg1);                       //out.flush();                       system.out.println("s: sent.");                       system.out.println("s: done.");                     } catch(exception e) {                         system.out.println("s: error");                         e.printstacktrace();                     } //finally {                       //    client.close();                                             //}                //}          } catch (exception e) {              system.out.println("s: first try error");              e.printstacktrace();          }     }      public static void main (string a[]) {         thread desktopserverthread = new thread(new tcpsendserver());         desktopserverthread.start();     } } 

the android client code: main activity:

public class tcplisten extends activity implements tcplistener {     private textview mtitle;     public string data[] = new string[2];      /** called when activity first created. */      @override            public void oncreate(bundle savedinstancestate) {                super.oncreate(savedinstancestate);                //setcontentview(r.layout.main);                        // set window layout              requestwindowfeature(window.feature_custom_title);              setcontentview(r.layout.main);              getwindow().setfeatureint(window.feature_custom_title, r.layout.custom_title);               // set custom title              mtitle = (textview) findviewbyid(r.id.title_left_text);              mtitle.settext(r.string.app_name);              mtitle = (textview) findviewbyid(r.id.title_right_text);               //tcpservicehandler handler=new tcpservicehandler(this);                //handler.execute("192.168.178.24");                 tcpservicehandler handler = new tcpservicehandler(this,this);                thread th = new thread(handler);                th.start();                       }                     public string[] callcompleted(string source){                  log.d("tcp", "std parser " + source);                 mtitle.settext(source);                 //string data[] = new string[2];                   //if (source.matches("<msg><n>.*</n><v>.*</v></msg>"))  {                                document doc = null;                       try{                        documentbuilderfactory dbf = documentbuilderfactory.newinstance();                          documentbuilder db = dbf.newdocumentbuilder();                          doc = (document) db.parse(new bytearrayinputstream(source.getbytes()));                          nodelist n = doc.getelementsbytagname("n");                          node nd = n.item(0);                          string msgname = nd.getfirstchild().getnodevalue();                          nodelist n1 = doc.getelementsbytagname("v");                          node nd1 = n1.item(0);                          string tmpval = nd1.getfirstchild().getnodevalue();                          data[0] = msgname;                          data[1] = tmpval;                         log.d("tcp", "inside std parser " + data[0] + " " + data[1]);                        //actionondata(data[0], data[1]);                       }                       catch(exception e){                       e.printstacktrace();                   }                 log.d("tcp", "just outside std parser " + data[0] + " " + data[1]);                 return data;                 //} else log.d("tcp", "message in wrong format " + source);                 //mtitle.settext("message in wrong format " + source);                 //return data;             } 

interface:

public interface tcplistener {     public string[] callcompleted(string msg); } 

tcpservicehandler:

public class tcpservicehandler implements runnable {        tcplistener _listener;                  private activity _act;        public tcpservicehandler(tcplistener listener, activity act){              _listener = listener;            _act = act;        }               public synchronized void run() {            // todo auto-generated method stub                     //if(socket==null){                  try {                    inetaddress serveraddr = inetaddress.getbyname("192.168.178.25");                    socket socket = new socket(serveraddr, 1200);            //                    while(true){                     try {                                                     bufferedreader in = new bufferedreader(new inputstreamreader(socket.getinputstream()));                            final string str = in.readline();                            this._act.runonuithread(new runnable(){                             public void run() {                                _listener.callcompleted(str);                                }                                                          });                                                                     }                     catch(exception e){                         e.printstacktrace();                     }                    }                } catch (unknownhostexception e) {                    // todo auto-generated catch block                    e.printstacktrace();                } catch (ioexception e) {                    // todo auto-generated catch block                    e.printstacktrace();                }        }      } 

this problem serverip here. running app emulator in local machine? emulator not part of lan. emulator runs behind virtual router/firewall service isolates development machine's network interfaces , settings , internet.

so need use network redirections or port forwarding achieve communication server on separate machine.

if running app on device can make device part of network , should work.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -