sockets - Connecting to TCP Protocol from Android Client -


i'am looking answer, , hope can me , guide me solution.

what want connect android device tcp protocol server exchange messages.

what have using example have found on github

i have modified connection ip 1 have. have succesfully managed connect server. got response server such as:

<?xml version="1.0" encoding="utf-8"?>  <messages>     <connection>accepted</connection>  </messages> 

and logcat shows:

net.stackueberflow.netcat i/netcat tcp client activity: host: 192.168.170.90, port: 3000 net.stackueberflow.netcat i/netcat tcp client activity: onpreexecute net.stackueberflow.netcat i/netcat tcp client activity: doinbackground: creating socket net.stackueberflow.netcat i/asynctask: doinbackground: socket created, streams assigned net.stackueberflow.netcat i/asynctask: doinbackground: waiting inital data... net.stackueberflow.netcat i/netcat tcp client activity: doinbackground: got data net.stackueberflow.netcat i/netcat tcp client activity: onprogressupdate: 100 bytes received. 

what needed

there problem after connected. when try send something, getting on logcat , nothing shows on output. message string have typed without response. logcat shows:

net.stackueberflow.netcat d/netcat tcp client activity: senddatatonetwork: writing received message socket 

and nothing happens more. keep sending messages without response back.

any appreciate it!

edit #1

package net.stackueberflow.netcat;  import java.io.file; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.io.unsupportedencodingexception; import java.net.inetsocketaddress; import java.net.socket; import java.net.socketaddress; import android.app.activity; import android.content.intent; import android.net.uri; import android.os.asynctask; import android.os.bundle; import android.text.method.scrollingmovementmethod; import android.util.log; import android.view.keyevent; import android.view.view; import android.view.view.onkeylistener; import android.widget.button; import android.widget.edittext; import android.widget.textview;  // todo: handle not connected  public class tcpclientactivity extends activity {     private static final string tag = "netcat tcp client activity";     private textview tv;     private edittext input;     private string host;     private int port;     private byte[] filebytes = null;     private uri uri;     private tcpclienttask networktask;   /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.connected);      tv = (textview)findviewbyid(r.id.output);     input = (edittext)findviewbyid(r.id.input);      intent = getintent();     host = i.getstringextra("host");     port = i.getintextra("port", 3000);       if ( (uri = (uri)i.getparcelableextra(intent.extra_stream)) != null) {         log.i(tag, "received file uri: "+uri.tostring());         try {             filebytes = iohelpers.readfile(new file(iohelpers.getrealpathfromuri(uri, this)));             input.setfocusable(false);             input.sethint("sending file "+iohelpers.getrealpathfromuri(uri, this));         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }     }      log.i(tag, "host: "+host+", port: "+port);       button button = (button)findviewbyid(r.id.button1);     networktask = new tcpclienttask();     networktask.execute();      input.setonkeylistener(new onkeylistener() {         @override         public boolean onkey(view v, int keycode, keyevent event) {             // if event key-down event on "enter" button             if ((event.getaction() == keyevent.action_down) &&                 (keycode == keyevent.keycode_enter)) {               // perform action on key press                 send();               return true;             }             return false;         }     });      button.setonclicklistener(new button.onclicklistener() {         @override         public void onclick(view v) {             send();         }     }); }  @override  public void onpause() {     super.onpause();     if (networktask != null)         networktask.cancel(true); }   private void send() {     if (filebytes != null)         sendblobtcp(uri);     else {         input.settext("" +                 "<login>\n" +                 "  <username>admin</username>\n" +                 "  <password>admin</password>\n" +                 "</login>\n");         string str = input.gettext().tostring();         networktask.senddatatonetwork(str.getbytes());         input.settext("");         appendtooutput(str);      } }  private void sendblobtcp(uri uri) {     networktask.senddatatonetwork(filebytes);     tv.append("\nsending file "+iohelpers.getrealpathfromuri(uri, this)); }  private void appendtooutput(string str) {     tv.append(str);     tv.setmovementmethod(new scrollingmovementmethod()); }  private void appendtooutput(byte[] data) {     string str;     try {         str = new string(data, "utf8");         appendtooutput(str);     } catch (unsupportedencodingexception e) {         // todo auto-generated catch block         e.printstacktrace();     } }   private class tcpclienttask extends asynctask<void, byte[], boolean> {     socket socket; //network socket     inputstream is; //network input stream     outputstream os; //network output stream     byte[] buffer = new byte[4096];      @override     protected void onpreexecute() {         log.i(tag, "onpreexecute");     }      @override     protected boolean doinbackground(void... params) { //this runs on different thread         boolean result = false;         try {             // connect address             log.i(tag, "doinbackground: creating socket");             socketaddress sockaddr = new inetsocketaddress(host, port);             socket = new socket();             socket.connect(sockaddr, 5000); //10 second connection timeout             if (socket.isconnected()) {                 = socket.getinputstream();                 os = socket.getoutputstream();                 log.i("asynctask", "doinbackground: socket created, streams assigned");                 log.i("asynctask", "doinbackground: waiting inital data...");                 int read;                 //this blocking                 while((read = is.read(buffer, 0, 4096)) > 0 ) {                     byte[] tempdata = new byte[read];                     system.arraycopy(buffer, 0, tempdata, 0, read);                     publishprogress(tempdata);                     log.i(tag, "doinbackground: got data");                 }             }         } catch (ioexception e) {             e.printstacktrace();             log.i(tag, "doinbackground: ioexception");             result = true;         } catch (exception e) {             e.printstacktrace();             log.i(tag, "doinbackground: exception");             result = true;         } {             try {                 is.close();                 os.close();                 socket.close();             } catch (ioexception e) {                 e.printstacktrace();             } catch (exception e) {                 e.printstacktrace();             }             log.i(tag, "doinbackground: finished");         }         return result;     }      public boolean senddatatonetwork(final byte[] cmd) { //you run main thread.         // wait until socket open , ready use          if (socket.isconnected()) {             log.d(tag, "senddatatonetwork: writing received message socket");             new thread(new runnable() {                 public void run() {                     try {                         os.write(cmd);                     }                     catch (exception e) {                         e.printstacktrace();                         log.i(tag, "senddatatonetwork: message send failed. caught exception");                     }                 }             }                     ).start();             return true;         }         else             log.i(tag, "senddatatonetwork: cannot send message. socket closed");          return false;     }      @override     protected void onprogressupdate(byte[]... values) {         if (values.length > 0) {             log.i(tag, "onprogressupdate: " + values[0].length + " bytes received.");             appendtooutput(buffer);         }     }      @override     protected void oncancelled() {         log.i(tag, "cancelled.");     }     @override     protected void onpostexecute(boolean result) {         if (result) {             log.i(tag, "onpostexecute: completed error.");          } else {             log.i(tag, "onpostexecute: completed.");         }     }     } } 

the answer solution needed end transaction via eot command. in java is:

"/4"  

this solved issue


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -